{% for n in range(colcnt|int) %} {% if loop.index != 1 %} UNION SELECT pg_get_indexdef({{ cid|string }}, {{ loop.index|string }}, true) AS column, {{ loop.index|string }} AS idx {% else %} SELECT pg_get_indexdef({{ cid|string }} , {{ loop.index|string }} , true) AS column, {{ loop.index|string }} AS idx {% endif %} {% endfor %} ORDER BY idx
There are a few other templates where it looks like the same pattern happens. In a lot of them you use UNION ALL, which may coincidentally return rows the same order as the individual SELECT statements. In the one above you use UNION so hashing is used and rows are returned in a less predictable order. I think that even in the first case an ORDER BY clause is required for correctness -- or the rows need to be sorted in the client code before generating the SQL.