Fix portability issues with parsing of recovery_target_xid
authorMichael Paquier <[email protected]>
Wed, 23 Dec 2020 03:51:22 +0000 (12:51 +0900)
committerMichael Paquier <[email protected]>
Wed, 23 Dec 2020 03:51:22 +0000 (12:51 +0900)
The parsing of this parameter has been using strtoul(), which is not
portable across platforms.  On most Unix platforms, unsigned long has a
size of 64 bits, while on Windows it is 32 bits.  It is common in
recovery scenarios to rely on the output of txid_current() or even the
newer pg_current_xact_id() to get a transaction ID for setting up
recovery_target_xid.  The value returned by those functions includes the
epoch in the computed result, which would cause strtoul() to fail where
unsigned long has a size of 32 bits once the epoch is incremented.

WAL records and 2PC data include only information about 32-bit XIDs and
it is not possible to have XIDs across more than one epoch, so
discarding the high bits from the transaction ID set has no impact on
recovery.  On the contrary, the use of strtoul() prevents a consistent
behavior across platforms depending on the size of unsigned long.

This commit changes the parsing of recovery_target_xid to use
pg_strtouint64() instead, available down to 9.6.  There is one TAP test
stressing recovery with recovery_target_xid, where a tweak based on
pg_reset{xlog,wal} is added to bump the XID epoch so as this change gets
tested, as per an idea from Alexander Lakhin.

Reported-by: Alexander Lakhin
Discussion: https://postgr.es/m/16780-107fd0c0385b1035@postgresql.org
Backpatch-through: 9.6

src/backend/utils/misc/guc.c
src/test/recovery/t/003_recovery_targets.pl

index dabcbb07360bfe5a7ef501356c9d6ed7245be8b4..878fcc2236585c197b433d3d4f10ad4500d6b846 100644 (file)
@@ -11938,7 +11938,7 @@ check_recovery_target_xid(char **newval, void **extra, GucSource source)
        TransactionId *myextra;
 
        errno = 0;
-       xid = (TransactionId) strtoul(*newval, NULL, 0);
+       xid = (TransactionId) pg_strtouint64(*newval, NULL, 0);
        if (errno == EINVAL || errno == ERANGE)
            return false;
 
index cc701c5539eb51c9fd608f3e8b0c36c9ad9a950c..dd8e1cc47cafa753a6b7857c87b930b112a2476a 100644 (file)
@@ -50,6 +50,10 @@ sub test_recovery_standby
 my $node_primary = get_new_node('primary');
 $node_primary->init(has_archiving => 1, allows_streaming => 1);
 
+# Bump the transaction ID epoch.  This is useful to stress the portability
+# of recovery_target_xid parsing.
+system_or_bail('pg_resetwal', '--epoch', '1', $node_primary->data_dir);
+
 # Start it
 $node_primary->start;