pgsql: Save/restore SPI's global variables in SPI_connect() and SPI_fin

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Save/restore SPI's global variables in SPI_connect() and SPI_fin
Date: 2018-09-08 00:10:20
Message-ID: E1fyQpQ-0007UY-8x@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

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

Branch
------
REL_11_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/825f10fbda7a5d8a48d187b8193160e5e44e4011

Modified Files
--------------
src/backend/executor/spi.c | 42 ++++++++++++++++++++++++++++++++---------
src/include/executor/spi_priv.h | 6 ++++++
2 files changed, 39 insertions(+), 9 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Michael Paquier 2018-09-08 19:24:59 pgsql: Remove duplicated words split across lines in comments
Previous Message Tom Lane 2018-09-07 22:14:54 pgsql: Limit depth of forced recursion for CLOBBER_CACHE_RECURSIVELY.