Re: cataloguing NOT NULL constraints
Alvaro Herrera <alvherre@alvh.no-ip.org>
Commits
GET /api/v1/messages/:b64id/commits
the thread's linked commits as JSON, with link sources.
API reference →
-
Revert structural changes to not-null constraints
- 6f8bb7c1e961 17.0 landed
-
Fix inconsistencies in error messages
- 21ac38f498b3 17.0 landed
-
Disallow direct change of NO INHERIT of not-null constraints
- d45597f72fe5 17.0 landed
-
Disallow NO INHERIT not-null constraints on partitioned tables
- 13daa33fa5a6 17.0 landed
-
Better handle indirect constraint drops
- 0cd711271d42 17.0 cited
-
Don't try to assign smart names to constraints
- d72d32f52d26 17.0 cited
-
Fix restore of not-null constraints with inheritance
- d9f686a72ee9 17.0 landed
-
ATTACH PARTITION: Don't match a PK with a UNIQUE constraint
- cee8db3f680b 17.0 landed
-
Fix propagating attnotnull in multiple inheritance
- c3709100be73 17.0 landed
-
Check stack depth in new recursive functions
- b0f7dd915bca 17.0 landed
-
Move privilege check to the right place
- ac22a9545ca9 17.0 cited
-
Update information_schema definition for not-null constraints
- 3af721794272 17.0 landed
-
Fix not-null constraint test
- d0ec2ddbe088 17.0 landed
-
Disallow changing NO INHERIT status of a not-null constraint
- 9b581c534186 17.0 cited
-
Catalog not-null constraints
- b0e96f311985 17.0 cited
-
parallel_schedule: add comment on event_trigger test dependency
- c8e43c22be27 17.0 landed
-
Revert "Catalog NOT NULL constraints" and fallout
- 9ce04b50e120 16.0 landed
-
Adjust contrib/sepgsql regression test expected outputs.
- 76c111a7f166 16.0 landed
-
Fix table name clash in recently introduced test
- 728015a47016 16.0 landed
-
Catalog NOT NULL constraints
- e056c557aef4 16.0 landed
-
Change the rules for inherited CHECK constraints to be essentially the same
- cd902b331dc4 8.4.0 cited
Thanks for spending so much time with this patch -- really appreciated. On 2023-Jul-26, Dean Rasheed wrote: > On Tue, 25 Jul 2023 at 13:36, Alvaro Herrera <alvherre@alvh.no-ip.org> wrote: > > > > Okay then, I've made these show up in the footer of \d+. This is in > > patch 0003 here. Please let me know what do you think of the regression > > changes. > > The new \d+ output certainly makes testing and reviewing easier, > though I do understand people's concerns that this may make the output > significantly longer in many real-world cases. I don't think it would > be a good idea to filter the list in any way though, because I think > that will only lead to confusion. I think it should be all-or-nothing, > though I'm not necessarily opposed to using something like \d++ to > enable it, if that turns out to be the least-bad option. Yeah, at this point I'm inclined to get the \d+ version committed immediately after the main patch, and we can tweak the psql UI after the fact -- for instance so that they are only shown in \d++, or some other idea we may come across. > Going back to this example: > > drop table if exists p1, p2, foo; > create table p1(a int not null check (a > 0)); > create table p2(a int not null check (a > 0)); > create table foo () inherits (p1,p2); > I remain of the opinion that that should create 2 NOT NULL constraints > on foo, for consistency with CHECK constraints, and the misleading > name that results if p1_a_not_null is dropped from p1. That way, the > names of inherited NOT NULL constraints could be kept in sync, as they > are for other constraint types, making it easier to keep track of > where they come from, and it wouldn't be necessary to treat them > differently (e.g., matching by column number, when dropping NOT NULL > constraints). I think having two constraints is more problematic, UI-wise. Previous versions of this patchset did it that way, and it's not great: for example ALTER TABLE ALTER COLUMN DROP NOT NULL fails and tells you to choose which exact constraint you want to drop and use DROP CONSTRAINT instead. And when searching for the not-null constraints for a column, the code had to consider the case of there being multiple ones, which led to strange contortions. Allowing a single one is simpler and covers all important cases well. Anyway, you still can't drop the doubly-inherited constraint directly, because it'll complain that it is an inherited constraint. So you have to deinherit first and only then can you drop the constraint. Now, one possible improvement here would be to ignore the parent constraint's name, and have 'foo' recompute its own constraint name from scratch, inheriting the name only if one of the parents had a manually-specified constraint name (and we would choose the first one, if there's more than one). I think complicating things more than that is unnecessary -- particularly considering that legacy inheritance is, well, legacy, and I doubt people are relying too much on it. > Given the following sequence: > > drop table if exists p,c; > create table p(a int primary key); > create table c() inherits (p); > alter table p drop constraint p_pkey; > > p.a ends up being nullable, where previously it would have been left > non-nullable. That change makes sense, and is presumably one of the > benefits of tying the nullability of columns to pg_constraint entries. Right. > However, c.a remains non-nullable, with a NOT NULL constraint that > claims to be inherited: > > \d+ c > Table "public.c" > Column | Type | Collation | Nullable | Default | Storage | > Compression | Stats target | Description > --------+---------+-----------+----------+---------+---------+-------------+--------------+------------- > a | integer | | not null | | plain | > | | > Not null constraints: > "c_a_not_null" NOT NULL "a" (inherited) > Inherits: p > Access method: heap > > That's a problem, because now the NOT NULL constraint on c cannot be > dropped (attempting to drop it on c errors out because it thinks it's > inherited, but it can't be dropped via p, because p.a is already > nullable). Oh, I think the bug here is just that this constraint should not claim to be inherited, but standalone. So you can drop it afterwards; but if you drop it and end up with NULL values in your PK-labelled column in the parent table, that's on you. > I wonder if NOT NULL constraints created as a result of inherited PKs > should have names based on the PK name (e.g., > <PK_name>_<col_name>_not_null), to make it more obvious where they > came from. That would be more consistent with the way NOT NULL > constraint names are inherited. Hmm, interesting idea. I'll play with it. (It may quickly lead to constraint names that are too long, though.) > Given the following sequence: > > drop table if exists p,c; > create table p(a int); > create table c() inherits (p); > alter table p add primary key (a); > > c.a ends up non-nullable, but there is no pg_constraint entry > enforcing the constraint: > > \d+ c > Table "public.c" > Column | Type | Collation | Nullable | Default | Storage | > Compression | Stats target | Description > --------+---------+-----------+----------+---------+---------+-------------+--------------+------------- > a | integer | | not null | | plain | > | | > Inherits: p > Access method: heap Oh, this one's a bad omission. I'll fix it. > Given a database containing these 2 tables: > > create table p(a int primary key); > create table c() inherits (p); > > doing a pg_dump and restore fails to restore the NOT NULL constraint > on c, because all constraints created by the dump are local to p. Strange. I'll see about fixing this one too. -- Álvaro Herrera 48°01'N 7°57'E — https://www.EnterpriseDB.com/ "La primera ley de las demostraciones en vivo es: no trate de usar el sistema. Escriba un guión que no toque nada para no causar daños." (Jakob Nielsen)