Goal: measure a table rewrite and observe why its lock blocks readers. Setup created bloat_demo, inserted 100,000 rows and deleted 80,000 with automatic maintenance disabled only for this fixture. At the shell, save the table's exact total bytes in a baseline table and print them; pg_size_pretty is a rounded presentation rather than the comparison value.
Run VACUUM FULL and compare current total bytes with the saved baseline. Expect a smaller relation and 20,000 surviving rows. It rewrites the table and rebuilds indexes, needs temporary disk space, and holds ACCESS EXCLUSIVE throughout; do not treat this as routine online maintenance.
Start a controlled lock holder and a separate reader. BEGIN keeps ACCESS EXCLUSIVE until COMMIT; & backgrounds each SQL client and log redirection captures its result. The helper waits until the holder has its lock before launching the reader. This demonstrates the same lock strength as VACUUM FULL, rather than timing the short rewrite itself. The holder has a one-hour safety bound; release it explicitly in step 5, or reset the lab to discard both sessions. The provided for loop retries a read-only check; sleep spaces attempts, and break ends the loop once the expected result is observed. It does not perform the change being checked. Keep the provided brace block together: { ... } executes its shell actions as one group, with a single final prompt. A > continuation prompt means the shell is still reading the block; completion requires the final normal shell prompt.
Inspect the labelled reader while it is waiting and identify its blocking PID. wait_event_type = Lock and wait_event = relation show why it cannot read; a nonempty pg_blocking_pids array ties this wait to the lock holder. Release the holder only after observing this evidence.
Terminate only the controlled lock-holder backend (rolling back its open transaction) and read the reader's log once it completes. Expect 20,000, proving the query was delayed rather than lost. The read-only query must not be cancelled as part of cleanup. The provided for loop retries a read-only check; sleep spaces attempts, and break ends the loop once the expected result is observed. It does not perform the change being checked.
Run CLUSTER bloat_demo USING idx_bloat_id to rewrite rows in this index order. Verify physical ID order with ctid, a row's temporary block/offset location. This fixture already started in ID order; the check verifies the resulting order, not a measured improvement. Later writes do not maintain it, and normal result ordering still requires ORDER BY.
Check whether pg_repack and pg_squeeze extensions are available; expect no rows in this image. These are reference-only alternatives. Online rewrite tools can reduce the long exclusive-lock interval but still need locks at particular phases and have prerequisites; they do not avoid every lock.