Re: Semantics around INSERT INTO with SELECT and ORDER BY.

From: Ravi Krishna <sravikrishna3(at)gmail(dot)com>
To: Steve Krenzel <sgk284(at)gmail(dot)com>
Cc: PG mailing List <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: Semantics around INSERT INTO with SELECT and ORDER BY.
Date: 2018-06-12 08:57:34
Message-ID: CACER=P2AL6G5ZC4HQFhXBAAYXQ5-tmEdOcbcyUbUwzece9vjsA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

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

As I understand, your business requirement is to retrieve the rows from the
target
table the same way they were inserted. The one and only way to achieve it
is
to use the same ORDER by clause to SELECT from target, what it was used
to insert into target. In your case, the insert is
INSERT INTO bar (val) SELECT val FROM foo ORDER BY id DESC;
So the SELECT should also be ORDER BY ID desc

Just don't care on how it inserts and stores row internally.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Bo Peng 2018-06-12 09:09:21 Re: Add to watchdog cluster request is rejected by node
Previous Message Laurenz Albe 2018-06-12 08:39:46 Re: Bad performance with cascaded deletes