pgsql: Fix race in parallel hash join batch cleanup, take II. - Mailing list pgsql-committers

From Thomas Munro
Subject pgsql: Fix race in parallel hash join batch cleanup, take II.
Date
Msg-id E1peRGP-004ZcI-0I@gemulon.postgresql.org
Whole thread Raw
List pgsql-committers
Fix race in parallel hash join batch cleanup, take II.

With unlucky timing and parallel_leader_participation=off (not the
default), PHJ could attempt to access per-batch shared state just as it
was being freed.  There was code intended to prevent that by checking
for a cleared pointer, but it was racy.  Fix, by introducing an extra
barrier phase.  The new phase PHJ_BUILD_RUNNING means that it's safe to
access the per-batch state to find a batch to help with, and
PHJ_BUILD_DONE means that it is too late.  The last to detach will free
the array of per-batch state as before, but now it will also atomically
advance the phase, so that late attachers can avoid the hazard.  This
mirrors the way per-batch hash tables are freed (see phases
PHJ_BATCH_PROBING and PHJ_BATCH_DONE).

An earlier attempt to fix this (commit 3b8981b6, later reverted) missed
one special case.  When the inner side is empty (the "empty inner
optimization), the build barrier would only make it to
PHJ_BUILD_HASHING_INNER phase before workers attempted to detach from
the hashtable.  In that case, fast-forward the build barrier to
PHJ_BUILD_RUNNING before proceeding, so that our later assertions hold
and we can still negotiate who is cleaning up.

Revealed by build farm failures, where BarrierAttach() failed a sanity
check assertion, because the memory had been clobbered by dsa_free().
In non-assert builds, the result could be a segmentation fault.

Back-patch to all supported releases.

Author: Thomas Munro <thomas.munro@gmail.com>
Author: Melanie Plageman <melanieplageman@gmail.com>
Reported-by: Michael Paquier <michael@paquier.xyz>
Reported-by: David Geier <geidav.pg@gmail.com>
Tested-by: David Geier <geidav.pg@gmail.com>
Discussion: https://postgr.es/m/20200929061142.GA29096%40paquier.xyz

Branch
------
REL_15_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/c03c6e8cf6a9118a3d1219ec0cb06b439db54100

Modified Files
--------------
src/backend/executor/nodeHash.c     | 50 +++++++++++++++++++++++-----------
src/backend/executor/nodeHashjoin.c | 54 ++++++++++++++++++++++++++-----------
src/include/executor/hashjoin.h     |  3 ++-
3 files changed, 74 insertions(+), 33 deletions(-)


pgsql-committers by date:

Previous
From: Thomas Munro
Date:
Subject: pgsql: Fix race in parallel hash join batch cleanup, take II.
Next
From: Thomas Munro
Date:
Subject: pgsql: Fix race in parallel hash join batch cleanup, take II.