Stage 5 track
Track B: Backend Engineer
Goal: Put PostgreSQL behind a real application — drivers done safely, ORMs kept honest, migrations, pooling, least-privilege roles, Row-Level Security, and the queue patterns — shipping a multi-tenant SaaS database as your capstone. Time: 4–6 weeks alongside the capstone · Builds on: Stages 3 and 4 especially
Who this track is for
You build applications, and the database is the part of the stack you want to stop treating as a black box. Parts 10–12 felt load-bearing; this track turns them into production habits.
The path
1. PostgreSQL from Python. psycopg 3 is the modern driver: connections, cursors, transactions mapped to exactly the BEGIN/COMMIT semantics you drilled in Part 12 (with conn.transaction(): is that lesson as a context manager). Everything here transfers to any language's driver — the concepts are the course's, the syntax is local.
2. SQL injection and parameterized queries. The non-negotiable: SQL text and user values travel separately — cur.execute("... WHERE email = %s", (email,)). String formatting into SQL is the vulnerability that never dies; parameterization kills it by construction, because a value can never become syntax. Every driver has placeholders; there is no legitimate exception in application code.
3. ORMs vs raw SQL. SQLAlchemy (and its peers) excel at CRUD plumbing, model definitions, and migrations integration — and quietly manufacture Part 18's N+1 under default lazy loading. The working rule: ORM for writes and simple reads, eager-load relationships deliberately, and drop to real SQL (Parts 5–8) for reporting and anything with a window function. Measure with pg_stat_statements; the ORM's calls column tells no lies.
4. Schema migrations. Part 13's closing rule — database logic lives in versioned files — becomes tooling: Alembic (or Flyway) numbers every schema change, applies them in order, records the current version in the database. The zero-downtime discipline on top: additive first (add a nullable column, backfill it, then add the constraint), never rename-in-place, and Part 17's CONCURRENTLY for every index on a live table.
5. Connection pooling. Part 15 made connections expensive (a process each); PgBouncer makes a few hundred app connections share a few dozen real ones. The catch you must design for: in transaction pooling mode, consecutive transactions may run on different server connections — so session state (SET, session advisory locks, prepared statements) can't be relied on across transactions. Part 12's pg_advisory_xact_lock habit was preparing you for this.
6. Roles and privileges. The app should not connect as a superuser-ish owner. GRANT/REVOKE least privilege: a migration role that owns the schema, an app role with table-level DML only, a read-only role for analytics. One revoked DELETE has saved many a company.
7. Row-Level Security. The multi-tenant question — "how do I guarantee tenant A never sees tenant B's rows?" — answered in the database: RLS policies filter every query by tenant_id = current_setting('app.tenant_id')::int, enforced below the application, immune to a forgotten WHERE. Remember the owner bypass: test policies as the app role (and know FORCE ROW LEVEL SECURITY).
8. Job queues and events. You already built the queue: Part 12's FOR UPDATE SKIP LOCKED is the production idiom, verbatim. Add LISTEN/NOTIFY — transactional pub/sub, delivered on commit — and workers stop polling. For many systems this pair deletes an entire message-broker dependency.
Capstone: the multi-tenant SaaS database
A small "team notes" SaaS (any web framework, or bare psycopg):
- Schema under Alembic from migration 001: tenants, users, notes — every table carrying
tenant_id. - RLS policies on every tenant table; a test proving cross-tenant reads return zero rows even with a buggy query.
- Three roles: migrator (owner), app (DML only), readonly — with a failed-DELETE proof for readonly.
- A background worker claiming jobs via SKIP LOCKED, woken by NOTIFY.
- PgBouncer in front, transaction mode — and the code audited for session-state assumptions.
Done when: the cross-tenant test suite passes with RLS on and fails loudly with it off, and a migration adding a column ships with zero seconds of downtime.