pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: benj.dev@laposte.net
Cc: pgsql-general@postgresql.org
Subject: Re: plpgsql function problem whith creating temp table - not correctly using search_path ?
Date: Tue, 11 Jan 2022 10:18:47 -0500
Message-ID: <2589876.1641914327@sss.pgh.pa.us> (raw)
In-Reply-To: <617527791.607206.1641894840385@wlpnf0208>
References: <617527791.607206.1641894840385@wlpnf0208>

benj.dev@laposte.net writes:
> -- Executing each row on differents transactions but in the same session
> /*Session A - Transaction 1*/ SELECT * FROM test_search_path_v1(true);  -- => OK takes table from pg_temp (no existing table in public)
> /*Session A - Transaction 2*/ SELECT * FROM test_search_path_v1(false); -- => OK takes table from public
> /*Session A - Transaction 3*/ SELECT * FROM test_search_path_v1(true);  -- => OK takes table from pg_temp (and the existing from public)
> /*Session A - Transaction 4*/ SELECT * FROM test_search_path_v1(false); -- => OK takes table from public
> /*Session A - Transaction 5*/ SELECT * FROM test_search_path_v1(true);  -- => NOK => it takes public and not pg_temp

I think what you've done here is maneuver the plpgsql function into a
state where it has a cached query plan [1] using the public table.
Dropping the public table, or changing your search_path, would
invalidate that cached plan ... but creating a new temp table does not.
(This isn't specific to temp tables --- in any situation where you have
more than one schema in the search path, creation of a new object could
mask objects later in the path, and we won't invalidate plans just
because that possibly happened.)

My advice is "don't do that", ie, avoid masking permanent objects
with temporary ones.  It's extremely confusing to humans as well
as machines.  If you must do it, DISCARD PLANS might help you
with keeping plpgsql functions in line.

			regards, tom lane

[1] https://www.postgresql.org/docs/current/plpgsql-implementation.html#PLPGSQL-PLAN-CACHING





view thread (6+ messages)  latest in thread

Message-ID: <2589876.1641914327@sss.pgh.pa.us>
Permalink:  ../2589876.1641914327@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/2589876.1641914327@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-general@postgresql.org
  Cc: tgl@sss.pgh.pa.us, benj.dev@laposte.net
  Subject: Re: plpgsql function problem whith creating temp table - not correctly using search_path ?
  In-Reply-To: <2589876.1641914327@sss.pgh.pa.us>
  References: <617527791.607206.1641894840385@wlpnf0208> <2589876.1641914327@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