Re: INSERT INTO .. SELECT nextval() ORDER BY - returns unexpectedly ordered values - Mailing list pgsql-bugs

From Terje Elde
Subject Re: INSERT INTO .. SELECT nextval() ORDER BY - returns unexpectedly ordered values
Date
Msg-id B34674E4-7C86-440E-8CEF-21A929571973@elde.net
Whole thread Raw
In response to INSERT INTO .. SELECT nextval() ORDER BY - returns unexpectedly ordered values  (Sjon Hortensius <sjon@hortensius.net>)
List pgsql-bugs
On 3. sep. 2015, at 14:00, Sjon Hortensius <sjon@hortensius.net> wrote:

> 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?

Your nextval is run before the ordering. Otherwise, you wouldn't be able to o=
rder based on things computed in the select.=20

You can stack it though, such as:

INSERT INTO <...>
SELECT name, nextval(sequence), old_id FROM
( SELECT name, old_id FROM test ORDER BY old ASC ) as=20
 x;

That way, you sort before pulling a new value with nextval, giving you the r=
esult you want. Did that make sense?

Or simply put; not a bug. :-)

Terje=

pgsql-bugs by date:

Previous
From: Sjon Hortensius
Date:
Subject: INSERT INTO .. SELECT nextval() ORDER BY - returns unexpectedly ordered values
Next
From: Sjon Hortensius
Date:
Subject: Re: INSERT INTO .. SELECT nextval() ORDER BY - returns unexpectedly ordered values