pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Heikki Linnakangas <hlinnaka@iki.fi>
To: Alexander Lakhin <exclusion@gmail.com>
To: Andrew Bille <andrewbille@gmail.com>
To: pgsql-bugs@lists.postgresql.org
Subject: Re: BUG #18658: Assert in SerialAdd() due to race condition
Date: Fri, 18 Oct 2024 23:15:08 +0300
Message-ID: <ff9d4350-a82d-4a3f-86e5-3948175a9a86@iki.fi> (raw)
In-Reply-To: <5634e3e1-6e82-b883-ac87-52d7e926a04d@gmail.com>
References: <18658-7dab125ec688c70b@postgresql.org>
	<CAJnzarxBO18Ha==H3hX28yz2zd7cVuasXv1wxyJ7ERd9DzJf=Q@mail.gmail.com>
	<5634e3e1-6e82-b883-ac87-52d7e926a04d@gmail.com>

On 18/10/2024 11:00, Alexander Lakhin wrote:
> Hello Andrew,
> 
> 17.10.2024 08:47, Andrew Bille wrote:
>> Using a build with no asserts enabled and without that define, I can 
>> also see the following errors using the script with increased 
>> concurrency (Initially we got a report from our client, who 
>> encountered these errors in production):
>>
>> 2024-10-17 04:58:29.774 UTC|test|regression|67109975.ed069|PANIC: 
>>  could not access status of transaction 18204
>> 2024-10-17 04:58:29.774 UTC|test|regression|67109975.ed069|DETAIL: 
>>  Could not open file "pg_serial/0000": No such file or directory.
>>
>> I changed ERROR to PANIC to get a core dump.
>>
> 
> With debug logging added (look at the patch attached), I could observe
> events, leading to the error:
> 2024-10-18 07:11:29.301 UTC [2370369:55][client backend][96/762:0] LOG:  
> statement: INSERT INTO t VALUES(42);
> 2024-10-18 07:11:29.301 UTC [2370325:58][client backend][91/0:77843] 
> LOG:  !!!SerialSetActiveSerXmin| serialControl->tailXid: 77832
> 2024-10-18 07:11:29.301 UTC [2370325:59][client backend][91/0:77843] 
> STATEMENT:  INSERT INTO t VALUES(42);
> 
> 1) SerialSetActiveSerXmin(), called by some backend, sets tailXid, which
>   corresponds to page 77832 / 1024 = 76.
> 
> 2024-10-18 07:11:29.301 UTC [2370252:83][client backend][85/794:0] LOG:  
> statement: INSERT INTO t VALUES(42);
> ...
> 2024-10-18 07:11:29.309 UTC [2370375:56][client backend][83/767:0] LOG:  
> statement: INSERT INTO t VALUES(42);
> 2024-10-18 07:11:29.309 UTC [2370271:106][client backend][63/810:0] 
> LOG:  !!!SerialAdd| xid: 77824, serialControl->headPage: -1, tailXid: 
> 77832, SERIAL_ENTRIESPERPAGE: 1024, firstZeroPage: 76, targetPage: 76, 
> isNewPage: 1
> 2024-10-18 07:11:29.309 UTC [2370271:107][client backend][63/810:0] 
> STATEMENT:  INSERT INTO t VALUES(42);
> 
> 2.1) SerialAdd() called when headPage is not defined yet, for xid 77824,
>   which corresponds to page 77824 / 1024 = 76.
> 
> 2024-10-18 07:11:29.309 UTC [2370327:86][client backend][97/795:0] LOG:  
> statement: INSERT INTO t VALUES(42);
> 2024-10-18 07:11:29.309 UTC [2370379:72][client backend][87/777:0] LOG:  
> statement: INSERT INTO t VALUES(42);
> 2024-10-18 07:11:29.309 UTC [2370376:74][client backend][98/781:0] LOG:  
> statement: INSERT INTO t VALUES(42);
> 2024-10-18 07:11:29.309 UTC [2370321:74][client backend][71/789:0] LOG:  
> statement: INSERT INTO t VALUES(42);
> 2024-10-18 07:11:29.309 UTC [2370277:86][client backend][76/795:0] LOG:  
> statement: INSERT INTO t VALUES(42);
> 2024-10-18 07:11:29.309 UTC [2370325:82][client backend][91/791:0] LOG:  
> statement: INSERT INTO t VALUES(42);
> 2024-10-18 07:11:29.309 UTC [2370373:64][client backend][84/779:0] LOG:  
> statement: INSERT INTO t VALUES(42);
> 2024-10-18 07:11:29.309 UTC [2370271:108][client backend][63/810:0] 
> LOG:  !!!SerialAdd| after SimpleLruZeroPage(), firstZeroPage: 76, slotno: 0
> 2024-10-18 07:11:29.309 UTC [2370271:109][client backend][63/810:0] 
> STATEMENT:  INSERT INTO t VALUES(42);
> 
> 2.2) SerialAdd() calls SimpleLruZeroPage() to zero out page 76.
> 
> 2024-10-18 07:11:29.309 UTC [2370330:96][client backend][1/803:0] LOG:  
> statement: INSERT INTO t VALUES(42);
> 2024-10-18 07:11:29.309 UTC [2370274:95][client backend][70/805:0] LOG:  
> !!!SerialAdd| xid: 77790, serialControl->headPage: 76, firstZeroPage: 
> 77, targetPage: 75, isNewPage: 0
> 2024-10-18 07:11:29.309 UTC [2370274:96][client backend][70/805:0] 
> STATEMENT:  INSERT INTO t VALUES(42);
> 
> 3.1) SerialAdd() called for another transaction, with xid 77790, which
>   corresponds to page 77790 / 1024 = 75, i. e. the page before the page
>   just zeroed; this page considered not new.
> 
> 2024-10-18 07:11:29.309 UTC [2370274:97][client backend][70/805:0] LOG:  
> !!!SimpleLruReadPage| pageno: 75, slotno: 16, 
> shared->page_status[slotno]: 0, shared->page_number[slotno]: 0, 
> shared->page_dirty[slotno]: 0
> 2024-10-18 07:11:29.309 UTC [2370274:98][client backend][70/805:0] 
> STATEMENT:  INSERT INTO t VALUES(42);
> 
> 3.2) SerialAdd() calls SimpleLruReadPage() for page 75, the page has status
>   SLRU_PAGE_EMPTY, so SimpleLruReadPage() wants to read it from disk...
> 
> 2024-10-18 07:11:29.309 UTC [2370257:89][client backend][58/802:0] LOG:  
> statement: INSERT INTO t VALUES(42);
> 2024-10-18 07:11:29.309 UTC [2370355:71][client backend][74/780:0] LOG:  
> statement: INSERT INTO t VALUES(42);
> 2024-10-18 07:11:29.309 UTC [2370274:99][client backend][70/805:0] 
> PANIC:  could not access status of transaction 77790
> 2024-10-18 07:11:29.309 UTC [2370274:100][client backend][70/805:0] 
> DETAIL:  Could not open file "pg_serial/0002": No such file or directory.
> 2024-10-18 07:11:29.309 UTC [2370274:101][client backend][70/805:0] 
> STATEMENT:  INSERT INTO t VALUES(42);
> 
> 3.3) And fails inside SlruPhysicalReadPage(), because that page was never
>   written to disk.
> 
> (I used log_line_prefix = '%m [%p:%l][%b][%v:%x] '; decreased 80 to 50 in
> the script posted above, had to run the script several dozen times to
> catch the error.)
> 
> I also saw a failure, when SerialAdd(), which actions 2 and 3 were
> performed by one backend.


Thanks for the repro, Andrew & Alexander! I was able to reproduce this 
too. It reproduces very quickly with the script you provided, if you add 
this sleep to ReleasePredicateLocks():

@@ -3654,6 +3667,8 @@ ReleasePredicateLocks(bool isCommit, bool 
isReadOnlySafe)

  	LWLockRelease(SerializableFinishedListLock);

+	pg_usleep(1000);
+
  	if (needToClear)
  		ClearOldPredicateLocks();

I think the assertion is too strict. It is normal for tailXid to be 
invalid in this scenario. The condition is that an XID was added to the 
finished list, but the global xmin has already advanced past that XID. 
It gets cleared from the finished list by the ClearOldPredicateLocks() 
call, but another backend might call SummarizeOldestCommittedSxact() 
before that.

The attached patch fixes it.

-- 
Heikki Linnakangas
Neon (https://neon.tech)

Attachments:

0001-Fix-race-condition-in-committing-a-serializable-tran.patch (text/x-patch, 2.2K) download From cd5144999fe98def8f21fa54abf16401d53a4f02 Mon Sep 17 00:00:00 2001 From: Heikki Linnakangas <heikki.linnakangas@iki.fi> Date: Fri, 18 Oct 2024 23:13:15 +0300 Subject: [PATCH 1/1] Fix race condition in committing a serializable transaction The finished transaction list can contain XIDs that are older than the serializable global xmin. It's a short-lived state; ClearOldPredicateLocks() removes any such transactions from the list, and it's called whenever the global xmin advances. But if another backend calls SummarizeOldestCommittedSxact() in that window, it will call SerialAdd() on an XID that's older than the global xmin, or if there are no more transactions running, when global xmin is invalid. That trips the assertion in SerialAdd(). Fixes bug #18658 reported by Andrew Bille. Thanks to Alexander Lakhin for analysis. Backpatch to all versions. Discussion: https://www.postgresql.org/message-id/18658-7dab125ec688c70b%40postgresql.org --- src/backend/storage/lmgr/predicate.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/backend/storage/lmgr/predicate.c b/src/backend/storage/lmgr/predicate.c index e24a0f2fdb..2030322f95 100644 --- a/src/backend/storage/lmgr/predicate.c +++ b/src/backend/storage/lmgr/predicate.c @@ -877,12 +877,17 @@ SerialAdd(TransactionId xid, SerCommitSeqNo minConflictCommitSeqNo) LWLockAcquire(SerialControlLock, LW_EXCLUSIVE); /* - * If no serializable transactions are active, there shouldn't be anything - * to push out to the SLRU. Hitting this assert would mean there's - * something wrong with the earlier cleanup logic. + * If 'xid' is older than the global xmin (== tailXid), there's no need to + * store it, after all. This can happen if the oldest transaction holding + * back the global xmin just finished, making 'xid' uninteresting, but + * ClearOldPredicateLocks() has not yet run. */ tailXid = serialControl->tailXid; - Assert(TransactionIdIsValid(tailXid)); + if (!TransactionIdIsValid(tailXid) || TransactionIdPrecedes(xid, tailXid)) + { + LWLockRelease(SerialControlLock); + return; + } /* * If the SLRU is currently unused, zero out the whole active region from -- 2.39.5

view thread (7+ messages)  latest in thread

Message-ID: <ff9d4350-a82d-4a3f-86e5-3948175a9a86@iki.fi>
Permalink:  ../ff9d4350-a82d-4a3f-86e5-3948175a9a86@iki.fi/
Also on:    postgresql.org/message-id/ff9d4350-a82d-4a3f-86e5-3948175a9a86@iki.fi

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: hlinnaka@iki.fi, exclusion@gmail.com, andrewbille@gmail.com
  Subject: Re: BUG #18658: Assert in SerialAdd() due to race condition
  In-Reply-To: <ff9d4350-a82d-4a3f-86e5-3948175a9a86@iki.fi>
  References: <18658-7dab125ec688c70b@postgresql.org> <CAJnzarxBO18Ha==H3hX28yz2zd7cVuasXv1wxyJ7ERd9DzJf=Q@mail.gmail.com> <5634e3e1-6e82-b883-ac87-52d7e926a04d@gmail.com> <ff9d4350-a82d-4a3f-86e5-3948175a9a86@iki.fi>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox