pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: David G. Johnston <david.g.johnston@gmail.com>
Cc: Steve Krenzel <sgk284@gmail.com>
Cc: sravikrishna3@gmail.com <sravikrishna3@gmail.com>
Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Re: Semantics around INSERT INTO with SELECT and ORDER BY.
Date: Tue, 12 Jun 2018 10:26:59 -0400
Message-ID: <29386.1528813619@sss.pgh.pa.us> (raw)
In-Reply-To: <CAKFQuwZWFXPCN_kxhVqCL26OVkEGsrzCeM_NukxgfEZik55g7A@mail.gmail.com>
References: <CADBwkHv1nUx5bqxqRLp26k8iUWSnY3uW-1_0EMYxKns0t2JnUA@mail.gmail.com>
	<CACER=P2Qp5xKJK+3+EiEAn6cruTtPimTeedWLqL==hw1KTeEmQ@mail.gmail.com>
	<CADBwkHvdAZUgU+VgvqyE_BNyJ047wbDQSOcEsyOn-5RRdpTxgQ@mail.gmail.com>
	<CAKFQuwZWFXPCN_kxhVqCL26OVkEGsrzCeM_NukxgfEZik55g7A@mail.gmail.com>

"David G. Johnston" <david.g.johnston@gmail.com> writes:
> On Tuesday, June 12, 2018, Steve Krenzel <sgk284@gmail.com> wrote:
>> This is relevant for tables that have a column with a SERIAL type, I need
>> to guarantee that the relative ordering remains the same as the ordering of
>> the selected result set.

> The logical insertion order, and thus the sequence values, will be assigned
> according to the order by.

What is actually going to happen, given say

create table targ (d text, id serial);

insert into targ select x from src order by y;

is that you're going to get a parse tree equivalent to

	select x, nextval('targ_id_seq')
	from (select x from src order by y) ss;

and then it's a question of whether the planner is capable of reordering
the steps into something you don't want.  I think that the presence of
the explicit "ORDER BY" in the sub-select will prevent flattening of the
sub-select, which is enough to make it safe.  However, if for some reason
you did not say "ORDER BY" but nonetheless expected the serial values to
get assigned in the same order that the underlying query would produce
rows natively, you might get burnt.

As of 9.6, there are more guarantees in this area than there used
to be (cf commit 9118d03a8), but I don't think it matters as long
as you write an ORDER BY.

			regards, tom lane




view thread (8+ messages)  latest in thread

Message-ID: <29386.1528813619@sss.pgh.pa.us>
Permalink:  ../29386.1528813619@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/29386.1528813619@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 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: tgl@sss.pgh.pa.us, david.g.johnston@gmail.com, sgk284@gmail.com, sravikrishna3@gmail.com
  Subject: Re: Semantics around INSERT INTO with SELECT and ORDER BY.
  In-Reply-To: <29386.1528813619@sss.pgh.pa.us>
  References: <CADBwkHv1nUx5bqxqRLp26k8iUWSnY3uW-1_0EMYxKns0t2JnUA@mail.gmail.com> <CACER=P2Qp5xKJK+3+EiEAn6cruTtPimTeedWLqL==hw1KTeEmQ@mail.gmail.com> <CADBwkHvdAZUgU+VgvqyE_BNyJ047wbDQSOcEsyOn-5RRdpTxgQ@mail.gmail.com> <CAKFQuwZWFXPCN_kxhVqCL26OVkEGsrzCeM_NukxgfEZik55g7A@mail.gmail.com> <29386.1528813619@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