pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: PostgreSQL Hackers <pgsql-hackers@postgresql.org>
Subject: Re: Let's get rid of SPI_push/SPI_pop
Date: Mon, 07 Nov 2016 15:53:42 -0500
Message-ID: <9633.1478552022@sss.pgh.pa.us> (raw)
In-Reply-To: <16696.1478530048@sss.pgh.pa.us>
References: <20808.1478481403@sss.pgh.pa.us>
	<CAFj8pRBXFKsM9z795KTgepLs-7F8+0zMw2-ZUVxh9Zd8=vdf7g@mail.gmail.com>
	<16696.1478530048@sss.pgh.pa.us>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-hackers>

I wrote:
> Pavel Stehule <pavel.stehule@gmail.com> writes:
>> 2016-11-07 2:16 GMT+01:00 Tom Lane <tgl@sss.pgh.pa.us>:
>>> So I think we should just delete these functions and adjust SPI_connect
>>> and SPI_finish so that they just push/pop a context level unconditionally.
>>> (Which will make them simpler, not more complicated.)

>> cannot be there some performance impacts?

> Any impact will be for the better, since we're removing logic.
> I haven't gone through it in detail, but it might be as simple
> as removing _SPI_curid and code that manipulates it.

After studying spi.c more closely, I find that it can't be quite as
simple as that.  There are five functions that look at _SPI_curid
to determine where they will palloc their results:
	SPI_copytuple
	SPI_returntuple
	SPI_modifytuple
	SPI_palloc
	SPI_datumTransfer
Specifically, when used in a normal SPI execution context, they
will palloc in the "savedcxt", the one that had been current when
SPI_connect was called; this behavior is used for returning values
out of a SPI-using function.  However, when not in a SPI context,
they just allocate in CurrentMemoryContext.  So their behavior is
different after SPI_push than before it.

The majority of call sites use these functions while connected to SPI,
so that it wouldn't change things if we redefined these functions
to insist on being in SPI context and always use the "savedcxt".
However, there are about half a dozen places that use SPI_modifytuple
while not connected.  Some of them never do connect at all, and
some use it after disconnecting.

I don't think that this discovery constitutes a reason not to get rid of
SPI_push/SPI_pop.  What it shows is that forgetting to call them actually
has another, subtler way of causing bugs.  If you forget, and call some
code that is using one of these functions this way and expecting to
produce a result in CurrentMemoryContext, it won't produce the result
there but in the context of the caller of your SPI-using procedure.
That probably means the memory will get leaked until you return, which
could be quite a long time (consider a long-running plpgsql function
that repeatedly calls something that acts like this).

So I think that we should still get rid of SPI_push/SPI_pop, and to that
end, redefine these five functions to throw error outside SPI context.

To simplify adapting the places that use SPI_modifytuple outside SPI
context, we could provide a function with a similar API that allocates
in CurrentMemoryContext.  I have not found any caller that really needs
a run-time determination of which memory context to use.

			regards, tom lane


-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers



view thread (5+ messages)

Message-ID: <9633.1478552022@sss.pgh.pa.us>
Permalink:  ../9633.1478552022@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/9633.1478552022@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-hackers@postgresql.org
  Cc: tgl@sss.pgh.pa.us
  Subject: Re: Let's get rid of SPI_push/SPI_pop
  In-Reply-To: <9633.1478552022@sss.pgh.pa.us>
  References: <20808.1478481403@sss.pgh.pa.us> <CAFj8pRBXFKsM9z795KTgepLs-7F8+0zMw2-ZUVxh9Zd8=vdf7g@mail.gmail.com> <16696.1478530048@sss.pgh.pa.us> <9633.1478552022@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox