Re: INSERT RETURNING

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: goel922(at)gmail(dot)com, Pg Docs <pgsql-docs(at)lists(dot)postgresql(dot)org>
Subject: Re: INSERT RETURNING
Date: 2022-08-28 17:16:39
Message-ID: CAKFQuwb+Ha82w+3Lfc1rJuJ7e2296iyUqf7nRE-F_ZMmvRz8pA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-docs

On Sun, Aug 28, 2022 at 7:51 AM PG Doc comments form <noreply(at)postgresql(dot)org>
wrote:

> The following documentation comment has been logged on the website:
>
> Page: https://www.postgresql.org/docs/14/dml-returning.html
> Description:
>
> If I execute the below query
> ```
> CREATE TABLE users (
> id serial primary key,
> name varchar(255)
> )
> INSERT INTO users (name) VALUES ('a'), ('b'), ('c') RETURNING id
> ```
> Is there any chance that the order of the returned ids is different from
> the
> order of data passed
> To be more clear, if the returned ids are `1, 2, 3`, is it guaranteed that
> the data order is ((1, a), (2, b), (3, c)) or can it be ((1, b), (2, a),
> (3,
> c))?
>

There is no guarantee that insertion will happen in any particular order.
If you have to associate a given generated id with a given record either
the record must have it own natural key (in which case return both the
natural and surrogate keys) or you have to insert one record at a time.

David J.

In response to

Browse pgsql-docs by date

  From Date Subject
Next Message PG Doc comments form 2022-08-28 21:33:40 [`] Paragraph doesn't match example in
Previous Message Pavel Stehule 2022-08-28 17:08:25 Re: Does postgres have Equivalent range C range Ty​pe for Built-in SQL range Types