registry
@automindzsheets 01–77shadcn 4.x
SHEET 76

Postgres Client (Python)

item / server / data

One psycopg 3 access layer instead of five: lazily pooled, query/one/execute/transaction, bounded retry on connection-level failures only, healthcheck, and the two settings a Supabase pooler needs.

type
item
files
2
registry deps
none
npm deps
none
env vars
DATABASE_URL, DATABASE_POOL_MAX, DATABASE_STATEMENT_TIMEOUT_MS, DATABASE_CONNECT_TIMEOUT_S

installs

  • ~/backend/db_client.py
  • ~/backend/tests/test_db_client.py
  • psycopg 3 imported lazily; one process-wide pool, dict rows, query / one / execute / transaction / healthcheck
  • A lost connection is retried at checkout, never after your statement was sent — replaying an INSERT that may already have committed writes it twice
  • Never logs parameters or the DSN; the password is redacted in every error, including the chained cause

import db_client; rows = db_client.query("select * from jobs where id = %s", [job_id]) — dict rows; one() returns a row or None and raises on more than one; execute() returns the row count; with db_client.transaction() as tx: runs BEGIN/COMMIT/ROLLBACK on one connection; healthcheck() is a bool for the health item; close() at shutdown. One process-wide pool, opened lazily and once (locked); Db(dsn=..., driver=...) for tests or a second database. Requires DATABASE_URL at call time — missing means a DbError naming the var and no connection attempt; DATABASE_POOL_MAX (10), DATABASE_STATEMENT_TIMEOUT_MS (30000, applied per connection so it survives the Supabase pooler), DATABASE_CONNECT_TIMEOUT_S (10) are optional. The driver is yours: pip install "psycopg[binary,pool]"; it is imported lazily and the error tells you what to install. Retry is phase-aware: connection errors at checkout are retried with jittered backoff, but after a statement is sent only SQLSTATEs the server itself rolled back (40001, 40P01, 57P01) are — a lost connection mid-statement surfaces, because replaying an INSERT that may have committed writes it twice; nothing is ever retried inside your transaction. Parameters never reach a log; the DSN password is redacted in every error, chained cause included. Prepared statements are off for the pooler. Run the shipped test with cd backend && python3 -m unittest tests/test_db_client.py.

Variations

one state per section

No previews for this item yet.