Null commitTS bug - Mailing list pgsql-hackers

From Kingsborough, Alex
Subject Null commitTS bug
Date
Msg-id 73A66172-4050-4F2A-B7F1-13508EDA2144@amazon.com
Whole thread Raw
Responses Re: Null commitTS bug
List 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

pgsql-hackers by date:

Previous
From: Tom Lane
Date:
Subject: Re: tab completion of enum values is broken
Next
From: Tom Lane
Date:
Subject: Re: Why is src/test/modules/committs/t/002_standby.pl flaky?