]> granicus.if.org Git - postgresql/commit
Fix race between GetNewTransactionId and GetOldestActiveTransactionId.
authorHeikki Linnakangas <heikki.linnakangas@iki.fi>
Thu, 13 Jul 2017 12:47:02 +0000 (15:47 +0300)
committerHeikki Linnakangas <heikki.linnakangas@iki.fi>
Thu, 13 Jul 2017 12:48:30 +0000 (15:48 +0300)
commit5c5af32d1870ef198c04955e71a843f0ccab6df6
tree9323ebd0de09588bbc1194519a49fe5881c8b6e8
parentaea1a3f0ebfcbe17f3ce89cf4cf9bd20828c199e
Fix race between GetNewTransactionId and GetOldestActiveTransactionId.

The race condition goes like this:

1. GetNewTransactionId advances nextXid e.g. from 100 to 101
2. GetOldestActiveTransactionId reads the new nextXid, 101
3. GetOldestActiveTransactionId loops through the proc array. There are no
   active XIDs there, so it returns 101 as the oldest active XID.
4. GetNewTransactionid stores XID 100 to MyPgXact->xid

So, GetOldestActiveTransactionId returned XID 101, even though 100 only
just started and is surely still running.

This would be hard to hit in practice, and even harder to spot any ill
effect if it happens. GetOldestActiveTransactionId is only used when
creating a checkpoint in a master server, and the race condition can only
happen on an online checkpoint, as there are no backends running during a
shutdown checkpoint. The oldestActiveXid value of an online checkpoint is
only used when starting up a hot standby server, to determine the starting
point where pg_subtrans is initialized from. For the race condition to
happen, there must be no other XIDs in the proc array that would hold back
the oldest-active XID value, which means that the missed XID must be a top
transaction's XID. However, pg_subtrans is not used for top XIDs, so I
believe an off-by-one error is in fact inconsequential. Nevertheless, let's
fix it, as it's clearly wrong and the fix is simple.

This has been wrong ever since hot standby was introduced, so backport to
all supported versions.

Discussion: https://www.postgresql.org/message-id/e7258662-82b6-7a45-56d4-99b337a32bf7@iki.fi
src/backend/storage/ipc/procarray.c