PostgreSQL 00:00:01 Two answers to one inventory question A marina has two boats to rent. Its evening report starts a repeatable-read transaction. While the report is running, a clerk updates the available count to three. Which number should the report print? PostgreSQL keeps two row versions for this update. The report's snapshot still sees two. A new transaction can see the committed value three. Neither reader needs to overwrite the other's view of the inventory. Vacuum cannot reclaim the old version while a snapshot still needs it. When the old reader finishes and the version is eligible, vacuum can mark that space reusable. An ordinary vacuum usually keeps the space inside the table for later writes. 00:00:47 What survived the crash? Follow the clerk's update. PostgreSQL changes a data page in shared buffers and builds a write-ahead log record. The data page and the log describe the same change, but their journeys to durable storage differ. In this example synchronous commit is enabled. Before acknowledging success, PostgreSQL flushes the required log records to durable storage. Concurrent transactions may share the cost of a log flush. The updated table page can reach disk later. Cut the power after the acknowledgement but before that page write. On restart, recovery uses the checkpoint's redo location and the durable log to bring pages forward. An older on-disk page can coexist with a durable, committed change. 00:01:37 Three clients share two backends Three marina terminals keep connections open through PgBouncer. Configure transaction pooling and a pool of two database backends. A connected terminal can wait between transactions without reserving a PostgreSQL backend exclusively. Two terminals begin transactions and occupy both backends. The third begins and waits. Pooling bounds database concurrency. It moves the wait to an explicit queue, so measure wait time alongside the count of established connections. A commit releases one backend to serve the waiting terminal. The third client may get a different backend next time. Application code that assumes the same session state across transactions must account for that boundary. 00:02:27 Promote a replica with a safety condition In a separate high-availability setup, Patroni coordinates leadership through a distributed configuration store. The primary accepts the marina's writes and streams log records to a standby. Client routing follows whichever node is authorized to write. When the primary becomes unavailable, an eligible standby can acquire leadership and promote. The surrounding system must prevent the old primary from continuing to write. A lost connection alone cannot prove that another machine has stopped. With asynchronous replication, the old primary may have acknowledged a sale that had not reached the promoted standby. Ask about the permitted data-loss window as well as failover time. A quick promotion answers only the second question. 00:03:14 Pick an index for a question Now the marina wants to find a customer by email or list bookings over a range of dates. A B-tree index supports equality and range searches and can supply a compatible ordering. Choose it against a real query shape. A different query searches inside a document or a set of words. A suitable GIN index can map component values to candidate rows. Its supported operators and index definition determine which questions it can answer. For a huge time-ordered booking log, a BRIN index can summarize ranges of table pages. Correlation between timestamp and physical location lets it skip ranges. Matching ranges still need checking, so size and selectivity both matter. 00:04:00 The forgotten report On Friday evening, a report opens a repeatable-read transaction and its session goes idle. No new queries appear. Its snapshot can still retain versions while active bookings continue changing rows through the weekend. On Monday, check session activity and transaction age. Session seven is the old reader in this example. Confirm who owns its work, then end the abandoned transaction. A quiet session can impose a storage cost on somebody else's workload. Once the old snapshot is gone, vacuum can reclaim eligible dead tuples for reuse. A suitable idle-in-transaction timeout helps avoid a repeat. The marina's opening puzzle has one answer per snapshot, and preserving that promise has a measurable lifetime and cost.