Semantics around INSERT INTO with SELECT and ORDER BY.

From: Steve Krenzel <sgk284(at)gmail(dot)com>
To: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Semantics around INSERT INTO with SELECT and ORDER BY.
Date: 2018-06-12 08:14:26
Message-ID: CADBwkHv1nUx5bqxqRLp26k8iUWSnY3uW-1_0EMYxKns0t2JnUA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

If I insert using the results of a select statement, are the inserts
guaranteed to happen in the order of the rows returned from the select?

That is, are these two equivalent:

INSERT INTO <table2> SELECT <field> FROM <table1> ORDER BY <col> DESC;

And:

FOR row IN SELECT <field> FROM <table1> ORDER BY <col> DESC LOOP
INSERT INTO <table2> VALUES (row.<field>);
END LOOP;

I read through the SQL spec on insertion but they don't address insertion
ordering (perhaps, purposefully).

Any clarification here would be super helpful.

Thank you!
Steve

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Ravi Krishna 2018-06-12 08:24:28 Re: Semantics around INSERT INTO with SELECT and ORDER BY.
Previous Message a 2018-06-12 08:02:43 Does pgAgent support chinese, japanese characters?