pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Wolfgang Walther <walther@technowledgy.de>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: Execution order of CTEs / set_config and current_setting in the same query
Date: Wed, 9 Dec 2020 17:37:47 +0100
Message-ID: <b8c22ed1-ac2f-461e-3f7c-c2665085c3b1@technowledgy.de> (raw)
In-Reply-To: <3578729.1607527086@sss.pgh.pa.us>
References: <cf303847-0bd1-4eca-2b3c-3055416df8bb@technowledgy.de>
	<3578729.1607527086@sss.pgh.pa.us>

Tom Lane:
> I think you're on fairly shaky ground here.  Generally speaking, a CTE
> will be executed/read only when the parent query needs the next row from
> it.  Your examples ensure that the CTE is read before the parent query's
> results are computed; but in realistic usage you'd presumably be joining
> the CTE with some other table(s), and then the execution order is going
> to be a lot harder to predict.  This approach is also going to
> fundamentally not work for settings that need to apply during planning
> of the query (which, notably, probably includes "role").

Ok, thanks for confirming that.

> You'd be far better off to investigate ways to send SET LOCAL first,
> without incurring a separate network round trip for that.  If you're
> using simple query mode that's easy, you can just do
> 
> 	res = PQexec("SET LOCAL ... ; <real query>");
> 
> In extended query mode you can't get away with that, but you might be able
> to issue the SET LOCAL without immediately waiting for the result.

Yes, that's what we did so far. We switched to set_config to parametrize 
the query.

Is there any way to not wait for a SELECT? I don't care about the 
resultset, so I need something like PERFORM but for SQL, not plpgsql, I 
think?





view thread (3+ messages)

Message-ID: <b8c22ed1-ac2f-461e-3f7c-c2665085c3b1@technowledgy.de>
Permalink:  ../b8c22ed1-ac2f-461e-3f7c-c2665085c3b1@technowledgy.de/
Also on:    postgresql.org/message-id/b8c22ed1-ac2f-461e-3f7c-c2665085c3b1@technowledgy.de

 · 

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: walther@technowledgy.de, tgl@sss.pgh.pa.us
  Subject: Re: Execution order of CTEs / set_config and current_setting in the same query
  In-Reply-To: <b8c22ed1-ac2f-461e-3f7c-c2665085c3b1@technowledgy.de>
  References: <cf303847-0bd1-4eca-2b3c-3055416df8bb@technowledgy.de> <3578729.1607527086@sss.pgh.pa.us> <b8c22ed1-ac2f-461e-3f7c-c2665085c3b1@technowledgy.de>

* 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