Re: Convert pg_constraint.conkey array to same-order array of column names

From: Alvaro Herrera <alvherre(at)alvh(dot)no-ip(dot)org>
To: Dominique Devienne <ddevienne(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Convert pg_constraint.conkey array to same-order array of column names
Date: 2023-03-24 10:51:19
Message-ID: 20230324105119.43vqslrpj5klofs2@alvherre.pgsql
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

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)

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Inzamam Shafiq 2023-03-24 11:07:11 PostgreSQL vs MariaDB
Previous Message Dominique Devienne 2023-03-24 10:28:49 Re: Convert pg_constraint.conkey array to same-order array of column names