Null commitTS bug

From: "Kingsborough, Alex" <kingsboa(at)amazon(dot)com>
To: "pgsql-hackers(at)lists(dot)postgresql(dot)org" <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Null commitTS bug
Date: 2022-01-14 22:49:59
Message-ID: 73A66172-4050-4F2A-B7F1-13508EDA2144@amazon.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi Hackers,
I've been working with commitTS code recently and during my testing I 
found a bug when writing commit timestamps for subxids. Normally for 
sub-transaction commit timestamps in TransactionTreeSetCommitTsData(),
we iterate through the subxids until we find one that is on the next commits
page. In the code [1] this is the jth subxid. In SetXidCommitTsInPage()
where we set all the commit timestamps up to but not including the jth
timestamp. The jth timestamp then becomes the head timestamp for next
group of timestamps on the next page. However, if the jth timestamp is 
the last subxid (put another way, if the LAST subxid is the FIRST
timestamp on a new page), then the code will break on line 188 [2] and
the timestamp for the last subxid will never be written.

This can be reproduced by enabling track_commit_timestamp and running 
a simple loop that has a single sub-transaction like:

psql -t -c 'create table t (id int);'

for i in {1..500}
do
psql -t -c 'begin; insert into t select 1; savepoint a; insert into t select 2;commit'
done

Then querying for NULL commitTS in that table will return that there are 
unwritten timestamps:

postgres=# select count(*) from t where pg_xact_commit_timestamp(t.xmin) is NULL;
count

    1
(1 row)

The fix for this is very simple

/* if we wrote out all subxids, we're done. /
- if (j + 1 >= nsubxids)
+ if (j >= nsubxids)
break;
 
[1] https://github.com/postgres/postgres/blame/master/src/backend/access/transam/commit_ts.c#L178
[2] https://github.com/postgres/postgres/blame/master/src/backend/access/transam/commit_ts.c#L188

Attachment Content-Type Size
0001-commitTS-subxids-bug-fix.patch application/octet-stream 921 bytes

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2022-01-14 22:51:52 Re: Why is src/test/modules/committs/t/002_standby.pl flaky?
Previous Message Tom Lane 2022-01-14 22:48:39 Re: tab completion of enum values is broken