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 1oR6hC-00063W-KD for pgsql-general@arkaria.postgresql.org; Thu, 25 Aug 2022 06:50:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oR6hB-00025k-CP for pgsql-general@arkaria.postgresql.org; Thu, 25 Aug 2022 06:50:29 +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 1oR6hA-00025Z-Na for pgsql-general@lists.postgresql.org; Thu, 25 Aug 2022 06:50:29 +0000 Received: from mail-pf1-x432.google.com ([2607:f8b0:4864:20::432]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1oR6gz-0000O3-P7 for pgsql-general@lists.postgresql.org; Thu, 25 Aug 2022 06:50:27 +0000 Received: by mail-pf1-x432.google.com with SMTP id t129so86850pfb.6 for ; Wed, 24 Aug 2022 23:50:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yugabyte.com; s=google; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:from:to:cc; bh=hDNHEQm2+nm+YtEB80JAr+JLpXacqURCnvuqfK031zE=; b=W5H6glKNtefQsNiQO2+oNI1p19IuQXSt6IFOvNF/66Av1ErEIGrsjZbRF+r9xhw91d zbfq7PGaAYJAgq4HUGYlhIPbUdtTWAVjLY6UzqU3UEo3KPVxpFUHrsvUP8+NPQDTATjt BGxBp62Z2SerwWOp+VfUnM9V49u4aQeU3i3Zs= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:x-gm-message-state:from:to:cc; bh=hDNHEQm2+nm+YtEB80JAr+JLpXacqURCnvuqfK031zE=; b=cAfmuIzQGfm/dbLY9OQN8dia7g5P58kq1n4oNDrVg+5wUlk8A/lIlhASUuzWBX1oty uqT2Pmg05BaVRe90GYq+SMMxeOel2TDkMyE8+M7gL5pI9CxEqWGuBGf19ugvj+JGje74 b0z/DVHl8YMWx3cCs+o/KiTCzQUZoYwjgKEcYbp5M8b8whSRQr43mot6qYT3phLxXmYl gPMj1jxucsoYv0Gjaus1BmTGubSDV9vXI6KM33jAygIrPaAukeG1L7h22kggcSEpcq/H YTlCmbsZcLxayWvdOZgOycu5YG42vXjsAy82r1wZ+G7xmqS7dXXODpDCtQbHfSv1fZXf uMfw== X-Gm-Message-State: ACgBeo0xLVZWKHHHbu9IJU7kxAOi6iMGSRjpBj8J0yE/ca37LqDpuXr2 sPWEc9bcFMRPKOTg/f40n48Q9g== X-Google-Smtp-Source: AA6agR6fEhcH/D+7zJto3jzR3ybJUieeRRBwREvza/HRLhog7km83rTwi0uN8tgX4f5B2owGFQKnBQ== X-Received: by 2002:a63:581f:0:b0:42a:e7a6:9721 with SMTP id m31-20020a63581f000000b0042ae7a69721mr2144280pgb.455.1661410216293; Wed, 24 Aug 2022 23:50:16 -0700 (PDT) Received: from smtpclient.apple ([2601:646:101:7fda:b8cb:7b44:a87f:fded]) by smtp.gmail.com with ESMTPSA id c76-20020a624e4f000000b00535f2b5a23dsm13026446pfb.155.2022.08.24.23.50.15 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 24 Aug 2022 23:50:15 -0700 (PDT) From: Bryn Llewellyn Message-Id: <470B384D-5F76-449B-AFC3-1AE36FF84C03@yugabyte.com> Content-Type: multipart/alternative; boundary="Apple-Mail=_BBBBCEFD-A842-4493-8138-6C8A77A9D7C6" Mime-Version: 1.0 (Mac OS X Mail 14.0 \(3654.120.0.1.13\)) Subject: Re: Two questions about "pg_constraint" Date: Wed, 24 Aug 2022 23:50:14 -0700 In-Reply-To: <1161776.1661374651@sss.pgh.pa.us> Cc: pgsql-general list , Adrian Klaver To: Tom Lane PostgreSQL References: <496793CE-A048-434B-ABC0-6BFC0C65C3EC@yugabyte.com> <1161776.1661374651@sss.pgh.pa.us> X-Mailer: Apple Mail (2.3654.120.0.1.13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Apple-Mail=_BBBBCEFD-A842-4493-8138-6C8A77A9D7C6 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > tgl@sss.pgh.pa.us wrote: >=20 >> bryn@yugabyte.com writes: >>=20 >> Question 1: why does "pg_constraint" have a "connamespace" column? >=20 > You appear to be assuming that every pg_constraint entry is tied to a = table. This isn't so. I see how this might have appeared to be the case. But I did, very much, = know about the possibility to define a constraint on a domain. (It's a = very valuable feature and I've used it a lot.) > (1) That catalog also carries check constraints for domains, which are = tied to types instead. Yeah, you could imagine some rule like "look in = either pg_class or pg_type to find the schema", but it'd be really = painful. Yes, I knew that too. The documentation of the catalog tables is terse = to a fault=E2=80=94and I, at least, have to do empirical tests to try to = discern the proper mental model. I'm probably misunderstanding some = things. Having said this, the results from these query seem to be = informative. I did it in a "play" database where I'd created both a = table with a column check constraint and a domain with a value = constraint. select count(*) from pg_constraint where (conrelid <> 0 and contypid =3D = 0); -->> 107 rows select count(*) from pg_constraint where (conrelid =3D 0 and contypid = <> 0); -->> 3 rows select exists( select 1 from pg_constraint where not ( (conrelid =3D 0 and contypid <> 0) or (conrelid <> 0 and contypid =3D 0) ) )::text as "conrelid and contypid both non-zero"; -->> false My hypothesis (of which I've been unable to find a statement in the doc) = is that this shows a reliable invariant. In other words, it seems that a = constraint has an arc-FK relationship to its parent EITHER in "pg_class" = OR in "pg_type". I think that you imply this with your "look in either = pg_class or pg_type to find the schema". However, I don't feel this = lookup to be painful. I anyway want a view that lists all the schema = objects that implement my database app. I've copied my definition of my "schema_objects" view at the end. It = unions these (aliased) columns: oid, name, schema_oid, owner_oid, kind from "pg_class", "pg_type", and "pg_proc". (These, so far, are = sufficient for my present purpose.) It also adds the column "namespace" = to materialize which catalog table is the source: pg_class =E2=86=92 'relations', pg_type =E2=86=92 'types', and pg_proc = =16=E2=86=92 'subprograms' Then it joins this union with "pg_namespace" and "pg_roles" to get the = human-readable names for these. The upshot is that my "schema_objects" view has a surrogate PK, "oid", = and the unique business key "(name, schema, namespace)". I don't know what better name to use for my "namespace" column. It's = taken up by the name of the view for schemas (which should, in my book, = be called "pg_schema"=E2=80=94in the singular or plural according to the = usual coin-toss. My name captures the meaning and accords with the fact = that I can have an object whose "name" is 'x' three times in "schema" = called 's1' distinguished from each other by the fact that each has a = different "namespace" thus: 'relations', 'types' and 'subprograms'. I'm confused by these outcomes: - when you create a composite type manually, you automatically get an = entry with its name and schema BOTH in "pg_class" AND in "pg_type". - when you create a table or a view manually, you again automatically = get an entry with its name and schema BOTH in "pg_class" AND in = "pg_type". Where can I read about the mental model for this? Anyway, with my "schema objects" in place, I can create a "constrants" = view thus: create view a.constraints(c_name, c_kind, c_expr, t_name, t_schema, = t_namespace, t_kind) as with c(conname, contype, expr, sch_obj_id) as ( select conname, contype, pg_get_expr(conbin, conrelid), case contypid when 0 then conrelid else contypid end from pg_constraint) select c.conname, c.contype, c.expr, s.name, s.schema, s.namespace, s.kind from a.schema_objects s inner join c on sch_obj_id =3D s.oid; Then this query: select c_name, c_kind, c_expr, t_name, t_schema, t_namespace, t_kind = from a.constraints where t_schema =3D any(array['s1', 's2', 's3']) order by c_name, t_name, t_schema, t_namespace; shows this result: c_name | c_kind | c_expr | t_name | t_schema | t_namespace | = t_kind =20 = ----------+--------+----------------+--------+----------+-------------+---= ------------- d_chk | c | s2.fnc(VALUE) | dom | s3 | types | = domain itm_pkey | p | | itm | s1 | relations | = ordinary-table t_chk | c | (v =3D lower(v)) | tab | s1 | relations = | ordinary-table tab_pkey | p | | tab | s1 | relations | = ordinary-table for the test objects that I created. So (from the user P.o.V.) a = constraint is uniquely identified by its name, its kind, and the = identity of the schema object that it hangs off. This seems to me to be = just what I want. > (2) The SQL standard describes "assertions", which are global check = constraints that can affect multiple tables. We don't support those, and = very possibly never will, but the pg_constraint catalog is set up to = support them. Presumably they'd be stored with conrelid and contypid = both zero, so there would be no other place to find out the assertion's = schema. Well yes, I wish that PG did support SQL assertions. I expect that you = know that there's a vote to see how popular he idea would be for Oracle = Database: = https://community.oracle.com/tech/apps-infra/discussion/4390732/sql-assert= ions-declarative-multi-row-constraints But I've no idea what might come of that. > I'm not entirely convinced that putting these two (or three) sorts of = objects in the same catalog was a great design. However, that's what = we've got and changing it seems like more trouble than it'd be worth. I don't know what you mean by "in that same catalog". I thought that = "catalog" was the prose name for all those tables in the "pg_catalog" = schema of which we've focused on a few here: "pg_class", "pg_type", = "pg_proc", "pg_namespace", "pg_roles", and "pg_constraint". It would seem to me that a SQL assertion must be a (first class) schema = object (i.e. with, as the user would see it, the unique business key = "(name, schema, namespace)" and with its very own "owner"=E2=80=94and = then all sorts of other properties. I can't see how it could belong in "pg_constraint" because it doesn't = "hang off" a single schema object (like existing kinds of constraint do) = but, rather, spans any number of tables which (I'd hope) could have = various owners. I'd assume there be a new "assertion on" privilege that = would have to be granted to the owner of the to-be-created assertion on = each of the tables that the new assertion would span. So, to follow the pattern, there should be a new dedicated = "pg_assertion" table. This would have no impact on what the existing = "pg_constraint" would record because you couldn't have a constraint on = an assertion. = --------------------------------------------------------------------------= ------ create view a.schema_objects(oid, name, schema, namespace, owner, kind) = as with o(oid, name, schema_oid, namespace, owner_oid, kind) as ( select oid, relname, relnamespace, 'relations', relowner, case relkind when 'r' then 'ordinary-table' when 'v' then 'view' when 'i' then 'index' when 'S' then 'sequence' when 'c' then 'composite-type' else 'other' end from pg_class union all select oid, typname, typnamespace, 'types', typowner, case typtype when 'b' then 'base type' when 'c' then 'composite-type' when 'd' then 'domain' when 'e' then 'enum' else 'other' end from pg_type -- 'b' (here) is automtically gerenerated 'base type' -- has no info. value for the present use-case where typtype <> 'b' union all select oid, proname, pronamespace, 'subprograms', proowner, case prokind when 'f' then 'function' when 'p' then 'procedure' else 'other' end from pg_proc ) select o.oid, o.name, s.nspname, namespace, r.rolname, o.kind from o inner join pg_namespace s on o.schema_oid =3D s.oid inner join pg_roles r on o.owner_oid =3D r.oid; --Apple-Mail=_BBBBCEFD-A842-4493-8138-6C8A77A9D7C6 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8
tgl@sss.pgh.pa.us wrote:

bryn@yugabyte.com = writes:

Question 1: why = does "pg_constraint" have a "connamespace" column?

You = appear to be assuming that every pg_constraint entry is tied to a = table. This isn't so.
I see how this might have appeared to = be the case. But I did, very much, know about the possibility to define = a constraint on a domain. (It's a very valuable feature and I've used it = a lot.)

(1) That catalog also = carries check constraints for domains, which are tied to types = instead. Yeah, you could imagine some rule like "look in either = pg_class or pg_type to find the schema", but it'd be really = painful.

Yes, I knew that too. The documentation = of the catalog tables is terse to a fault=E2=80=94and I, at least, have = to do empirical tests to try to discern the proper mental model. I'm = probably misunderstanding some things. Having said this, the results = from these query seem to be informative. I did it in a "play" database = where I'd created both a table with a column check constraint and a = domain with a value constraint.

select count(*) from pg_constraint where = (conrelid <> 0 and contypid =3D   0); -->> 107 rows

select count(*) from pg_constraint where = (conrelid =3D  0 and contypid <>  0); -->> 3 = rows

select exists(
  =   select 1 from pg_constraint
  =   where not (
      =   (conrelid =3D  0 and contypid <> 0) or
        (conrelid <> 0 and = contypid =3D  0)
      )
  )::text as "conrelid and contypid both non-zero"; = -->> false

My hypothesis (of which I've been unable to find a statement = in the doc) is that this shows a reliable invariant. In other words, it = seems that a constraint has an arc-FK relationship to its parent EITHER = in "pg_class" OR in "pg_type". I think that you imply this with your = "look in either pg_class or pg_type to find the schema". However, I = don't feel this lookup to be painful. I anyway want a view that lists = all the schema objects that implement my database app.

I've copied my = definition of my "schema_objects" view at the end. It unions these = (aliased) columns:

oid, name, schema_oid, owner_oid, = kind

from "pg_class", "pg_type", and "pg_proc". (These, so far, = are sufficient for my present purpose.) It also adds the column = "namespace" to materialize which catalog table is the source:

pg_class =E2=86=92 'relations', = pg_type =E2=86=92 'types', and pg_proc =16=E2=86=92 = 'subprograms'

Then it joins this union with = "pg_namespace" and "pg_roles" to get the human-readable names for = these.

The = upshot is that my "schema_objects" view has a surrogate PK, "oid", and = the unique business key "(name, schema, namespace)".

I don't know what better = name to use for my "namespace" column. It's taken up by the name of the = view for schemas (which should, in my book, be called "pg_schema"=E2=80=94= in the singular or plural according to the usual coin-toss. My name = captures the meaning and accords with the fact that I can have an object = whose "name" is 'x' three times in "schema" called 's1' distinguished = from each other by the fact that each has a different "namespace" thus: = 'relations', 'types' and 'subprograms'.

I'm confused by these = outcomes:

- = when you create a composite type manually, you automatically get an = entry with its name and schema BOTH in "pg_class" AND in = "pg_type".

- = when you create a table or a view manually, you again automatically get = an entry with its name and schema BOTH in "pg_class" AND in = "pg_type".

Where= can I read about the mental model for this?

Anyway, with my "schema objects" in = place, I can create a "constrants" view thus:

create view a.constraints(c_name, c_kind, = c_expr, t_name, t_schema, t_namespace, t_kind) as
with = c(conname, contype, expr, sch_obj_id) as (
  select
  =   conname,
    contype,
    pg_get_expr(conbin, conrelid),
    case contypid
    =   when 0 then conrelid
    =   else        contypid
    end
  from = pg_constraint)
select
  c.conname,
  c.contype,
  c.expr,
  s.name,
  s.schema,
  s.namespace,  s.kind
from
  a.schema_objects s
  inner = join
  c
  on sch_obj_id = =3D s.oid;

Then this query:

select c_name, c_kind, c_expr, t_name, t_schema, t_namespace, = t_kind from a.constraints
where t_schema =3D = any(array['s1', 's2', 's3'])
order by c_name, t_name, = t_schema, t_namespace;

shows this result:

  c_name  | c_kind = |     c_expr     | t_name | = t_schema | t_namespace |     t_kind    =  
----------+--------+----------------+--------+----------+------= -------+----------------
 d_chk    | = c      | s2.fnc(VALUE)  | dom  =   | s3       | types   =     | domain
 itm_pkey | p  =     |              =   | itm    | s1     =   | relations   | ordinary-table
 t_chk    | c      | = (v =3D lower(v)) | tab    | s1     =   | relations   | ordinary-table
 tab_pkey | p      |    =             | tab    | = s1       | relations   | = ordinary-table

for the test objects that I created. So = (from the user P.o.V.) a constraint is uniquely identified by its name, = its kind, and the identity of the schema object that it hangs off. This = seems to me to be just what I want.

(2) The SQL = standard describes "assertions", which are global check constraints = that can affect multiple tables. We don't support those, and very = possibly never will, but the pg_constraint catalog is set up to = support them. Presumably they'd be stored with conrelid and = contypid both zero, so there would be no other place to find out = the assertion's schema.

Well = yes, I wish that PG did support SQL assertions. I expect that you know = that there's a vote to see how popular he idea would be for Oracle = Database:


But I've no idea what = might come of that.

I'm not entirely = convinced that putting these two (or three) sorts of objects in the = same catalog was a great design. However, that's what we've got and = changing it seems like more trouble than it'd be worth.

I don't know what you mean by "in = that same catalog". I thought that "catalog" was the prose name for all = those tables in the "pg_catalog" schema of which we've focused on a few = here: "pg_class", "pg_type", "pg_proc", "pg_namespace", "pg_roles", and = "pg_constraint".

It would seem to me that a SQL assertion must be a (first = class) schema object (i.e. with, as the user would see it, the unique = business key "(name, schema, namespace)" and with its very own = "owner"=E2=80=94and then all sorts of other properties.

I can't see how it could = belong in "pg_constraint" because it doesn't "hang off" a single schema = object (like existing kinds of constraint do) but, rather, spans any = number of tables which (I'd hope) could have various owners. I'd assume = there be a new "assertion on" privilege that would have to be granted to = the owner of the to-be-created assertion on each of the tables that the = new assertion would span.

So, to follow the pattern, there should be a new dedicated = "pg_assertion" table. This would have no impact on what the existing = "pg_constraint" would record because you couldn't have a constraint on = an assertion.

---------------------------------------------------------------= -----------------

create view a.schema_objects(oid, name, schema, = namespace, owner, kind) as
  with o(oid, name, = schema_oid, namespace, owner_oid, kind) as
  =   (
      select
        oid,
  =       relname,
      =   relnamespace,
      =   'relations',
      =   relowner,
      =   case relkind
        =   when 'r' then 'ordinary-table'
    =       when 'v' then 'view'
  =         when 'i' then 'index'
          when 'S' then = 'sequence'
          when = 'c' then 'composite-type'
        =   else          'other'
        end
  =     from pg_class
    union = all
      select
  =       oid,
      =   typname,
      =   typnamespace,
      =   'types',
      =   typowner,
      =   case typtype
        =   when 'b' then 'base type'
      =     when 'c' then 'composite-type'
  =         when 'd' then 'domain'
          when 'e' then = 'enum'
          else  =         'other'
    =     end
      from = pg_type
      -- 'b' (here) is = automtically gerenerated 'base type'
    =   -- has no info. value for the present use-case
      where typtype <> 'b'
    union all
    =   select
      =   oid,
      =   proname,
      =   pronamespace,
      =   'subprograms',
      =   proowner,
      =   case prokind
        =   when 'f' then 'function'
      =     when 'p' then 'procedure'
    =       else        =   'other'
      =   end
      from pg_proc
    )
select
  o.oid,
  o.name,
  s.nspname,
  namespace,
  r.rolname,
  o.kind
from
  o
  inner join
  pg_namespace = s
  on o.schema_oid =3D s.oid
  inner join pg_roles r
  on = o.owner_oid =3D r.oid;

= --Apple-Mail=_BBBBCEFD-A842-4493-8138-6C8A77A9D7C6--