简体   繁体   中英

Ordering guarantees on UNNEST

I am a bit confused around the ordering guarantees of UNNEST(some_array)) . I have read different posts on SO where it is mentioned that Postgres does not guarantee that the order of the input and output will be the same.

For example, on this query select unnest(array[1, 2, 3]), unnest(array[4, 5, 6]); The output is:

orestis=# select unnest(array[1, 2, 3]), unnest(array[4, 5, 6]);
 unnest | unnest
--------+--------
      1 |      4
      2 |      5
      3 |      6
(3 rows)

Is this, also, a possible outcome?:

orestis=# select unnest(array[1, 2, 3]), unnest(array[4, 5, 6]);
 unnest | unnest
--------+--------
      1 |      5
      2 |      4
      3 |      6
(3 rows)

Interesting. The documentation explains:

The array's elements are read out in storage order.

This would imply that the two are aligned.

I would instead recommend:

select *
from unnest(array[1, 2, 3], array[4, 5, 6]);

The multiple argument form aligns the values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM