Alex Rivera | Logout

Is INSERT RETURNING guaranteed to return things in the "right" order?

Asked 2011-03-25T23:52:32.817
37

Example:

create table foo(
    id serial, 
    txt text
);

insert into foo(txt) values ('a'),('b'),('c') returning id;

Returns:

 id 
----
  1
  2
  3
(3 rows)

It seems that the first id in the return value will always be the id for 'a', the second for 'b' and so on, but is this defined behaviour of insert into, or is it a coincidence that may fail under odd circumstances?

Edit
Report

1 Answer

7

While this won't help you now, 9.1 will include "writeable common table expressions". That's the official name for the WITH syntax. (Wikipedia.)

This new ability should let you place your INSERT ... RETURNING inside a WITH, give an alias, and then SELECT against that with a specific ordering with a plain old ORDER BY clause.

answered 2011-03-26T05:17:01.363

Your Answer