Injection point: fix race condition between attach and cleanup

From: "Hayato Kuroda (Fujitsu)" <kuroda(dot)hayato(at)fujitsu(dot)com>
To: "'pgsql-hackers(at)lists(dot)postgresql(dot)org'" <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Cc: Masahiko Sawada <sawada(dot)mshk(at)gmail(dot)com>, Alexander Lakhin <exclusion(at)gmail(dot)com>
Subject: Injection point: fix race condition between attach and cleanup
Date: 2026-11-11 12:26:41
Message-ID: OS7PR01MB183175FA935E5DC8734FF2CD6F5712@OS7PR01MB18317.jpnprd01.prod.outlook.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi hackers,
(CC: Sawada-san and Alexander, who attended in the original thread)

This is a fork thread from [1]. I found a race condition between the exiting
backend and another one, while using the injection points.

Found issue
==========
Injection points attached after injection_points_set_local() are removed via
before_shmem_exit callback. This cleanup identifies an injection point only by name.
If another backend detached the point and attached a new callback with the same
name before the original backend exited, the cleanup could detach the new callback
instead.

0001 reproduces the same issue.

Proposed fix
==========
The fix idea is to remember the indicator of attached injection points by the
backend. When the backend is in the local injection point mode and points, the
slot id and its generation are stored in the local list. At the cleanup phase,
the backend iterates all listed injection points and removes it if the generation
is the same. See 0002.

Alternative fix
===========
So far, a regression test 010_index_concurrently_upsert can fail by this issue [2].
If we only focus on fixing this test, there is another approach to wait till
cleanups are done. The patch with the prefix "nocfbot" implements the same.

How do you feel? Feedbacks are very welcome.

[1]: postgresql.org/message-id/flat/OS7PR01MB183172A425382C8A781DB153FF56D2%40OS7PR01MB18317.jpnprd01.prod.outlook.com
[2]: https://www.postgresql.org/message-id/8ccd0497-c914-47e2-804d-299b678863bf%40gmail.com

Best regards,
Hayato Kuroda
FUJITSU LIMITED

Attachment Content-Type Size
nocfbot-0001-Stabilize-010_index_concurrently_upsert.patch application/octet-stream 6.1 KB
0001-injection-points-reproducer-for-the-race-condition-b.patch application/octet-stream 0 bytes
0002-injection-points-Add-handles-for-detaching-callbacks.patch application/octet-stream 0 bytes

Browse pgsql-hackers by date

  From Date Subject
Next Message Keyerror Smart 2026-11-11 12:44:32 Re: [PATCH] Consider window functions when reordering GROUP BY items
Previous Message Kirill Reshke 2026-11-11 12:22:55 Re: Fix GIN pending cleanup race