PostgreSQL patterns for Python with psycopg and asyncpg — connection pooling,
99
99%
Does it follow best practices?
Impact
99%
1.15xAverage score across 5 eval scenarios
Passed
No known issues
A fintech startup needs an event logging pipeline for their Python backend that records every financial transaction, API call, and user action for compliance and debugging. The system must be reliable -- no events can be silently dropped.
The PostgreSQL database has these tables:
event_sources -- id, name (unique), description, api_key, is_active, created_atevents -- id, source_id (FK to event_sources), event_type, severity (info/warning/error/critical), payload (JSONB), ip_address, created_atevent_archives -- same columns as events, used for moving old events out of the hot tableThe pipeline needs:
Register event source -- Create a new event source or update it if a source with that name already exists (the API key and description might change).
Log events -- Accept a list of events from a source (the queue delivers them in batches of 50-1000) and persist them. The source_id foreign key must be validated. All events in a batch must be stored atomically -- either all succeed or none.
Query recent events -- Given a source name and severity level, return matching events from the last 24 hours, most recent first.
Archive old events -- Move events older than 90 days from events to event_archives. This involves reading old events, inserting them into the archive table, then deleting the originals. For data integrity, this must be done in a transaction. The archive table can have millions of rows, so the archival must process events in batches to avoid long-held locks.
The application uses psycopg 3 (synchronous) with Flask.
Produce:
app/db.py -- Database pool setup and connection helperapp/pipeline.py -- The four functions: register_source, log_events, query_recent, archive_old_eventsapp/main.py -- Flask application with shutdown hookevals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
skills
postgresql-python-best-practices