{"id":26905,"library":"django-pglocks","title":"django-pglocks","description":"Provides useful context managers for PostgreSQL advisory locks in Django. Version 1.0.4 (latest) released Aug 2021. Low maintenance, stable, no major changes expected.","status":"maintenance","version":"1.0.4","language":"python","source_language":"en","source_url":"https://github.com/Xof/django-pglocks","tags":["django","postgresql","advisory-locks","postgres"],"install":[{"cmd":"pip install django-pglocks","lang":"bash","label":"Install from PyPI"}],"dependencies":[{"reason":"Core dependency; operates within Django's ORM and database connections.","package":"django","optional":false},{"reason":"Required for PostgreSQL support; uses psycopg2 features.","package":"psycopg2","optional":false}],"imports":[{"note":"Renaming is fine but not needed; just use standard import.","wrong":"from django_pglocks import advisory_lock as lock","symbol":"advisory_lock","correct":"from django_pglocks import advisory_lock"}],"quickstart":{"code":"from django_pglocks import advisory_lock\n\nwith advisory_lock('my_lock_key'):\n    # critical section\n    pass","lang":"python","description":"Acquire a PostgreSQL advisory lock using a string key. The lock is released when the context exits."},"warnings":[{"fix":"Always use the context manager; avoid manual acquire/release.","message":"Advisory locks are not automatically released on transaction rollback; ensure the context manager exits properly.","severity":"gotcha","affected_versions":"all"},{"fix":"Use unique, descriptive key strings; consider prepending a namespace.","message":"Lock key collisions: The library hashes strings to 64-bit integers; different strings may collide (hash collision).","severity":"gotcha","affected_versions":"all"},{"fix":"Keep the same database connection active within the lock context.","message":"PostgreSQL advisory locks are session-level; closing the connection releases all locks. Ensure connection persistence inside the critical section.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-04-27T00:00:00.000Z","next_check":"2026-07-26T00:00:00.000Z","problems":[{"fix":"Ensure the lock is acquired inside a view or management command where a DB connection is available.","cause":"advisory_lock called outside of a database connection context (e.g., before Django is fully configured).","error":"AttributeError: 'NoneType' object has no attribute 'cursor'"},{"fix":"Use different keys for nested locks or avoid nesting same-key locks.","cause":"Trying to acquire the same lock twice in the same session without releasing it (e.g., nested locks with same key).","error":"OperationalError: advisory lock already held by this session"},{"fix":"Run `pip install django-pglocks`.","cause":"Library not installed in current environment.","error":"ModuleNotFoundError: No module named 'django_pglocks'"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}