django-postgresql-netfields icon indicating copy to clipboard operation
django-postgresql-netfields copied to clipboard

SQL error while deleting from table with InetAddressField.

Open esirotinski opened this issue 3 years ago • 4 comments

Hello,

could you please help me identify what's the problem with my setup? I'm running Django version 3.2.9 with the models presented below. Been able to insert Addresses to Postgresql database, but when I'm trying to delete Addresses it hangs up indefinitely and throws the following error when CTRL+C is pressed:

psql# delete from addresses;
psql# ERROR: canceling statement due to user request
psql# CONTEXT: SQL Statement "SELECT 1 FROM ONLY "public":"addresses" x WHERE "address" OPERATOR(pg_catalog.=) $1 FOR KEY SHARE OF x"

Models:

class Prefix(models.Model):
    class Version(models.IntegerChoices):
        FOUR = 4
        SIX = 6

    prefix = CidrAddressField(unique=True)
    version = models.IntegerField(choices=Version.choices, null=False, blank=False)
    country = models.ForeignKey(Country, on_delete=models.CASCADE)
    created = models.DateTimeField(auto_now_add=True, null=True, blank=True)
    last_updated = models.DateTimeField(auto_now=True, null=True, blank=True)
    objects = NetManager()

    def __str__(self):
        return format(self.prefix)

    class Meta:
        indexes = (
            GistIndex(
                fields=('prefix',), opclasses=('inet_ops',),
                name='prefixes_inet_idx' 
            ),
        )
        db_table = 'prefixes'


class Address(models.Model):
    class Version(models.IntegerChoices):
        FOUR = 4
        SIX = 6

    address = InetAddressField(primary_key=True, unique=True, null=False, blank=False)          
    version = models.IntegerField(choices=Version.choices, null=False, blank=False)    
    prefix = models.ForeignKey(Prefix, on_delete=models.CASCADE)
    created = models.DateTimeField(auto_now_add=True, null=True, blank=True)
    last_updated = models.DateTimeField(auto_now=True, null=True, blank=True)
    objects = NetManager()

    def __str__(self):
        return format(self.address)

    class Meta:
        indexes = (
            GistIndex(
                fields=('address',), opclasses=('inet_ops',),
                name='addresses_inet_idx'
            ),
        )
        db_table = 'addresses'

esirotinski avatar Nov 17 '21 20:11 esirotinski

I've never seen anything like that. Seems like a postgres-level thing.

Have you seen https://stackoverflow.com/questions/44952432/delete-query-in-postgres-hangs-indefinitely ?

jimfunk avatar Nov 19 '21 14:11 jimfunk

@jimfunk , yes, I've seen the stackoverflow post, but the solution did not work for me :( Also I did not identify any locks in the database.

Thanks a lot for your reply, I'll investigate further.

esirotinski avatar Nov 19 '21 18:11 esirotinski

I've removed the GistIndex from the Meta class, then created index manually as specified on stackoverflow and now I'm able to delete addresses.

esirotinski avatar Nov 19 '21 19:11 esirotinski

Odd. Perhaps it has something to do with the how Django sets up the Gist index. That would not be part of this project.

jimfunk avatar Nov 20 '21 14:11 jimfunk