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

From: Terje Elde <terje(at)elde(dot)net>
To: Sjon Hortensius <sjon(at)hortensius(dot)net>
Cc: "pgsql-bugs(at)postgresql(dot)org" <pgsql-bugs(at)postgresql(dot)org>
Subject: Re: INSERT INTO .. SELECT nextval() ORDER BY - returns unexpectedly ordered values
Date: 2015-09-03 15:55:00
Message-ID: B34674E4-7C86-440E-8CEF-21A929571973@elde.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On 3. sep. 2015, at 14:00, Sjon Hortensius <sjon(at)hortensius(dot)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 order based on things computed in the select.

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
x;

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

Or simply put; not a bug. :-)

Terje

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Sjon Hortensius 2015-09-03 16:01:37 Re: INSERT INTO .. SELECT nextval() ORDER BY - returns unexpectedly ordered values
Previous Message Michael Paquier 2015-09-03 12:28:51 Re: BUG #13594: pg_ctl.exe redirects stderr to Windows Events Log if stderr is redirected to pipe