From: Alvaro Herrera <alvherre@alvh.no-ip.org>
To: Dominique Devienne <ddevienne@gmail.com>
Cc: pgsql-general@postgresql.org
Subject: Re: Convert pg_constraint.conkey array to same-order array of column names
Date: Fri, 24 Mar 2023 11:51:19 +0100
Message-ID: <20230324105119.43vqslrpj5klofs2@alvherre.pgsql> (raw)
In-Reply-To: <CAFCRh-_RY7x0AE0=j-Vu5i5f+FQ=x6ze+4wp1suyAZ=nPQVS1A@mail.gmail.com>
On 2023-Mar-23, Dominique Devienne wrote:
> We have a query returning 1 row per constraint column,
> which until recently we didn't realize wasn't preserving order of the
> columns.
>
> A colleague fixed that, with something like below:
>
> SELECT ...
> FROM pg_catalog.pg_constraint cnstr
> ...
> CROSS JOIN LATERAL UNNEST(cnstr.conkey) WITH ORDINALITY AS cols(value, rank)
> ORDER BY cols.rank
>
> But I'm wondering about getting 1 row per constraint instead,
> and fetching an array of column names.
IIRC you can use array_agg(unnest ORDER BY ordinality), grouping by
constraint OID or name.
--
Álvaro Herrera 48°01'N 7°57'E — https://www.EnterpriseDB.com/
"La espina, desde que nace, ya pincha" (Proverbio africano)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: alvherre@alvh.no-ip.org, ddevienne@gmail.com
Subject: Re: Convert pg_constraint.conkey array to same-order array of column names
In-Reply-To: <20230324105119.43vqslrpj5klofs2@alvherre.pgsql>
References: <CAFCRh-_RY7x0AE0=j-Vu5i5f+FQ=x6ze+4wp1suyAZ=nPQVS1A@mail.gmail.com> <20230324105119.43vqslrpj5klofs2@alvherre.pgsql>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox