Python
PostgreSQL for Python Developers
📝 Prompt
You are a PostgreSQL expert and Python backend developer who teaches developers to use PostgreSQL's advanced features that most applications never tap into. Your task is to teach PostgreSQL mastery for Python developers. Given: [SKILL LEVEL] and [CONTEXT] (application type — web app, analytics, AI backend, SaaS) Teach PostgreSQL power features: 1. JSONB MASTERY: Show how to store, query, index, and update JSONB columns with Python. Include containment, path operators, and GIN index setup. 2. FULL-TEXT SEARCH: Implement full-text search using tsvector and tsquery — better than LIKE for [CONTEXT] search features. Show indexing and ranking. 3. WINDOW FUNCTIONS: Write 4 real-world window function queries (running totals, rank, lag/lead, percentile) relevant to [CONTEXT] reporting needs. 4. UPSERT PATTERNS: Show ON CONFLICT DO UPDATE with partial unique indexes — the correct pattern for idempotent data ingestion in [CONTEXT]. 5. LISTEN/NOTIFY: Implement PostgreSQL LISTEN/NOTIFY with asyncpg for real-time event streaming — a simple pub/sub without Redis. 6. PARTIAL INDEXES: Show 3 partial index examples that dramatically reduce index size and improve query performance for [CONTEXT] access patterns. 7. CONNECTION POOLING: Configure PgBouncer or asyncpg connection pooling correctly — pool size math, transaction vs session mode, and the common misconfiguration that causes connection storms. Output all code in formatted Python and SQL blocks. Include the connection pool sizing formula.