Act IV — Expansion#
Month 6. Porto is bustling, nightly partner feeds arrive at midnight, Rocco the Rhino is running connection pooling at the door, and a Polar Bear analytics team plus a flock of Duck auditors just flew in.
Polar Bear: We brought Polars! Just hand us the door scans — and please, no CSVs.
Duck: And we brought DuckDB and Parquet! Let's audit the entire dormitory before lunch.
Rocco: One transaction at a time, folks. Once you commit, I forget you were ever here.
Month 6. The single Lisbon hostel is now a multi-city chain with IoT door scanners and a full data ecosystem. In Act IV you'll ingest nightly feeds via pg_temp staging + MERGE … RETURNING in 1 RTT, isolate tenants inside PostgreSQL with PgBouncer-safe Row-Level Security, partition door scans by month with proven partition pruning, and stream columnar data zero-copy between PostgreSQL, Polars, and DuckDB over Apache Arrow.
This act's shifts#
| Ch. | Shift | What you'll get | Superpowers |
|---|---|---|---|
| 13 | The Nightly Feed | Stage a whole partner feed and MERGE it with full OLD/NEW reporting in one round trip. | temp=True tables pipe.stage() pg.merge() |
| 14 | Lisbon Can't See Porto | Tenant isolation enforced by PostgreSQL itself, and still one round trip behind PgBouncer. | enable_rls() create_policy() local_settings= |
| 15 | A Million Door Scans | Monthly partitions with a safety-net default, and EXPLAIN proof that old months are skipped. | PartitionBy.range() create_partition() default partition |
| 16 | The Polar Bears Arrive | PostgreSQL to Polars and back over Arrow and binary COPY, with zero Python row objects. | copy_from_arrow() on_error="ignore" pool.arrow() |
| 17 | ETL, Lakehouse & Reverse ETL | Zero-CSV ETL, Parquet archival, and Reverse ETL across PostgreSQL, Polars, and DuckDB. | __arrow_c_stream__ ETL / EtLT Reverse ETL |
The Act IV schema#
Act IV adds the monthly range-partitioned DoorEvent table (and ephemeral temp=True staging tables) from examples/schema.py:
class DoorEvent(pg.Table):
door_event_id: Col[int] = pg.pk(default=pg.identity())
scanned_at: Col[datetime] = pg.pk()
dormitory_id: Col[int] = pg.fk(Dormitory)
room_number: Col[Code]
badge_code: Col[str]
species_code: Col[str]
granted: Col[bool]
weight_kg: Col[float]
tariff_eur: Col[Money]
tags: Col[list[str]] = pg.col(default_factory=list)
@classmethod
def __constraints__(cls) -> tuple[pg.TableConstraint, ...]:
return (pg.PartitionBy.range(cls.scanned_at),)
← PreviousChapter 12 · Mapping the EmpireChapter 12 · Recursive CTEs & Set Ops
Next →Chapter 13 · The Nightly FeedChapter 13 · Staging & MERGE RETURNING