Goal: Verify schema changes without a rewrite, then prove that the exclusive lock can block a read until its transaction ends.
Before altering any tables, connect to beer_db. Run psql -U beer_db -d beer_db to open a psql session.
Before changing the table, save its storage identifier with the supplied SELECT and \gset. pg_relation_filenode identifies the relation storage, AS before_node names the value, and \gset stores it as a psql variable for this session. Add nullable style_notes text, then check all 400 existing rows have NULL notes. COUNT(*) is a provided aggregation preview; IS NULL tests missing values. Expect ALTER TABLE and a count of 400. This avoids a rewrite but still needs an ACCESS EXCLUSIVE lock.
Run \d beers and confirm style_notes appears in the column list.
Add is_seasonal boolean NOT NULL DEFAULT false. PostgreSQL has supported the fast path for non-volatile defaults since version 11. Run the provided storage comparison: :'before_node' expands the psql variable saved earlier, and ::oid casts it to the identifier type. Expect same_storage = t and 400 false seasonal flags. Together with the catalog probe, this demonstrates the fast-default path on this image; it does not measure production lock wait time.
Rename style_notes to brewing_notes, then inspect \d beers. Expect the new name and no old name; the column remains nullable text. Renaming changes metadata without rewriting rows, but can still wait for a conflicting lock.
Simulate the lock used by these ALTER TABLE operations with a second client. \! runs a shell command while the main psql session stays open. The background client sets application_name=schema_lab_lock so we can identify it, begins a transaction, acquires ACCESS EXCLUSIVE on beers, and sleeps for ten minutes. & backgrounds that client; its output is redirected to a log to avoid mixing two clients in your terminal. The next step verifies the lock is actually held. This is an explicit lock demonstration, not a timed table rewrite. Later you will stop this dedicated session rather than waiting ten minutes.
Run the provided query joining lock records to client sessions by process ID (pid). The WHERE filter identifies only our dedicated background client and beers relation. Expect beers | AccessExclusiveLock | t: t means the lock is granted. This lock conflicts even with a read. If the client has not acquired it yet, retry this read-only inspection.
Set statement_timeout to two seconds in your foreground session, then count beers. This setting limits how long the statement may run, including lock waits. Expect canceling statement due to statement timeout while the inspected exclusive lock remains held; no count result arrives. RESET restores the normal timeout. This makes the blocked read observable without manual Ctrl+C. The two-second limit is a guard, not a speed benchmark.
Release the lock by terminating only the session tagged schema_lab_lock. pg_terminate_backend(pid) stops that client and rolls back its open transaction; the beer_db role may stop its own session. Expect t. Re-run the same lock inspection and expect zero rows, then count beers again and expect 400. The read now succeeds because the conflicting transaction ended. The background log may contain a termination error; that is expected for the stopped client.