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 healthcare platform needs a notification service that manages patient appointment reminders, prescription alerts, and system notifications. The service is built with Python and PostgreSQL and must handle both creating notifications in bulk (when a batch of appointments is scheduled) and querying them efficiently.
The PostgreSQL database has these tables:
patients -- id, name, email, phone, preferred_channel (email/sms/push), created_atnotifications -- id, patient_id (FK), notification_type (appointment_reminder/prescription_alert/system), title, body, channel, status (pending/sent/failed/read), scheduled_at, sent_at, created_at, updated_atnotification_templates -- id, notification_type, channel, subject_template, body_template, created_atThe service needs:
Schedule bulk notifications -- Given a list of patient IDs and a notification type, look up each patient's preferred channel, find the matching template, and create notification records for all of them. Appointment reminders come in batches of 200-1000 patients. All notifications in a batch must be created atomically.
Mark notifications as sent -- Update a list of notification IDs from "pending" to "sent" and record the sent_at timestamp. This happens in batches of 50-200 after the delivery service confirms delivery.
Patient notification history -- Return a patient's notification history (all types) sorted by most recent first. Some patients have thousands of notifications over years of treatment, so the query must handle large histories.
Pending notifications feed -- Return all pending notifications scheduled before now, grouped by channel, ordered by scheduled time. This query runs every minute and the results feed into the email/SMS/push delivery workers.
The application uses psycopg 3 (synchronous) and runs as a standalone service.
Produce:
app/db.py -- Connection pool setupapp/notifications.py -- The four functions: schedule_bulk, mark_sent, patient_history, pending_feedapp/main.py -- Application entry point with pool lifecycleevals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
skills
postgresql-python-best-practices