From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Steve Krenzel <sgk284@gmail.com>
To: sravikrishna3@gmail.com
Cc: pgsql-general@lists.postgresql.org
Subject: Re: Semantics around INSERT INTO with SELECT and ORDER BY.
Date: Tue, 12 Jun 2018 06:25:57 -0700
Message-ID: <dfcb047d-78ff-2a61-42b5-97975dd0c693@aklaver.com> (raw)
In-Reply-To: <CADBwkHvdAZUgU+VgvqyE_BNyJ047wbDQSOcEsyOn-5RRdpTxgQ@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>
On 06/12/2018 01:39 AM, Steve Krenzel 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.
>
> More concretely, given:
>
> > CREATE TABLE foo (id SERIAL, val TEXT);
> > CREATE TABLE bar (id SERIAL, val TEXT);
> > INSERT INTO foo (val) VALUES ('A'), ('B'), ('C');
> > TABLE foo;
> id | val
> ----+-----
> 1 | A
> 2 | B
> 3 | C
> (3 rows)
>
> Then,
>
> > INSERT INTO bar (val) SELECT val FROM foo ORDER BY id DESC;
> > TABLE bar;
> id | val
> ----+-----
> 1 | C
> 2 | B
> 3 | A
> (3 rows)
>
> The rows should be inserted in reverse. (Note: I don't care about the
> actual value of the id, only the relative ordering).
>
> Inserting more values should similarly append into the table in order
> (where "append" is used in terms of the serial id).
>
> > INSERT INTO bar (val) SELECT val FROM foo ORDER BY id DESC;
> > TABLE bar;
> id | val
> ----+-----
> 1 | C
> 2 | B
> 3 | A
> 4 | C
> 5 | B
> 6 | A
> (6 rows)
>
> Or to put it another way, I want to select values from one table ordered
> by complex criteria and insert them into another table. I want to be
> able to retrieve the rows from the target table in the same order they
> were inserted, but I don't care about the specific ordering criteria. I
> only care about the order they were inserted.
That will only work until some other INSERT or UPDATE occurs. Using
table from your example:
UPDATE bar SET val = 'C1' where id = 1;
TABLE bar;
id | val
----+-----
2 | B
3 | A
4 | C
5 | B
6 | A
1 | C1
(6 rows)
You can use CLUSTER:
https://www.postgresql.org/docs/10/static/sql-cluster.html
to reestablish order based on an index, though that has the same issue:
"Clustering is a one-time operation: when the table is subsequently
updated, the changes are not clustered."
As has been pointed out order of rows is not guaranteed.
>
> On Tue, Jun 12, 2018 at 1:24 AM Ravi Krishna <sravikrishna3@gmail.com
> <mailto:sravikrishna3@gmail.com>> wrote:
>
> Why is it even important? Once you use ORDER BY clause, you are
> guaranteed to get the rows in the order. Why do you need how it was
> inserted in the first place.
>
--
Adrian Klaver
adrian.klaver@aklaver.com
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: adrian.klaver@aklaver.com, sgk284@gmail.com, sravikrishna3@gmail.com
Subject: Re: Semantics around INSERT INTO with SELECT and ORDER BY.
In-Reply-To: <dfcb047d-78ff-2a61-42b5-97975dd0c693@aklaver.com>
References: <CADBwkHv1nUx5bqxqRLp26k8iUWSnY3uW-1_0EMYxKns0t2JnUA@mail.gmail.com> <CACER=P2Qp5xKJK+3+EiEAn6cruTtPimTeedWLqL==hw1KTeEmQ@mail.gmail.com> <CADBwkHvdAZUgU+VgvqyE_BNyJ047wbDQSOcEsyOn-5RRdpTxgQ@mail.gmail.com> <dfcb047d-78ff-2a61-42b5-97975dd0c693@aklaver.com>
* 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