Databasemaxxing: I GOT A HUNDRED CONNECTIONS A HUNDRED LOCKS (Now Postgres is Kill)

This is a follow up to the MVCC article to go a little deeper on the different logical locks in Postgres and why knowing them can help discover problems that may pop up in production.
In the MVCC article (https://x.com/ptr_to_joel/status/2119051139559854274), I touched on some different timeouts you can set. Here, we'll go over the logical locks that Postgres uses for different use cases. There are other types of locks but I won't go over them here.
Note: Deadlock detection was already covered in the MVCC article so for reference on how Postgres detects and resolves a deadlock you can read there too!

the lox didn't need them in their verzuz with dipset
These locks complement MVCC by coordinating concurrent operations when transactions need to modify or otherwise protect rows. Postgres implicitly acquires the appropriate locks for operations, so there's generally no need to explicitly request them unless you have a reason to want some desired behaviour (e.g. SELECT FOR UPDATE). I feel the best way of getting familiar is take an example query and follow along with what locks it may need.
Let's start with some straightforward operations:
- SELECT -> acquires the ACCESS SHARE table lock
- INSERT | UPDATE | DELETE -> all acquire the ROW EXCLUSIVE table lock
Compatibility between table locks is crucial for MVCC, so ACCESS SHARE and ROW EXCLUSIVE don't block each other. You can think of ROW EXCLUSIVE semantically meaning "I'm gonna go mutate something on this table". Even though the name is 'ROW EXCLUSIVE', this is not an exclusive table lock.
ACCESS EXCLUSIVE is the only table-level lock that conflicts with ACCESS SHARE, meaning it's the only table-level lock that can directly prevent an ordinary SELECT from acquiring its required table lock.

Second select is blocked due to transaction performing a drop table and has not yet committed
- UPDATE -> acquires FOR NO KEY UPDATE or FOR UPDATE row lock (depending on what is getting modified)
- DELETE -> acquires FOR UPDATE row lock
Note: With UPDATEs, FOR UPDATE is acquired when modifying columns that have a qualifying unique index on them that can be used in a foreign key (currently Postgres doesn't apply this for partial/expressional indexes)

Left txn performs a FOR NO KEY UPDATE successfully whilst right txn holds the FOR KEY SHARE lock on the row

Left txn is blocked from performing its UPDATE due to requiring a FOR UPDATE lock as right txn acquired the FOR KEY SHARE lock first
INSERT is an interesting case since there's no existing row, therefore logically you'd think two transactions could submit inserts to the same key in their open transaction.. but Postgres will detect this! Unique index enforcement must account for concurrent transactions independent of MVCC visibility!

Even under REPEATABLE READ Postgres detects the double INSERT and blocks the second transaction

Once the winning transaction commits, the other will detect that a constraint was violated
Going back to the first example from the MVCC article, two transactions looking to update the same row will be competing to acquire the row lock, which is held until COMMIT, therefore allowing the following transaction to acquire the row lock and perform its UPDATE.
Note: Transactions will generally hold their locks until they've completed.. BUT if locks are acquired after a savepoint and the transaction rolled back to the savepoint, the locks are released immediately.

Left side rolled back to savepoint before UPDATE which allowed right side to acquire the lock and be unblocked

UPDATE performed before the rollback to the savepoint is still valid with the lock held meaning the right txn is still blocked
The main thing to take away here is that an application performing a transaction that acquires the ACCESS EXCLUSIVE table lock could take down production.
Scenario: application opens transaction -> acquires ACCESS EXCLUSIVE table lock -> application enters an unhandled state -> transaction is left open -> subsequent operations requiring access to the table are blocked until the transaction finishes and releases its lock

Simulated example
Remembering back from the MVCC article, a transaction that's idle with no movement can be timed out if we set idle_in_transaction_session_timeout:

App had its transaction timed out
Now our SELECT was unblocked and could progress!
It's not enough to simply expect Postgres to take care of things itself, applications can also affect the health of the database. This is also besides the fact that if the database itself came crashing down, it would also be terrible if the order of execution of transactions that were part of crash recovery weren't replayed in a deterministic manner too.
MVCC allows concurrency, locks coordinate conflicting operations, and poorly managed transactions can undermine that concurrency.