]> git.ipfire.org Git - thirdparty/postgresql.git/commit
Fix incorrect order of database-locking operations in InitPostgres().
authorTom Lane <tgl@sss.pgh.pa.us>
Fri, 5 Jun 2015 17:22:27 +0000 (13:22 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Fri, 5 Jun 2015 17:22:27 +0000 (13:22 -0400)
commit04358dab214c592b57d300e30e46a0b28178bd1a
treecd83472a3e6b6104856172ccf902cbb0b9debf95
parent3f7928a2890b05908e7c1ef711d48e609c86982e
Fix incorrect order of database-locking operations in InitPostgres().

We should set MyProc->databaseId after acquiring the per-database lock,
not beforehand.  The old way risked deadlock against processes trying to
copy or delete the target database, since they would first acquire the lock
and then wait for processes with matching databaseId to exit; that left a
window wherein an incoming process could set its databaseId and then block
on the lock, while the other process had the lock and waited in vain for
the incoming process to exit.

CountOtherDBBackends() would time out and fail after 5 seconds, so this
just resulted in an unexpected failure not a permanent lockup, but it's
still annoying when it happens.  A real-world example of a use-case is that
short-duration connections to a template database should not cause CREATE
DATABASE to fail.

Doing it in the other order should be fine since the contract has always
been that processes searching the ProcArray for a database ID must hold the
relevant per-database lock while searching.  Thus, this actually removes
the former race condition that required an assumption that storing to
MyProc->databaseId is atomic.

It's been like this for a long time, so back-patch to all active branches.
src/backend/utils/init/postinit.c