forked frompostgres/postgres
- Notifications
You must be signed in to change notification settings - Fork6
Commite5fac1c
committed
Avoid unnecessary recursion to child tables in ALTER TABLE SET NOT NULL.
If a partitioned table's column is already marked NOT NULL, there isno need to examine its partitions, because we can rely on previousDDL to have enforced that the child columns are NOT NULL as well.(Unfortunately, the same cannot be said for traditional inheritance,so for now we have to restrict the optimization to partitioned tables.)Hence, we may skip recursing to child tables in this situation.The reason this case is worth worrying about is that when pg_dump dumpsa partitioned table having a primary key, it will include the requisiteNOT NULL markings in the CREATE TABLE commands, and then add theprimary key as a separate step. The primary key addition generates aSET NOT NULL as a subcommand, just to be sure. So the situation wherea SET NOT NULL is redundant does arise in the real world.Skipping the recursion does more than just save a few cycles: it meansthat a command such as "ALTER TABLE ONLY partition_parent ADD PRIMARYKEY" will take locks only on the partition parent table, not on thepartitions. It turns out that parallel pg_restore is effectivelyassuming that that's true, and has little choice but to do so becausethe dependencies listed for such a TOC entry don't include thepartitions. pg_restore could thus issue this ALTER while data restoreson the partitions are still in progress. Taking unnecessary locks onthe partitions not only hurts concurrency, but can lead to actualdeadlock failures, as reported by Domagoj Smoljanovic.(A contributing factor in the deadlock is that TRUNCATE on a childpartition wants a non-exclusive lock on the parent. This seemslikewise unnecessary, but the fix for it is more invasive so wewon't consider back-patching it. Fortunately, getting rid of oneof these two poor behaviors is enough to remove the deadlock.)Although support for partitioned primary keys came in with v11,this patch is dependent on the SET NOT NULL refactoring done bycommitf4a3fdf, so we can only patch back to v12.Patch by me; thanks to Alvaro Herrera and Amit Langote for review.Discussion:https://postgr.es/m/VI1PR03MB31670CA1BD9625C3A8C5DD05EB230@VI1PR03MB3167.eurprd03.prod.outlook.com1 parent3d65b05 commite5fac1c
1 file changed
+38
-7
lines changedLines changed: 38 additions & 7 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
5681 | 5681 |
| |
5682 | 5682 |
| |
5683 | 5683 |
| |
5684 |
| - | |
5685 |
| - | |
5686 |
| - | |
| 5684 | + | |
| 5685 | + | |
5687 | 5686 |
| |
5688 |
| - | |
5689 |
| - | |
5690 |
| - | |
5691 |
| - | |
| 5687 | + | |
5692 | 5688 |
| |
5693 | 5689 |
| |
5694 | 5690 |
| |
| |||
6698 | 6694 |
| |
6699 | 6695 |
| |
6700 | 6696 |
| |
| 6697 | + | |
| 6698 | + | |
| 6699 | + | |
| 6700 | + | |
| 6701 | + | |
| 6702 | + | |
| 6703 | + | |
| 6704 | + | |
| 6705 | + | |
| 6706 | + | |
| 6707 | + | |
| 6708 | + | |
| 6709 | + | |
| 6710 | + | |
| 6711 | + | |
| 6712 | + | |
| 6713 | + | |
| 6714 | + | |
| 6715 | + | |
| 6716 | + | |
| 6717 | + | |
| 6718 | + | |
| 6719 | + | |
| 6720 | + | |
| 6721 | + | |
| 6722 | + | |
| 6723 | + | |
| 6724 | + | |
| 6725 | + | |
| 6726 | + | |
| 6727 | + | |
| 6728 | + | |
| 6729 | + | |
| 6730 | + | |
| 6731 | + | |
6701 | 6732 |
| |
6702 | 6733 |
| |
6703 | 6734 |
| |
|
0 commit comments
Comments
(0)