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 1oRRBf-0002EP-W2 for pgsql-general@arkaria.postgresql.org; Fri, 26 Aug 2022 04:43:20 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oRRBe-0008WJ-2r for pgsql-general@arkaria.postgresql.org; Fri, 26 Aug 2022 04:43:18 +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 1oRRBd-0008Qb-CU for pgsql-general@lists.postgresql.org; Fri, 26 Aug 2022 04:43:17 +0000 Received: from mail-pl1-x632.google.com ([2607:f8b0:4864:20::632]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1oRRBZ-0002IX-M2 for pgsql-general@lists.postgresql.org; Fri, 26 Aug 2022 04:43:16 +0000 Received: by mail-pl1-x632.google.com with SMTP id u22so599375plq.12 for ; Thu, 25 Aug 2022 21:43:13 -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=Z+/1wTxis+lw9LnFAhj1sYJSIdMUKW7mttbkC+9FdkU=; b=HbfFE+cK7fhDxnSJw8RYptV90CgGnTQM519UAH6PdXwJVwuJA9jzF9lg5uEPOWQ6iW QLAkM0XuATUItNT2HA+z6QqHdxLcyqg5YHcpswVehbQLvTxijY/9Eo1ycD7HI6vSV0uD h1zSLaAm2OjXRkSVIQ+5s8nPcbCu1r0nVxvEI= 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=Z+/1wTxis+lw9LnFAhj1sYJSIdMUKW7mttbkC+9FdkU=; b=V64/mGPRjdayPFtBPZFOsQAT9EiylkaIK04ZtoLPVJsXBdgzIg4/J9e+JzwEbSnJaA OxX1OexhCRF7f1C7IAc7GO6BRub0KbGB/zuuTL5UOQhUsdKya6OhUJ2LWKXifQQwRsIZ DtdFu9yRRvLpEf5ET6tm6DNNOgWBc+k9dmIXT1eOLrCEattgbb4arY6TgHpbSRqeHiQ5 XkkKwv+iBvA6gQD0XiCbrXt2i9J1uxqOM1eggT3wsFOr4CXIKMYIy2dVL9HNbZtpVmkT DBnZoxYK7v4RK+147MlPsy9fANiFzRNucicTZJAIbTtFr8dEKKiqZZJVlC1/SZ3/NR1p n1kQ== X-Gm-Message-State: ACgBeo16q7+JoR2jIm8cNIPhy/CM031hgt2cp9qIWNh4/M/mnhmNCmYf 3gmv0zINTrh4aQ99/xDzHpj4NA== X-Google-Smtp-Source: AA6agR4HBuO1vw1pap7iMZwlHxS2+2DoZtIlBgC9mF9/fCfROvcptnwHOtxiWpbiU2RDsfFccKvEwQ== X-Received: by 2002:a17:902:d48f:b0:16f:a73:bf04 with SMTP id c15-20020a170902d48f00b0016f0a73bf04mr2184853plg.43.1661488987657; Thu, 25 Aug 2022 21:43:07 -0700 (PDT) Received: from smtpclient.apple ([2601:646:101:7fda:d4b:d2f1:b448:dcab]) by smtp.gmail.com with ESMTPSA id l23-20020a639857000000b0041a615381d5sm460874pgo.4.2022.08.25.21.43.06 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 25 Aug 2022 21:43:07 -0700 (PDT) From: Bryn Llewellyn Message-Id: Content-Type: multipart/alternative; boundary="Apple-Mail=_4B2667D3-4262-48D4-BF31-C5EA49B2439F" Mime-Version: 1.0 (Mac OS X Mail 14.0 \(3654.120.0.1.13\)) Subject: Re: Two questions about "pg_constraint" Date: Thu, 25 Aug 2022 21:43:05 -0700 In-Reply-To: <520dd935-a12c-6444-99a7-5a8f2d0f046d@aklaver.com> Cc: pgsql-general list , Tom Lane PostgreSQL To: Adrian Klaver References: <496793CE-A048-434B-ABC0-6BFC0C65C3EC@yugabyte.com> <520dd935-a12c-6444-99a7-5a8f2d0f046d@aklaver.com> 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=_4B2667D3-4262-48D4-BF31-C5EA49B2439F Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > adrian.klaver@aklaver.com wrote: >=20 >> bryn@yugabyte.comwrote: >>=20 >> Question 1: why does "pg_constraint" have a "connamespace" column? >=20 > create table c1 (id integer, constraint pk1 primary key(id)); >=20 > create table c2 (id integer, constraint pk1 primary key(id)); > ERROR: relation "pk1" already exists >=20 > create table test.c2 (id integer, constraint pk1 primary key(id)); >=20 > select conname, connamespace from pg_constraint where conname =3D = 'pk1'; > conname | connamespace > ---------+-------------- > pk1 | 2200 > pk1 | 59706 >=20 > [=46rom the doc] =E2=80=94 conname name =E2=80=94 Constraint name (not = necessarily unique!) So connamespace makes it unique. I'll assume that, in Adrian's example, bare "create table c1" puts it = into the public schema. But it could be, equivalently, that the creating = user has a search path with some other schema (but not the schema = 'test=E2=80=99) in first-to-be-searched position. The example implicitly brings indexes into the picture. The explanation = of the "index_parameters" rule in the =E2=80=9Ccreate table=E2=80=9D = syntax starts off thus: =C2=AB index_parameters in UNIQUE, PRIMARY KEY, and EXCLUDE constraints = are... =C2=BB and it goes on to mention some choices that you can make about the = indexes that necessarily back the PK and unique constraints. (The = notions apply at "alter table" time too.) However, it says nothing to = indicate that the user can specify the name (or the schema) for this = backing index. I can't find where the rule is stated for how the index = is (as it must be) automatically named. But it seems, from experiment, = that the name for the index that backs a PK or a unique constraint is = simply the name that the user gave the constraint=E2=80=94or the = generated name of the constraint if it wasn't named. The schema-and-name combination (a.k.a. the qualified name) for an index = must be unique in "pg_rel". So "create table c2 ... constraint pk1) = fails because there already is a backing index called "pk1" in place in = the schema in question. That's why the error message says =C2=AB = relation "pk1" already exists =C2=BB and not =C2=AB constraint "pk1" = already exists =C2=BB. Here's a contrived example to emphasize this point: create table s1.irrelevant_name_1( k int, v1 int, v2 int, constraint irrelevant_name_2 primary key(k)); create index c on s1.irrelevant_name_1(v1); It seems odd that an index must be in the same schema as the table upon = whose columns (or expressions) it is created. The "create index" account = documents this restriction. An ad hoc test shows that the index must = also have the same owner as its table. I can't find where this = restriction is documented. I don't suppose that these restrictions pose = a practical problem. Now do this: alter table s1.irrelevant_name_1 add constraint c unique(v2); It fails with the error "relation "c" already exists" (and not = "constraint "c" already exists"). And sure enough, there is no existing = constraint called 'c'. With me so far? Now try this: create table s1.irrelevant_name_1( k int primary key, v text, constraint c check(v =3D lower(v))); create domain s1.irrelevant_name_2 as int[] constraint c check(cardinality(value) > 1); No errors occur. B.t.w., the table and the domain must have different = names because of the secondary effect that the name of a table causes a = row BOTH in "pg_class" AND in "pg_type". Now do Adrian's query: select conname, connamespace from pg_constraint where conname =3D 'c'; This was my result. (Your oid values will vary.) conname | connamespace=20 ---------+-------------- c | 2114282 c | 2114282 We see two constraints with the same name "in" the same schema. I double = quoted "in" because it's the wrong way to talk about this. Constraints = (just like triggers) are not schema-objects. But the things that = "pg_class", "pg_type", and "pg_proc" list _are_ schema-objects. A schema-object's business unique key is its name, the name of the = schema in which it's located, and the name of the catalog table where = it's listed. (Elsewhere, I called this third fact the "namespace" = because its the scope within which the name must be unique. But you = might not like this use of "namespace".) Anyway, "in" is used correctly when we say that a schema-object is in a = schema. A schema-object also has (must have) an owner. But this isn't = part of its business unique key. In contrast, a constraint (and a trigger) are not in a schema. Nor do = they have an owner. Rather, each hangs off a schema object (and must do = this if it's to exist). The business unique key of each of a constraint = and a trigger is its own name together with the business unique key of = what it hangs off. You might argue that it inherits the schema and the = owner of the schema-object off which it hangs. But I think that this is = the wrong way to think about it. That's why I argue that "pg_constraint" = should not have a "connamespace" column=E2=80=94just as it already = doesn't have a "conowner" column. The fact that "pg_constraint" does have a "connamespace" column is an = example of what I believe the argot of our trade calls a "transitive = dependency" (the criterion which, added to the criteria for 2NF elevates = it to 3NF). "\d pg constraint" says that the table has a unique = constraint on "(conrelid, contypid, conname)". And (as I read the doc = for "conrelid" and "contypid") is does imply the rule that I observed: = either one is non zero and the other is zero or vice versa (and so not = both zero and not both non-zero). So "(conrelid, contypid)" (i.e. just = part of the unique key) does indeed translate to the unique identifier = of the item (table or domain) that the constraint hangs off. When I know = the value of (conrelid, contypid), I know the oid of the schema that = this schema object is in. Ergo, "connamespace" depends upon the partial = key "(conrelid, contypid)"=E2=80=94i.e. this is a transitive dependency. This takes me back to the query that I used as the background to pose my = =C2=AB why does "pg_constraint" have a "connamespace" column? =C2=BB = question. Here it is=E2=80=94in an extended form. And here it does = indeed use "(conrelid, contypid)" to look up the schema oid in the = appropriate one of "pg_class" or "pg_type". It helps to establish the query, for re-use, as a temporary view. create temporary view constraint_facts( conname, schema_object_oid, "catalog table", "schema_oid from pg_class/pg_type", same) as with c1(conname, schema_object_oid, c_tab, connamespace) as ( select conname, case contypid when 0 then conrelid else contypid end, case contypid when 0 then 'pg_class' else 'pg_type' end, connamespace from pg_constraint), c2(conname, schema_object_oid, c_tab, schema_oid, connamespace) as ( select conname, schema_object_oid, c_tab, case c_tab when 'pg_class' then (select relnamespace from pg_class where oid = =3D schema_object_oid) when 'pg_type' then (select typnamespace from pg_type where oid = =3D schema_object_oid) end, connamespace from c1) select conname, schema_object_oid, c_tab, schema_oid, (schema_oid =3D connamespace) from c2; Now use it to look at our two constraints called 'c': select conname, schema_object_oid, "catalog table", "schema_oid from = pg_class/pg_type", same::text from constraint_facts where conname =3D 'c' order by conname, schema_object_oid, "catalog table"; This is the result: conname | schema_object_oid | catalog table | schema_oid from = pg_class/pg_type | same=20 = ---------+-------------------+---------------+----------------------------= ------+------ c | 2114297 | pg_class | = 2114282 | true c | 2114306 | pg_type | = 2114282 | true Now try it on all the entire contents of "pg_constraint"=E2=80=94one's = own stuff together with the hundred or so rows that implement the PG = system: select exists(select 1 from constraint_facts where not same)::text; The answer is "false". Please tell me if you can see a typo in my query, = a fault in the reasoning the led to how I spelled it, or a flaw in my = overall analysis. I realize, of course that the "false" result here = merely shows that the hypothesis that the query tests is not disproved. = I don't think that it can be proved. Rather, its truth, or otherwise, = comes from the prose statement of the requirements and the design of the = entire set of catalog tables=E2=80=94before any programming was done. But it does seem to me that the "false" outcome is consistent with the = common sense analysis that leads to the conclusion that "pg_constraint" = is not in 3NF. Two more things. First, the results from my "constrainst" view (that = uses my "schema_objects" view). I showed these in this turn in the = present thread: = https://www.postgresql.org/message-id/470B384D-5F76-449B-AFC3-1AE36FF84C03= %40yugabyte.com select c_name, t_name, t_schema, t_namespace, t_kind, c_kind, c_expr = from a.constraints where c_name =3D 'c' order by t_name, t_schema, t_namespace; This is the result: c_name | t_name | t_schema | t_namespace | t_kind | = c_kind | c_expr =20 = --------+-------------------+----------+-------------+----------------+---= -----+-------------------------- c | irrelevant_name_1 | s1 | relations | ordinary-table | = c | (v =3D lower(v)) c | irrelevant_name_2 | s1 | types | domain | = c | (cardinality(VALUE) > 1) The first four columns are each constraint's business unique key. And = the last three are some of its properties or of what it hangs off. And finally, a little test to confirm that the constraint kind is not = part of a constraint's business unique key. Make sure that schema 's1' = is empty when you try it. create table s1.t( k int, v int, constraint c primary key(k), constraint c unique(v)); It fails because (as I'd say it) it attempts to create two constrains = whose "(c_name, t_name, t_schema, t_namespace)" values are the = same=E2=80=94or, briefly, =C2=AB constraint "c" already exists =C2=BB. = In fact, the error message spells it with "relation" and not with = "constraint". I think that this is an error. --Apple-Mail=_4B2667D3-4262-48D4-BF31-C5EA49B2439F Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8
adrian.klaver@aklaver.com wrote:


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

create table c1 (id = integer, constraint pk1 primary key(id));

create table c2 (id integer, constraint pk1 primary = key(id));
ERROR:  relation "pk1" already exists

create table test.c2 (id integer, constraint = pk1 primary key(id));

select conname, = connamespace from pg_constraint where conname =3D 'pk1';
= conname | connamespace
---------+--------------
pk1     | =         2200
pk1 =     | =        59706

[=46rom the doc] =E2=80=94 conname name = =E2=80=94 Constraint name (not necessarily unique!) So = connamespace makes it unique.

I'll= assume that, in Adrian's example, bare "create table c1" puts it into = the public schema. But it could be, equivalently, that the creating user = has a search path with some other schema (but not the schema 'test=E2=80=99= ) in first-to-be-searched position.

The example implicitly brings indexes = into the picture. The explanation of the "index_parameters" rule in the = =E2=80=9Ccreate table=E2=80=9D syntax starts off thus:

=C2=AB = index_parameters in UNIQUE, PRIMARY KEY, = and EXCLUDE constraints are... =C2=BB

and it goes on to mention some choices = that you can make about the indexes that necessarily back the PK and = unique constraints. (The notions apply at "alter table" time too.) = However, it says nothing to indicate that the user can specify the name = (or the schema) for this backing index. I can't find where the rule is = stated for how the index is (as it must be) automatically named. But it = seems, from experiment, that the name for the index that backs a PK or a = unique constraint is simply the name that the user gave the = constraint=E2=80=94or the generated name of the constraint if it wasn't = named.

The = schema-and-name combination (a.k.a. the qualified name) for an index = must be unique in "pg_rel". So "create table c2 ... constraint pk1) = fails because there already is a backing index called "pk1" in place in = the schema in question. That's why the error message says =C2=AB = relation "pk1" already exists =C2=BB and not =C2=AB constraint = "pk1" already exists =C2=BB.

Here's a contrived example to emphasize = this point:

create table s1.irrelevant_name_1(
  k = int,
  v1 int,
  v2 = int,
  constraint irrelevant_name_2 primary = key(k));

create index c on = s1.irrelevant_name_1(v1);

It seems odd that an index must = be in the same schema as the table upon whose columns (or expressions) = it is created. The "create index" account documents this = restriction. An ad hoc test shows that the index must also have the same = owner as its table. I can't find where this restriction is documented. I = don't suppose that these restrictions pose a practical = problem.

Now do this:

alter= table s1.irrelevant_name_1 add
  constraint c = unique(v2);

It fails with the error "relation "c" already exists" (and = not "constraint "c" already exists"). And sure enough, there is no = existing constraint called 'c'.

With me so far?

Now try this:

create table s1.irrelevant_name_1(
  k int primary key,
  v = text,
  constraint c check(v =3D lower(v)));

create domain s1.irrelevant_name_2 as int[]
  constraint c check(cardinality(value) > 1);

No = errors occur. B.t.w., the table and the domain must have different names = because of the secondary effect that the name of a table causes a row = BOTH in "pg_class" AND in "pg_type".

Now do Adrian's query:

select conname, connamespace from pg_constraint = where conname =3D 'c';

This was my result. (Your oid values = will vary.)

 conname | connamespace 
---------+--------------
 c   =     |      2114282
 c       |    =   2114282

We see two constraints with the same = name "in" the same schema. I double quoted "in" because it's the wrong = way to talk about this. Constraints (just like triggers) are not = schema-objects. But the things that "pg_class", "pg_type", and "pg_proc" = list _are_ schema-objects.

A schema-object's business unique key is its name, the name = of the schema in which it's located, and the name of the catalog table = where it's listed. (Elsewhere, I called this third fact the "namespace" = because its the scope within which the name must be unique. But you = might not like this use of "namespace".)

Anyway, "in" is used correctly when we = say that a schema-object is in a schema. A schema-object also has (must = have) an owner. But this isn't part of its business unique = key.

In = contrast, a constraint (and a trigger) are not in a schema. Nor do they = have an owner. Rather, each hangs off a schema object (and must do this = if it's to exist). The business unique key of each of a constraint and a = trigger is its own name together with the business unique key of what it = hangs off. You might argue that it inherits the schema and the owner of = the schema-object off which it hangs. But I think that this is the wrong = way to think about it. That's why I argue that "pg_constraint" should = not have a "connamespace" column=E2=80=94just as it already doesn't have = a "conowner" column.

The fact that "pg_constraint"  does have a = "connamespace" column is an example of what I believe the argot of our = trade calls a "transitive dependency" (the criterion which, added to the = criteria for 2NF elevates it to 3NF). "\d pg constraint" says that the = table has a unique constraint on "(conrelid, contypid, conname)". And = (as I read the doc for "conrelid" and "contypid") is does imply the rule = that I observed: either one is non zero and the other is zero or vice = versa (and so not both zero and not both non-zero). So "(conrelid, = contypid)" (i.e. just part of the unique key) does indeed translate to = the unique identifier of the item (table or domain) that the constraint = hangs off. When I know the value of (conrelid, contypid),  I = know the oid of the schema that this schema object is in. Ergo, = "connamespace" depends upon the partial key "(conrelid, = contypid)"=E2=80=94i.e. this is a transitive dependency.

This takes me back to = the query that I used as the background to pose my =C2=AB why does = "pg_constraint" have a "connamespace" column? =C2=BB question. Here it = is=E2=80=94in an extended form. And here it does indeed use "(conrelid, = contypid)" to look up the schema oid in the appropriate one of = "pg_class" or "pg_type".

It helps to establish the query, for re-use, as a temporary = view.

create temporary view = constraint_facts(
  conname,
  schema_object_oid,
  "catalog table",
  "schema_oid from pg_class/pg_type",
  same)
as
with
  c1(conname, schema_object_oid, c_tab, = connamespace) as (
  select
  =   conname,
    case contypid
      when 0 then conrelid
      else      =   contypid
    end,
    case contypid
    =   when 0 then 'pg_class'
    =   else        'pg_type'
    end,
  =   connamespace
  from = pg_constraint),

  c2(conname, = schema_object_oid, c_tab, schema_oid, connamespace) as (
  select
  =   conname,
  =   schema_object_oid,
  =   c_tab,
    case c_tab
      when 'pg_class' then (select = relnamespace  from pg_class where oid =3D = schema_object_oid)
      when = 'pg_type'  then (select typnamespace  from = pg_type  where oid =3D schema_object_oid)
  =   end,
    connamespace
  from c1)

select
  conname,
  schema_object_oid,
  c_tab,
  schema_oid,
  (schema_oid =3D connamespace)
from = c2;

Now use it to look at our two constraints called = 'c':

select conname, = schema_object_oid, "catalog table", "schema_oid from pg_class/pg_type", = same::text
from constraint_facts
where = conname =3D 'c'
order by conname, schema_object_oid, = "catalog table";

This is the result:

 conname | schema_object_oid | catalog = table | schema_oid from pg_class/pg_type | same 
---------+-------------------+---------------+-----------------= -----------------+------
 c     =   |           2114297 | = pg_class      |          =                 2114282 | = true
 c       |   =         2114306 | pg_type     =   |                =           2114282 | = true

Now try it on all the entire contents of = "pg_constraint"=E2=80=94one's own stuff together with the hundred or so = rows that implement the PG system:

select exists(select 1 from constraint_facts = where not same)::text;

The answer is "false". Please tell me = if you can see a typo in my query, a fault in the reasoning the led to = how I spelled it, or a flaw in my overall analysis. I realize, of course = that the "false" result here merely shows that the hypothesis that the = query tests is not disproved. I don't think that it can be proved. = Rather, its truth, or otherwise, comes from the prose statement of the = requirements and the design of the entire set of catalog tables=E2=80=94be= fore any programming was done.

But it does seem to me that the "false" = outcome is consistent with the common sense analysis that leads to the = conclusion that "pg_constraint" is not in 3NF.

Two more things. First, the results = from my "constrainst" view (that uses my "schema_objects" view). I = showed these in this turn in the present thread:


select c_name, = t_name, t_schema, t_namespace, t_kind, c_kind, c_expr from = a.constraints
where c_name =3D 'c'
order by = t_name, t_schema, t_namespace
;

This is the = result:

 c_name |    =   t_name       | t_schema | = t_namespace |     t_kind     | = c_kind |          c_expr   =        
--------+-------------------+----------+-------------+---------= -------+--------+--------------------------
 c  =     | irrelevant_name_1 | s1     =   | relations   | ordinary-table | c  =     | (v =3D lower(v))
 c    =   | irrelevant_name_2 | s1       | = types       | domain       =   | c      | (cardinality(VALUE) > = 1)

The first four columns are each constraint's business unique = key. And the last three are some of its properties or of what it hangs = off.

And = finally, a little test to confirm that the constraint kind is not part = of a constraint's business unique key. Make sure that schema 's1' is = empty when you try it.

create table s1.t(
  k int,
  v int,
  constraint c = primary key(k),
  constraint c unique(v));

It fails because (as I'd say = it) it attempts to create two constrains whose "(c_name, t_name, = t_schema, t_namespace)" values are the same=E2=80=94or, briefly, = =C2=AB constraint "c" already exists =C2=BB. In fact, the error = message spells it with "relation" and not with "constraint". I think = that this is an error.

= --Apple-Mail=_4B2667D3-4262-48D4-BF31-C5EA49B2439F--