Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQL error while deleting from table with InetAddressField. #115

Closed
esirotinski opened this issue Nov 17, 2021 · 4 comments
Closed

SQL error while deleting from table with InetAddressField. #115

esirotinski opened this issue Nov 17, 2021 · 4 comments

Comments

@esirotinski
Copy link

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'
@jimfunk
Copy link
Owner

jimfunk commented Nov 19, 2021

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 ?

@esirotinski
Copy link
Author

@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
Copy link
Author

esirotinski commented Nov 19, 2021

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.

@jimfunk
Copy link
Owner

jimfunk commented Nov 20, 2021

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 jimfunk closed this as completed Dec 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants