From: Tom Lane <tgl@sss.pgh.pa.us>
To: Sjon Hortensius <sjon@hortensius.net>
Cc: pgsql-bugs@postgresql.org
Subject: Re: INSERT INTO .. SELECT nextval() ORDER BY - returns unexpectedly ordered values
Date: Thu, 03 Sep 2015 08:27:49 -0400
Message-ID: <10914.1441283269@sss.pgh.pa.us> (raw)
In-Reply-To: <CAK_tfuaVhgA-5=VRycf-P9p_GoU1KDpPikkQeDtojXU=MpGOew@mail.gmail.com>
References: <CAK_tfuaVhgA-5=VRycf-P9p_GoU1KDpPikkQeDtojXU=MpGOew@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>
Sjon Hortensius <sjon@hortensius.net> writes:
> INSERT INTO test2 SELECT name, nextval('tmp'), id FROM test ORDER BY id ASC;
> I have worked around this by clustering the old table on the new id before
> SELECTing but this behavior doesn't seem to be documented, is this a bug?
No. You're assuming that the nextval() happens after the row ordering,
but this is not necessarily so --- indeed, a strict reading of the SQL
standard would imply that it should *never* be so, because the standard
execution model is that ORDER BY happens after computing the SELECT list.
(Without that, locutions like "ORDER BY 1" would make no sense.) It might
accidentally work if the ORDER BY were done via an indexscan rather than
an explicit sort step, but that's not possible in your test case.
Try something like this to force the evaluation order:
INSERT INTO test2
SELECT name, nextval('tmp'), id FROM
(SELECT name, id FROM test ORDER BY id ASC) ss;
regards, tom lane
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
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-bugs@postgresql.org
Cc: tgl@sss.pgh.pa.us, sjon@hortensius.net
Subject: Re: INSERT INTO .. SELECT nextval() ORDER BY - returns unexpectedly ordered values
In-Reply-To: <10914.1441283269@sss.pgh.pa.us>
References: <CAK_tfuaVhgA-5=VRycf-P9p_GoU1KDpPikkQeDtojXU=MpGOew@mail.gmail.com> <10914.1441283269@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