postgresql/src/backend
Tom Lane 78433f41fa Avoid crash during EvalPlanQual recheck of an inner indexscan.
Commit 09529a70b changed nodeIndexscan.c and nodeIndexonlyscan.c to
postpone initialization of the indexscan proper until the first tuple
fetch.  It overlooked the question of mark/restore behavior, which means
that if some caller attempts to mark the scan before the first tuple fetch,
you get a null pointer dereference.

The only existing user of mark/restore is nodeMergejoin.c, which (somewhat
accidentally) will never attempt to set a mark before the first inner tuple
unless the inner child node is a Material node.  Hence the case can't arise
normally, so it seems sufficient to document the assumption at both ends.
However, during an EvalPlanQual recheck, ExecScanFetch doesn't call
IndexNext but just returns the jammed-in test tuple.  Therefore, if we're
doing a recheck in a plan tree with a mergejoin with inner indexscan,
it's possible to reach ExecIndexMarkPos with iss_ScanDesc still null,
as reported by Guo Xiang Tan in bug #15032.

Really, when there's a test tuple supplied during an EPQ recheck, touching
the index at all is the wrong thing: rather, the behavior of mark/restore
ought to amount to saving and restoring the es_epqScanDone flag.  We can
avoid finding a place to actually save the flag, for the moment, because
given the assumption that no caller will set a mark before fetching a
tuple, es_epqScanDone must always be set by the time we try to mark.
So the actual behavior change required is just to not reach the index
access if a test tuple is supplied.

The set of plan node types that need to consider this issue are those
that support EPQ test tuples (i.e., call ExecScan()) and also support
mark/restore; which is to say, IndexScan, IndexOnlyScan, and perhaps
CustomScan.  It's tempting to try to fix the problem in one place by
teaching ExecMarkPos() itself about EPQ; but ExecMarkPos supports some
plan types that aren't Scans, and also it seems risky to make assumptions
about what a CustomScan wants to do here.  Also, the most likely future
change here is to decide that we do need to support marks placed before
the first tuple, which would require additional work in IndexScan and
IndexOnlyScan in any case.  Hence, fix the EPQ issue in nodeIndexscan.c
and nodeIndexonlyscan.c, accepting the small amount of code duplicated
thereby, and leave it to CustomScan providers to fix this bug if they
have it.

Back-patch to v10 where commit 09529a70b came in.  In earlier branches,
the index_markpos() call is a waste of cycles when EPQ is active, but
no more than that, so it doesn't seem appropriate to back-patch further.

Discussion: https://postgr.es/m/20180126074932.3098.97815@wrigleys.postgresql.org
2018-01-27 13:52:24 -05:00
..
access Report an ERROR if a parallel worker fails to start properly. 2018-01-23 11:13:42 -05:00
bootstrap Phase 3 of pgindent updates. 2017-06-21 15:35:54 -04:00
catalog Fix assorted syscache lookup sloppiness in partition-related code. 2017-11-27 19:22:08 -05:00
commands Fix StoreCatalogInheritance1 to use 32bit inhseqno 2018-01-19 10:15:08 -03:00
executor Avoid crash during EvalPlanQual recheck of an inner indexscan. 2018-01-27 13:52:24 -05:00
foreign Abstract logic to allow for multiple kinds of child rels. 2017-04-03 22:41:31 -04:00
lib Phase 3 of pgindent updates. 2017-06-21 15:35:54 -04:00
libpq Accept SCRAM channel binding enabled clients 2017-12-08 10:17:46 -05:00
main Install Windows crash dump handler before all else. 2017-11-12 14:31:04 -08:00
nodes Force rescanning of parallel-aware scan nodes below a Gather[Merge]. 2017-08-30 09:29:56 -04:00
optimizer Teach reparameterize_path() to handle AppendPaths. 2018-01-23 16:50:34 -05:00
parser Fix typo 2017-12-09 14:48:08 +01:00
po Translation updates 2017-11-05 17:00:29 -05:00
port Change pg_ctl to detect server-ready by watching status in postmaster.pid. 2017-06-28 17:31:32 -04:00
postmaster Remove unnecessary include 2018-01-23 15:22:37 -03:00
regex Phase 2 of pgindent updates. 2017-06-21 15:19:25 -04:00
replication Fix race condition during replication origin drop. 2018-01-09 12:09:30 -05:00
rewrite Fix creation of resjunk tlist entries for inherited mixed UPDATE/DELETE. 2017-11-27 17:54:09 -05:00
snowball Initial pgindent run with pg_bsd_indent version 2.0. 2017-06-21 14:39:04 -04:00
statistics Support boolean columns in functional-dependency statistics. 2017-12-04 11:51:43 -05:00
storage Remove dubious micro-optimization in ckpt_buforder_comparator(). 2018-01-10 15:50:54 -05:00
tcop Prevent idle in transaction session timeout from sometimes being ignored. 2017-10-11 14:02:41 -07:00
tsearch Improve make_tsvector() to handle empty input, and simplify its callers. 2017-07-18 13:13:47 -04:00
utils Fix pruning of locked and updated tuples. 2017-12-14 18:20:48 -08:00
.gitignore Add .gitignore entries for AIX-specific intermediate build artifacts. 2015-07-08 20:44:22 -04:00
common.mk Add ICU_CFLAGS to global CPPFLAGS 2017-06-12 15:57:22 -04:00
Makefile Inject $(ICU_LIBS) regardless of platform. 2017-08-20 21:22:33 -07:00
nls.mk Translation updates 2017-09-11 12:49:35 -04:00