- Notifications
You must be signed in to change notification settings - Fork4.9k
Commita379061
committed
Allow NOT NULL constraints to be added as NOT VALID
This allows them to be added without scanning the table, and validatingthem afterwards without holding access exclusive lock on the table afterany violating rows have been deleted or fixed.Doing ALTER TABLE ... SET NOT NULL for a column that has an invalidnot-null constraint validates that constraint. ALTER TABLE .. VALIDATECONSTRAINT is also supported. There are various checks on whether aninvalid constraint is allowed in a child table when the parent table hasa valid constraint; this should match what we do for enforced/notenforced constraints.pg_attribute.attnotnull is now only an indicator for whether a not-nullconstraint exists for the column; whether it's valid or invalid must bequeried in pg_constraint. Applications can continue to querypg_attribute.attnotnull as before, but now it's possible that NULL rowsare present in the column even when that's set to true.For backend internal purposes, we cache the nullability status inCompactAttribute->attnullability that each tuple descriptor carries(replacing CompactAttribute.attnotnull, which was a mirror ofForm_pg_attribute.attnotnull). During the initial tuple descriptorcreation, based on the pg_attribute scan, we set this to UNRESTRICTED ifpg_attribute.attnotnull is false, or to UNKNOWN if it's true; then weupdate the latter to VALID or INVALID depending on the pg_constraintscan. This flag is also copied when tupledescs are copied.Comparing tuple descs for equality must also compare theCompactAttribute.attnullability flag and return false in case of amismatch.pg_dump deals with these constraints by storing the OIDs of invalidnot-null constraints in a separate array, and running a query to obtaintheir properties. The regular table creation SQL omits them entirely.They are then dealt with in the same way as "separate" CHECKconstraints, and dumped after the data has been loaded. Because noadditional pg_dump infrastructure was required, we don't bump itsversion number.I decided not to bump catversion either, because the old catalog stateworks perfectly in the new world. (Trying to run with new catalog stateand the old server version would likely run into issues, however.)System catalogs do not support invalid not-null constraints (becausecommit14e87ff didn't allow them to have pg_constraint rowsanyway.)Author: Rushabh Lathia <rushabh.lathia@gmail.com>Author: Jian He <jian.universality@gmail.com>Reviewed-by: Álvaro Herrera <alvherre@alvh.no-ip.org>Tested-by: Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>Discussion:https://postgr.es/m/CAGPqQf0KitkNack4F5CFkFi-9Dqvp29Ro=EpcWt=4_hs-Rt+bQ@mail.gmail.com1 parentb52a4a5 commita379061
File tree
21 files changed
+1109
-111
lines changed- doc/src/sgml
- ref
- src
- backend
- access/common
- catalog
- commands
- jit/llvm
- optimizer/util
- parser
- utils/cache
- bin
- pg_dump
- t
- psql
- include
- access
- catalog
- test/regress
- expected
- sql
21 files changed
+1109
-111
lines changedLines changed: 1 addition & 1 deletion
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
1260 | 1260 |
| |
1261 | 1261 |
| |
1262 | 1262 |
| |
1263 |
| - | |
| 1263 | + | |
1264 | 1264 |
| |
1265 | 1265 |
| |
1266 | 1266 |
| |
|
Lines changed: 5 additions & 3 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
243 | 243 |
| |
244 | 244 |
| |
245 | 245 |
| |
| 246 | + | |
| 247 | + | |
246 | 248 |
| |
247 | 249 |
| |
248 | 250 |
| |
| |||
458 | 460 |
| |
459 | 461 |
| |
460 | 462 |
| |
461 |
| - | |
462 |
| - | |
| 463 | + | |
| 464 | + | |
463 | 465 |
| |
464 | 466 |
| |
465 | 467 |
| |
| |||
586 | 588 |
| |
587 | 589 |
| |
588 | 590 |
| |
589 |
| - | |
| 591 | + | |
590 | 592 |
| |
591 | 593 |
| |
592 | 594 |
| |
|
Lines changed: 36 additions & 2 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
22 | 22 |
| |
23 | 23 |
| |
24 | 24 |
| |
| 25 | + | |
25 | 26 |
| |
26 | 27 |
| |
27 | 28 |
| |
| |||
74 | 75 |
| |
75 | 76 |
| |
76 | 77 |
| |
77 |
| - | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
78 | 88 |
| |
79 | 89 |
| |
80 | 90 |
| |
| |||
144 | 154 |
| |
145 | 155 |
| |
146 | 156 |
| |
147 |
| - | |
| 157 | + | |
148 | 158 |
| |
149 | 159 |
| |
| 160 | + | |
150 | 161 |
| |
151 | 162 |
| |
152 | 163 |
| |
| |||
333 | 344 |
| |
334 | 345 |
| |
335 | 346 |
| |
| 347 | + | |
336 | 348 |
| |
337 | 349 |
| |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
338 | 354 |
| |
339 | 355 |
| |
340 | 356 |
| |
| |||
613 | 629 |
| |
614 | 630 |
| |
615 | 631 |
| |
| 632 | + | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
616 | 650 |
| |
617 | 651 |
| |
618 | 652 |
| |
|
Lines changed: 7 additions & 2 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
2616 | 2616 |
| |
2617 | 2617 |
| |
2618 | 2618 |
| |
| 2619 | + | |
| 2620 | + | |
2619 | 2621 |
| |
2620 | 2622 |
| |
2621 |
| - | |
| 2623 | + | |
| 2624 | + | |
| 2625 | + | |
2622 | 2626 |
| |
2623 | 2627 |
| |
2624 |
| - | |
| 2628 | + | |
| 2629 | + | |
2625 | 2630 |
| |
2626 | 2631 |
| |
2627 | 2632 |
| |
|
Lines changed: 30 additions & 16 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
576 | 576 |
| |
577 | 577 |
| |
578 | 578 |
| |
579 |
| - | |
580 |
| - | |
| 579 | + | |
| 580 | + | |
581 | 581 |
| |
582 | 582 |
| |
583 | 583 |
| |
| |||
606 | 606 |
| |
607 | 607 |
| |
608 | 608 |
| |
609 |
| - | |
610 |
| - | |
| 609 | + | |
| 610 | + | |
611 | 611 |
| |
612 | 612 |
| |
613 | 613 |
| |
614 |
| - | |
615 |
| - | |
616 | 614 |
| |
617 | 615 |
| |
618 | 616 |
| |
| |||
630 | 628 |
| |
631 | 629 |
| |
632 | 630 |
| |
633 |
| - | |
634 |
| - | |
635 |
| - | |
| 631 | + | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
636 | 635 |
| |
637 | 636 |
| |
638 | 637 |
| |
| |||
723 | 722 |
| |
724 | 723 |
| |
725 | 724 |
| |
| 725 | + | |
726 | 726 |
| |
727 |
| - | |
728 |
| - | |
729 |
| - | |
730 |
| - | |
| 727 | + | |
| 728 | + | |
| 729 | + | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
731 | 734 |
| |
732 | 735 |
| |
733 | 736 |
| |
734 |
| - | |
| 737 | + | |
735 | 738 |
| |
736 | 739 |
| |
737 | 740 |
| |
| |||
755 | 758 |
| |
756 | 759 |
| |
757 | 760 |
| |
| 761 | + | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
| 771 | + | |
758 | 772 |
| |
759 | 773 |
| |
760 | 774 |
| |
| |||
832 | 846 |
| |
833 | 847 |
| |
834 | 848 |
| |
835 |
| - | |
| 849 | + | |
836 | 850 |
| |
837 | 851 |
| |
838 | 852 |
| |
| |||
852 | 866 |
| |
853 | 867 |
| |
854 | 868 |
| |
855 |
| - | |
| 869 | + | |
856 | 870 |
| |
857 | 871 |
| |
858 | 872 |
| |
|
0 commit comments
Comments
(0)