Re: cataloguing NOT NULL constraints
Robert Haas <robertmhaas@gmail.com>
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
On Mon, May 13, 2024 at 12:45 PM Alvaro Herrera <alvherre@alvh.no-ip.org> wrote: > The point is that a column can be in a primary key and not have an > explicit not-null constraint. This is different from having a column be > NOT NULL and having a primary key on top. In both cases the attnotnull > flag is set; the difference between these two scenarios is what happens > if you drop the primary key. If you do not have an explicit not-null > constraint, then the attnotnull flag is lost as soon as you drop the > primary key. You don't have to do DROP NOT NULL for that to happen > > This means that if you have a column that's in the primary key but does > not have an explicit not-null constraint, then we shouldn't make one up. > (Which we would, if we were to keep an unadorned NOT NULL that we can't > drop at the end of the dump.) It seems to me that the practical thing to do about this problem is just decide not to solve it. I mean, it's currently the case that if you establish a PRIMARY KEY when you create a table, the columns of that key are marked NOT NULL and remain NOT NULL even if the primary key is later dropped. So, if that didn't change, we would be no less compliant with the SQL standard (or your reading of it) than we are now. And if you do really want to make that change, why not split it out into its own patch, so that the patch that does $SUBJECT is changing the minimal number of other things at the same time? That way, reverting something might not involve reverting everything, plus you could have a separate design discussion about what that fix ought to look like, separate from the issues that are truly inherent to cataloging NOT NULL constraints per se. What I meant about changing the order of operations is that, currently, the database knows that the column is NOT NULL before the COPY happens, and I don't think we can change that. I think you agree -- that's why you invented the throwaway constraints. As far as I can see, the problems all have to do with getting the "throwaway" part to happen correctly. It can't be a problem to just mark the relevant columns NOT NULL in the relevant tables -- we already do that. But if you want to discard some of those NOT NULL markings once the PRIMARY KEY is added, you have to know which ones to discard. If we just consider the most straightforward scenario where somebody does a full dump-and-restore, getting that right may be annoying, but it seems like it surely has to be possible. The dump will just have to understand which child tables (or, more generally, descendent tables) got a NOT NULL marking on a column because of the PK and which ones had an explicit marking in the old database and do the right thing in each case. But what if somebody does a selective restore of one table from a partitioning hierarchy? Currently, the columns that would have been part of the primary key end up NOT NULL, but the primary key itself is not restored because it can't be. What will happen in this new system? If you don't apply any NOT NULL constraints to those columns, then a user who restores one partition from an old dump and tries to reattach it to the correct partitioned table has to recheck the NOT NULL constraint, unlike now. If you apply a normal-looking garden-variety NOT NULL constraint to that column, you've invented a constraint that didn't exist in the source database. And if you apply a throwaway NOT NULL constraint but the user never attaches that table anywhere, then the throwaway constraint survives. None of those options sound very good to me. Another scenario: Say that you have a table with a PRIMARY KEY. For some reason, you want to drop the primary key and then add it back. Well, with this definitional change, as soon as you drop it, you forget that the underlying columns don't contain any nulls, so when you add it back, you have to check them again. I don't know who would find that behavior an improvement over what we have today. So I don't really think it's a great idea to change this behavior, but even if it is, is it such a good idea that we want to sink the whole patch set repeatedly over it, as has already happened twice now? I feel that if we did what Tom suggested a year ago in https://www.postgresql.org/message-id/3801207.1681057430@sss.pgh.pa.us -- "I'm inclined to think that this idea of suppressing the implied NOT NULL from PRIMARY KEY is a nonstarter and we should just go ahead and make such a constraint" -- there's a very good chance that a revert would have been avoided here and it would still be just as valid to think of revisiting this particular question in a future release as it is now. -- Robert Haas EDB: http://www.enterprisedb.com