agora inbox for pgsql-sql@postgresql.org  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Peter Geoghegan <pg@bowt.ie>
Cc: Andreas Joseph Krogh <andreas@visena.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: Unable to use INSERT ... RETURNING with column from other table
Date: Tue, 24 Oct 2017 23:04:44 -0400
Message-ID: <15480.1508900684@sss.pgh.pa.us> (raw)
In-Reply-To: <CAH2-Wz=egG5KCku7h4AHMYiLv_ht4b5D7o=GVaHRof54Eop+MA@mail.gmail.com>
References: <CAH2-Wzm4Qss48XbhWEDuLTcnFCi7QzKWtiNwYBk1LurcC9CdKQ@mail.gmail.com>
	<VisenaEmail.5e.f9aed09bf35804e1.15f509189ac@tc7-visena>
	<CAH2-Wz=egG5KCku7h4AHMYiLv_ht4b5D7o=GVaHRof54Eop+MA@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-sql>

Peter Geoghegan <pg@bowt.ie> writes:
> As David says, you could use multiple CTEs for this.

Yeah.  The SELECT portion of the query, so far as the outer INSERT
is concerned, is just a black box that yields some column values to
be inserted.  We could wish that the INSERT's RETURNING clause
could examine additional column values that are available inside that
subquery, but I'm afraid that there are insurmountable semantic problems.
In particular, DISTINCT seems to break that entirely --- consider

insert into foo(id, name)
  select distinct 3, f.name
  from foo f
  where ...
returning id, f.id;

We can't just add "f.id" to the set of columns returned by the SELECT
part without changing the semantics of the DISTINCT.  Or if we ignore
that (acting like it was DISTINCT ON (3, f.name)) then we get an
underdetermined value of f.id, which doesn't seem appetizing either.

			regards, tom lane


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



view thread (9+ messages)  latest in thread

Message-ID: <15480.1508900684@sss.pgh.pa.us>
Permalink:  ../15480.1508900684@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/15480.1508900684@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 using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: tgl@sss.pgh.pa.us, pg@bowt.ie, andreas@visena.com
  Subject: Re: Unable to use INSERT ... RETURNING with column from other table
  In-Reply-To: <15480.1508900684@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 agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox