Re: *_to_xml() should copy SPI_processed/SPI_tuptable

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Chapman Flack <chap(at)anastigmatix(dot)net>
Cc: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: *_to_xml() should copy SPI_processed/SPI_tuptable
Date: 2018-09-06 16:25:34
Message-ID: 12766.1536251134@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

I wrote:
> Chapman Flack <chap(at)anastigmatix(dot)net> writes:
>> Another alternative might be to have SPI_connect save them and
>> SPI_finish put them back, which leaves you just responsible for
>> reasoning about your own code. You'd still be expected to save them
>> across your own uses of other SPI calls, but no longer exposed to
>> spooky action at a distance from nested uses of SPI in stuff you call.

> Hmm. I'd thought about that briefly and concluded that it didn't offer
> a full fix, but on reflection it's not clear why it'd be any less of
> a fix than the macroized-SPI_tuptable approach. You end up with
> per-SPI-stack-level storage either way, and while that isn't perfect
> it does go a long way, as you say. More, this has the huge advantage
> of being back-patchable, because there'd be no API/ABI change.

Here's a proposed patch along that line. I included SPI_result (SPI's
equivalent of errno) in the set of saved-and-restored variables,
so that all the exposed SPI globals behave the same.

Also, in further service of providing insulation between SPI nesting
levels, I thought it'd be a good idea for SPI_connect() to reset the
globals to zero/NULL after saving them. This ensures that a nesting
level can't accidentally be affected by the state of an outer level.
It's barely possible that there's somebody out there who's *intentionally*
accessing state from a calling SPI level, but that seems like it'd be
pretty dangerous programming practice. Still, maybe there's an argument
for omitting that hunk in released branches.

Comments?

regards, tom lane

Attachment Content-Type Size
save-restore-SPI-global-variables-1.patch text/x-diff 3.6 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message James Coleman 2018-09-06 16:51:53 Re: [PATCH] Incremental sort (was: PoC: Partial sort)
Previous Message Michael Paquier 2018-09-06 16:20:15 Re: test_pg_dump missing cleanup actions