CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl-labs/postgresql-python-best-practices

PostgreSQL patterns for Python with psycopg and asyncpg — connection pooling,

99

1.15x
Quality

99%

Does it follow best practices?

Impact

99%

1.15x

Average score across 5 eval scenarios

SecuritybySnyk

Passed

No known issues

Overview
Quality
Evals
Security
Files

task.mdevals/scenario-1/

Event Logging Pipeline

Problem/Feature Description

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_at
  • events -- id, source_id (FK to event_sources), event_type, severity (info/warning/error/critical), payload (JSONB), ip_address, created_at
  • event_archives -- same columns as events, used for moving old events out of the hot table

The pipeline needs:

  1. 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).

  2. 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.

  3. Query recent events -- Given a source name and severity level, return matching events from the last 24 hours, most recent first.

  4. 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.

Output Specification

Produce:

  • app/db.py -- Database pool setup and connection helper
  • app/pipeline.py -- The four functions: register_source, log_events, query_recent, archive_old_events
  • app/main.py -- Flask application with shutdown hook

evals

scenario-1

criteria.json

task.md

tile.json