Reorder EPQ work, to fix rowmark related bugs and improve efficiency

Enterprise / PostgreSQL - Andres Freund [anarazel.de] - 9 September 2019 12:14 EDT

In ad0bda5d24ea I changed the EvalPlanQual machinery to store substitution tuples in slot, instead of using plain HeapTuples. The main motivation for that was that using HeapTuples will be inefficient for future tableams. But it turns out that that conversion was buggy for non-locking rowmarks - the wrong tuple descriptor was used to create the slot.

As a secondary issue 5db6df0c0 changed ExecLockRows() to begin EPQ earlier, to allow to fetch the locked rows directly into the EPQ slots, instead of having to copy tuples around. Unfortunately, as Tom complained, that forces some expensive initialization to happen earlier.

As a third issue, the test coverage for EPQ was clearly insufficient.

Fixing the first issue is unfortunately not trivial: Non-locked row marks were fetched at the start of EPQ, and we don't have the type information for the rowmarks available at that point. While we could change that, it's not easy. It might be worthwhile to change that at some point, but to fix this bug, it seems better to delay fetching non-locking rowmarks when they're actually needed, rather than eagerly. They're referenced at most once, and in cases where EPQ fails, might never be referenced. Fetching them when needed also increases locality a bit.

To be able to fetch rowmarks during execution, rather than initialization, we need to be able to access the active EPQState, as that contains necessary data. To do so move EPQ related data from EState to EPQState, and, only for EStates creates as part of EPQ, reference the associated EPQState from EState.

To fix the second issue, change EPQ initialization to allow use of EvalPlanQualSlot() to be used before EvalPlanQualBegin() (but obviously still requiring EvalPlanQualInit() to have been done).

As these changes made struct EState harder to understand, e.g. by adding multiple EStates, significantly reorder the members, and add a lot more comments.

Also add a few more EPQ tests, including one that fails for the first issue above. More is needed.

27cc7cd2bc Reorder EPQ work, to fix rowmark related bugs and improve efficiency.
src/backend/commands/trigger.c | 3 +-
src/backend/executor/execMain.c | 424 +++++++++++++------------
src/backend/executor/execScan.c | 66 +++-
src/backend/executor/execUtils.c | 2 -
src/backend/executor/nodeIndexonlyscan.c | 24 +-
src/backend/executor/nodeIndexscan.c | 22 +-
src/backend/executor/nodeLockRows.c | 14 +-
src/backend/executor/nodeModifyTable.c | 11 +-
src/include/executor/executor.h | 10 +-
src/include/nodes/execnodes.h | 83 ++++-
src/test/isolation/expected/eval-plan-qual.out | 273 +++++++++++++++-
src/test/isolation/specs/eval-plan-qual.spec | 46 ++-
12 files changed, 705 insertions(+), 273 deletions(-)

Upstream: git.postgresql.org


  • Share