Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1l7UWa-00049V-Rj for pgsql-hackers@arkaria.postgresql.org; Thu, 04 Feb 2021 02:37:41 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1l7UWZ-0002xg-Dx for pgsql-hackers@arkaria.postgresql.org; Thu, 04 Feb 2021 02:37:39 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1l7UWX-0002xZ-WA for pgsql-hackers@lists.postgresql.org; Thu, 04 Feb 2021 02:37:39 +0000 Received: from out5-smtp.messagingengine.com ([66.111.4.29]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1l7UWV-0007tL-8P for pgsql-hackers@lists.postgresql.org; Thu, 04 Feb 2021 02:37:37 +0000 Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.nyi.internal (Postfix) with ESMTP id 321BE5C00F2; Wed, 3 Feb 2021 21:37:34 -0500 (EST) Received: from imap2 ([10.202.2.52]) by compute5.internal (MEProxy); Wed, 03 Feb 2021 21:37:34 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=compiler.org; h= mime-version:message-id:in-reply-to:references:date:from:to:cc :subject:content-type; s=fm3; bh=io8dIl0tWCyishdiCo2Lmj3C1FWEpns IXOK1HFO3+uU=; b=CeZL7lX4tlAb1KvK2eU4SEedDkTly/jRULYM2vyZreDbWuu ftQa48IGb2XR/Z+ud/edP7pYA5YFYEW0Sw8JVBgndP92rn2xWf3L2c0qf7dLnAd8 04gw3cjsffE2tmItucIXBLXtHFA+du3nsgPF4A0Yxc3ejpBZnJ6EOKH0Pt8YBGWd D7F9Mj8ym7lWq+E99/kMfcU8l/qiWALRHJ/+kz42JcuIWpvy0hmCUeTvAmpSmZ1s ImctMiW8RbEHVdCW1++JsKm6tsA384AqqLPwJesgfi7nW2Itq47/u57A0FS0B3eV BZTaT0I5C6PezGsc47oaHeJuLJWZMaU6w10ONFA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; bh=io8dIl 0tWCyishdiCo2Lmj3C1FWEpnsIXOK1HFO3+uU=; b=nKrZkIBEoT3o4404vKt42w 1dRWyim09+bL4Nl1c12T+OPeY77cLPywi5pA5Mleb4KzMNzLc2b6Lr4VxTUr2mrS eFg7NE8tbuSunf2e7xSLYOZWBQeFeuBgCOokGcXdpxo9lafdS7U/GvLneNqzyWiu GLfFABGQNje9kFcQOxQalQzaJCu/CtKKtq9EIquSZni+v9mbq0DNiMxjlHa3wwrY kCcT9Qym2WdEMlggSLQ7Cb+wm9NncaUMJmtt3GPFz+mqmXTwLCPVgnMg9XlCTuGH O7oAzy7lTPyI4fnggumlLgY5ty+Sq2kbYYPiG/K/I4sVmJFVwg/w1ciQux7x8mzg == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledrgeefgdegfecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefofgggkfgjfhffhffvufgtsegrtderreerredtnecuhfhrohhmpedflfhovghl ucflrggtohgsshhonhdfuceojhhovghlsegtohhmphhilhgvrhdrohhrgheqnecuggftrf grthhtvghrnheptdeuudfftdefffejuddttefhvdetheejheejieetgfduieffffefgffg gedvheefnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomh epjhhovghlsegtohhmphhilhgvrhdrohhrgh X-ME-Proxy: Received: by mailuser.nyi.internal (Postfix, from userid 501) id 4CC70A0005D; Wed, 3 Feb 2021 21:37:33 -0500 (EST) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.5.0-alpha0-93-gef6c4048e6-fm-20210128.002-gef6c4048 Mime-Version: 1.0 Message-Id: <9542ec0d-2ade-444d-95e4-92b335ae32f2@www.fastmail.com> In-Reply-To: References: <3240355.1612129197@sss.pgh.pa.us> <3418996.1612209831@sss.pgh.pa.us> Date: Thu, 04 Feb 2021 03:37:13 +0100 From: "Joel Jacobson" To: "Tom Lane" Cc: pgsql-hackers@lists.postgresql.org, "Peter Eisentraut" Subject: Re: Recording foreign key relationships for the system catalogs Content-Type: multipart/alternative; boundary=1ea2f176e8fc43c68b956d0f221ff3ff List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --1ea2f176e8fc43c68b956d0f221ff3ff Content-Type: text/plain On Wed, Feb 3, 2021, at 21:41, Joel Jacobson wrote: >Otherwise I think it would be more natural to change both is_array and is_opt >to boolean[] with the same cardinality as fkcols and pkcols, >to allow unnest()ing of them as well. Another option would perhaps be to add a new system view in src/backend/catalog/system_views.sql I see there are other cases with a slightly more complex view using a function with a similar name, such as the pg_stat_activity using pg_stat_get_activity(). Similar to this, maybe we could add a pg_catalog_foreign_keys view using the output from pg_get_catalog_foreign_keys(): Example usage: SELECT * FROM pg_catalog_foreign_keys WHERE fktable = 'pg_constraint'::regclass AND pktable = 'pg_attribute'::regclass; fkid | fktable | fkcol | pktable | pkcol | is_array | is_opt | ordinal_position ------+---------------+-----------+--------------+----------+----------+--------+------------------ 48 | pg_constraint | conkey | pg_attribute | attnum | t | t | 1 48 | pg_constraint | conrelid | pg_attribute | attrelid | f | f | 2 49 | pg_constraint | confkey | pg_attribute | attnum | t | f | 1 49 | pg_constraint | confrelid | pg_attribute | attrelid | f | f | 2 (4 rows) The point of this would be to avoid unnecessary increase of data model complexity, which I agree is not needed, since we only need single booleans as of today, but to provide a more information_schema-like system view, i.e. with columns on separate rows, with ordinal_position. Since we don't have any "constraint_name" for these, we need to enumerate the fks first, to let ordinal_position be the position within each such fkid. Here is my proposal on how to implement: CREATE VIEW pg_catalog_foreign_keys AS WITH enumerate_fks AS ( SELECT *, ROW_NUMBER() OVER () AS fkid FROM pg_catalog.pg_get_catalog_foreign_keys() ), unnest_cols AS ( SELECT C.fkid, C.fktable, unnest(C.fkcols) AS fkcol, C.pktable, unnest(C.pkcols) AS pkcol, unnest( CASE cardinality(fkcols) WHEN 1 THEN ARRAY[C.is_array] WHEN 2 THEN ARRAY[FALSE,C.is_array] END ) AS is_array, unnest( CASE cardinality(fkcols) WHEN 1 THEN ARRAY[C.is_opt] WHEN 2 THEN ARRAY[FALSE,C.is_opt] END ) AS is_opt FROM enumerate_fks AS C ) SELECT *, ROW_NUMBER() OVER ( PARTITION BY U.fkid ORDER BY U.fkcol, U.pkcol ) AS ordinal_position FROM unnest_cols AS U; I think both the pg_get_catalog_foreign_keys() function and this view are useful in different ways, so it's good to provide both. Only providing pg_get_catalog_foreign_keys() will arguably mean some users of the function will need to implement something like the same as above on their own, if they need the is_array and is_opt value for a specific fkcol. /Joel --1ea2f176e8fc43c68b956d0f221ff3ff Content-Type: text/html Content-Transfer-Encoding: quoted-printable
On Wed, Fe= b 3, 2021, at 21:41, Joel Jacobson wrote:
>Otherwise I = think it would be more natural to change both is_array and is_opt
>to boolean[] with the same cardinality as fkcols and pkcols,=
>to allow unnest()ing of them as well.
<= br>
Another option would perhaps be to add a new
system view in src/backend/catalog/system_views.sql
=
I see there are other cases with a slightly more complex = view
using a function with a similar name, such as
the pg_stat_activity using pg_stat_get_activity().

Similar t= o this, maybe we could add a pg_catalog_foreign_keys view
= using the output from pg_get_ca= talog_foreign_keys():

Example usage:=

SELECT * FROM pg_catalog_foreign_keys
<= /div>
WHERE fktable =3D 'pg_constraint'::regclass
AND = pktable =3D 'pg_attribute'::regclass;

fkid = |    fktable    |   fkcol &= nbsp; |   pktable    |  pkcol   = | is_array | is_opt | ordinal_position
------+------------= ---+-----------+--------------+----------+----------+--------+----------= --------
   48 | pg_constraint | conkey &nb= sp;  | pg_attribute | attnum   | t    = ;    | t      |   = ;            = ; 1
   48 | pg_constraint | conrelid  | pg_= attribute | attrelid | f        | f&n= bsp;     |       =          2
 &= nbsp; 49 | pg_constraint | confkey   | pg_attribute | attnum&n= bsp;  | t        | f  =     |        &nbs= p;       1
   49 |= pg_constraint | confrelid | pg_attribute | attrelid | f  &nbs= p;     | f      | &nbs= p;           &nbs= p;  2
(4 rows)

The point= of this would be to avoid unnecessary increase of data model complexity= ,
which I agree is not needed, since we only need single b= ooleans as of today,
but to provide a more information_sch= ema-like system view,
i.e. with columns on separate rows, = with ordinal_position.

Since we don't have any "constraint_name" fo= r these,
we need to enumerate the fks first, to let <= span style=3D"color:rgb(0, 0, 0);">ordinal_position
be the position within each such fk= id.

Here is my proposal on how to im= plement:

CREATE VIEW pg_catalog_foreign_key= s AS
    WITH
  &nb= sp; enumerate_fks AS (
      = ;  SELECT
       &= nbsp;    *,
     &= nbsp;      ROW_NUMBER() OVER () AS fkid
        FROM pg_catalog.pg_get= _catalog_foreign_keys()
    ),
    unnest_cols AS (
   &n= bsp;    SELECT
    &nbs= p;       C.fkid,
  = ;          C.fktable,
          &nb= sp; unnest(C.fkcols) AS fkcol,
    &nb= sp;       C.pktable,
 &= nbsp;          unnest(C.pkc= ols) AS pkcol,
       &= nbsp;    unnest(
    &n= bsp;           CASE ca= rdinality(fkcols)
      &nbs= p;           &nbs= p; WHEN 1 THEN ARRAY[C.is_array]
    &= nbsp;           &= nbsp;   WHEN 2 THEN ARRAY[FALSE,C.is_array]
&nbs= p;           &nbs= p;   END
      &nb= sp;     ) AS is_array,
  &nb= sp;         unnest(
           &nbs= p;    CASE cardinality(fkcols)
  =             =       WHEN 1 THEN ARRAY[C.is_opt]
            = ;        WHEN 2 THEN ARRAY[FALSE,C.is= _opt]
        &nbs= p;       END
  &nb= sp;         ) AS is_opt
        FROM enumerate_fks AS C=
    )
    SEL= ECT
        *,
        ROW_NUMBER() OVER (
=
          &= nbsp; PARTITION BY U.fkid
     &n= bsp;      ORDER BY U.fkcol, U.pkcol
        ) AS ordinal_position
<= /div>
    FROM unnest_cols AS U;

I think both the pg_ge= t_catalog_foreign_keys() function
and this view are useful in different ways,
so it's good to provide = both.

Only providing pg_get_catalog_foreign_keys() will
=
arguably mean some users of the= function will need to implement
something like the same as above on their own, if they= need the is_array and is_opt
value for a specific fkcol.

/Joel
--1ea2f176e8fc43c68b956d0f221ff3ff--