From ae5aa26dc39d7729b044013e8b213e79e24e613b Mon Sep 17 00:00:00 2001 From: Andrew Gierth Date: Tue, 24 Nov 2020 20:58:32 +0000 Subject: [PATCH] Properly check index mark/restore in ExecSupportsMarkRestore. Previously this code assumed that all IndexScan nodes supported mark/restore, which is not true since it depends on optional index AM support functions. This could lead to errors about missing support functions in rare edge cases of mergejoins with no sort keys, where an unordered non-btree index scan was placed on the inner path without a protecting Materialize node. (Normally, the fact that merge join requires ordered input would avoid this error.) Backpatch all the way since this bug is ancient. Per report from Eugen Konkov on irc. Discussion: https://postgr.es/m/87o8jn50be.fsf@news-spur.riddles.org.uk --- src/backend/executor/execAmi.c | 5 +++++ src/backend/optimizer/util/plancat.c | 2 ++ src/include/nodes/pathnodes.h | 1 + 3 files changed, 8 insertions(+) diff --git a/src/backend/executor/execAmi.c b/src/backend/executor/execAmi.c index 1f18e5d3a2f..4f5fdb0d427 100644 --- a/src/backend/executor/execAmi.c +++ b/src/backend/executor/execAmi.c @@ -414,6 +414,11 @@ ExecSupportsMarkRestore(Path *pathnode) { case T_IndexScan: case T_IndexOnlyScan: + /* + * Not all index types support mark/restore. + */ + return castNode(IndexPath, pathnode)->indexinfo->amcanmarkpos; + case T_Material: case T_Sort: return true; diff --git a/src/backend/optimizer/util/plancat.c b/src/backend/optimizer/util/plancat.c index 40f497660d1..bdb2f164295 100644 --- a/src/backend/optimizer/util/plancat.c +++ b/src/backend/optimizer/util/plancat.c @@ -273,6 +273,8 @@ get_relation_info(PlannerInfo *root, Oid relationObjectId, bool inhparent, info->amhasgettuple = (amroutine->amgettuple != NULL); info->amhasgetbitmap = amroutine->amgetbitmap != NULL && relation->rd_tableam->scan_bitmap_next_block != NULL; + info->amcanmarkpos = (amroutine->ammarkpos != NULL && + amroutine->amrestrpos != NULL); info->amcostestimate = amroutine->amcostestimate; Assert(info->amcostestimate != NULL); diff --git a/src/include/nodes/pathnodes.h b/src/include/nodes/pathnodes.h index 441e64eca9a..5de52edda4a 100644 --- a/src/include/nodes/pathnodes.h +++ b/src/include/nodes/pathnodes.h @@ -830,6 +830,7 @@ struct IndexOptInfo bool amhasgettuple; /* does AM have amgettuple interface? */ bool amhasgetbitmap; /* does AM have amgetbitmap interface? */ bool amcanparallel; /* does AM support parallel scan? */ + bool amcanmarkpos; /* does AM support mark/restore? */ /* Rather than include amapi.h here, we declare amcostestimate like this */ void (*amcostestimate) (); /* AM's cost estimator */ };