postgresql/src/include/executor
Tom Lane 361844fe56 Save/restore SPI's global variables in SPI_connect() and SPI_finish().
This patch removes two sources of interference between nominally
independent functions when one SPI-using function calls another,
perhaps without knowing that it does so.

Chapman Flack pointed out that xml.c's query_to_xml_internal() expects
SPI_tuptable and SPI_processed to stay valid across datatype output
function calls; but it's possible that such a call could involve
re-entrant use of SPI.  It seems likely that there are similar hazards
elsewhere, if not in the core code then in third-party SPI users.
Previously SPI_finish() reset SPI's API globals to zeroes/nulls, which
would typically make for a crash in such a situation.  Restoring them
to the values they had at SPI_connect() seems like a considerably more
useful behavior, and it still meets the design goal of not leaving any
dangling pointers to tuple tables of the function being exited.

Also, cause SPI_connect() to reset these variables to zeroes/nulls after
saving them.  This prevents interference in the opposite direction: it's
possible that a SPI-using function that's only ever been tested standalone
contains assumptions that these variables start out as zeroes.  That was
the case as long as you were the outermost SPI user, but not so much for
an inner user.  Now it's consistent.

Report and fix suggestion by Chapman Flack, actual patch by me.
Back-patch to all supported branches.

Discussion: https://postgr.es/m/9fa25bef-2e4f-1c32-22a4-3ad0723c4a17@anastigmatix.net
2018-09-07 20:09:57 -04:00
..
execExpr.h Post-feature-freeze pgindent run. 2018-04-26 14:47:16 -04:00
execParallel.h Update copyright for 2018 2018-01-02 23:30:12 -05:00
execPartition.h Fix run-time partition pruning for appends with multiple source rels. 2018-08-01 19:42:52 -04:00
execdebug.h Update copyright for 2018 2018-01-02 23:30:12 -05:00
execdesc.h Update copyright for 2018 2018-01-02 23:30:12 -05:00
executor.h Don't needlessly check the partition contraint twice 2018-06-11 17:12:16 -04:00
functions.h Support INOUT arguments in procedures 2018-03-14 12:07:28 -04:00
hashjoin.h Fix a boatload of typos in C comments. 2018-04-01 15:01:28 -04:00
instrument.h Post-feature-freeze pgindent run. 2018-04-26 14:47:16 -04:00
nodeAgg.h Improve spelling of new FINALFUNC_MODIFY aggregate attribute. 2018-05-21 11:41:42 -04:00
nodeAppend.h Update copyright for 2018 2018-01-02 23:30:12 -05:00
nodeBitmapAnd.h Update copyright for 2018 2018-01-02 23:30:12 -05:00
nodeBitmapHeapscan.h Update copyright for 2018 2018-01-02 23:30:12 -05:00
nodeBitmapIndexscan.h Update copyright for 2018 2018-01-02 23:30:12 -05:00
nodeBitmapOr.h Update copyright for 2018 2018-01-02 23:30:12 -05:00
nodeCtescan.h Update copyright for 2018 2018-01-02 23:30:12 -05:00
nodeCustom.h Update copyright for 2018 2018-01-02 23:30:12 -05:00
nodeForeignscan.h Update copyright for 2018 2018-01-02 23:30:12 -05:00
nodeFunctionscan.h Update copyright for 2018 2018-01-02 23:30:12 -05:00
nodeGather.h Update copyright for 2018 2018-01-02 23:30:12 -05:00
nodeGatherMerge.h Update copyright for 2018 2018-01-02 23:30:12 -05:00
nodeGroup.h Update copyright for 2018 2018-01-02 23:30:12 -05:00
nodeHash.h Update copyright for 2018 2018-01-02 23:30:12 -05:00
nodeHashjoin.h Update copyright for 2018 2018-01-02 23:30:12 -05:00
nodeIndexonlyscan.h Update copyright for 2018 2018-01-02 23:30:12 -05:00
nodeIndexscan.h Update copyright for 2018 2018-01-02 23:30:12 -05:00
nodeLimit.h Update copyright for 2018 2018-01-02 23:30:12 -05:00
nodeLockRows.h Update copyright for 2018 2018-01-02 23:30:12 -05:00
nodeMaterial.h Update copyright for 2018 2018-01-02 23:30:12 -05:00
nodeMergeAppend.h Update copyright for 2018 2018-01-02 23:30:12 -05:00
nodeMergejoin.h Update copyright for 2018 2018-01-02 23:30:12 -05:00
nodeModifyTable.h Revert MERGE patch 2018-04-12 11:22:56 +01:00
nodeNamedtuplestorescan.h Update copyright for 2018 2018-01-02 23:30:12 -05:00
nodeNestloop.h Update copyright for 2018 2018-01-02 23:30:12 -05:00
nodeProjectSet.h Update copyright for 2018 2018-01-02 23:30:12 -05:00
nodeRecursiveunion.h Update copyright for 2018 2018-01-02 23:30:12 -05:00
nodeResult.h Update copyright for 2018 2018-01-02 23:30:12 -05:00
nodeSamplescan.h Update copyright for 2018 2018-01-02 23:30:12 -05:00
nodeSeqscan.h Update copyright for 2018 2018-01-02 23:30:12 -05:00
nodeSetOp.h Update copyright for 2018 2018-01-02 23:30:12 -05:00
nodeSort.h Update copyright for 2018 2018-01-02 23:30:12 -05:00
nodeSubplan.h Update copyright for 2018 2018-01-02 23:30:12 -05:00
nodeSubqueryscan.h Update copyright for 2018 2018-01-02 23:30:12 -05:00
nodeTableFuncscan.h Update copyright for 2018 2018-01-02 23:30:12 -05:00
nodeTidscan.h Update copyright for 2018 2018-01-02 23:30:12 -05:00
nodeUnique.h Update copyright for 2018 2018-01-02 23:30:12 -05:00
nodeValuesscan.h Update copyright for 2018 2018-01-02 23:30:12 -05:00
nodeWindowAgg.h Update copyright for 2018 2018-01-02 23:30:12 -05:00
nodeWorktablescan.h Update copyright for 2018 2018-01-02 23:30:12 -05:00
spi.h Fix SPI error cleanup and memory leak 2018-05-03 08:39:15 -04:00
spi_priv.h Save/restore SPI's global variables in SPI_connect() and SPI_finish(). 2018-09-07 20:09:57 -04:00
tablefunc.h Fix a boatload of typos in C comments. 2018-04-01 15:01:28 -04:00
tqueue.h Update copyright for 2018 2018-01-02 23:30:12 -05:00
tstoreReceiver.h Update copyright for 2018 2018-01-02 23:30:12 -05:00
tuptable.h Deduplicate code between slot_getallattrs() and slot_getsomeattrs(). 2018-08-23 16:58:53 -07:00