- Notifications
You must be signed in to change notification settings - Fork4.9k
Commit46a8c27
Restructure foreign key handling code for ATTACH/DETACH
... to fix bugs when the referenced table is partitioned.The catalog representation we chose for foreign keys connectingpartitioned tables (in commitf56f8f8) is inconvenient, in thesense that a standalone table has a different way to represent theconstraint when referencing a partitioned table, than when the sametable becomes a partition (and vice versa). Because of this, we need tocreate additional catalog rows on detach (pg_constraint and pg_trigger),and remove them on attach. We were doing some of those things, but notall of them, leading to missing catalog rows in certain cases.The worst problem seems to be that we are missing action triggers afterdetaching a partition, which means that you could update/delete rowsfrom the referenced partitioned table that still had referencing rows onthat table, the server failing to throw the required errors.!!!Note that this means existing databases with FKs that referencepartitioned tables might have rows that break relational integrity, ontables that were once partitions on the referencing side of the FK.Another possible problem is that trying to reattach a tablethat had been detached would fail indicating that internal triggerscannot be found, which from the user's point of view is nonsensical.In branches 15 and above, we fix this by creating a new helper functionaddFkConstraint() which is in charge of creating a standalonepg_constraint row, and repurposing addFkRecurseReferencing() andaddFkRecurseReferenced() so that they're only the recursive routine foreach side of the FK, and they call addFkConstraint() to createpg_constraint at each partitioning level and add the necessary triggers.These new routines can be used during partition creation, partitionattach and detach, and foreign key creation. This reduces redundantcode and simplifies the flow.In branches 14 and 13, we have a much simpler fix that consists onsimply removing the constraint on detach. The reason is that thosebranches are missing commitf456634, which reworked the way thisworks in a way that we didn't consider back-patchable at the time.We opted to leave branch 12 alone, because it's different from branch 13enough that the fix doesn't apply; and because it is going in EOL modevery soon, patching it now might be worse since there's no way to undothe damage if it goes wrong.Existing databases might need to be repaired.In the future we might want to rethink the catalog representation toavoid this problem, but for now the code seems to do what's required tomake the constraints operate correctly.Co-authored-by: Jehan-Guillaume de Rorthais <jgdr@dalibo.com>Co-authored-by: Tender Wang <tndrwang@gmail.com>Co-authored-by: Alvaro Herrera <alvherre@alvh.no-ip.org>Reported-by: Guillaume Lelarge <guillaume@lelarge.info>Reported-by: Jehan-Guillaume de Rorthais <jgdr@dalibo.com>Reported-by: Thomas Baehler (SBB CFF FFS) <thomas.baehler2@sbb.ch>Discussion:https://postgr.es/m/20230420144344.40744130@karstDiscussion:https://postgr.es/m/20230705233028.2f554f73@karstDiscussion:https://postgr.es/m/GVAP278MB02787E7134FD691861635A8BC9032@GVAP278MB0278.CHEP278.PROD.OUTLOOK.COMDiscussion:https://postgr.es/m/18541-628a61bc267cd2d3@postgresql.org1 parent5e94f61 commit46a8c27
File tree
3 files changed
+281
-26
lines changed- src
- backend/commands
- test/regress
- expected
- sql
3 files changed
+281
-26
lines changedLines changed: 134 additions & 26 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
10389 | 10389 |
| |
10390 | 10390 |
| |
10391 | 10391 |
| |
| 10392 | + | |
| 10393 | + | |
| 10394 | + | |
| 10395 | + | |
| 10396 | + | |
| 10397 | + | |
| 10398 | + | |
| 10399 | + | |
| 10400 | + | |
| 10401 | + | |
| 10402 | + | |
| 10403 | + | |
| 10404 | + | |
| 10405 | + | |
| 10406 | + | |
| 10407 | + | |
| 10408 | + | |
| 10409 | + | |
| 10410 | + | |
| 10411 | + | |
| 10412 | + | |
| 10413 | + | |
| 10414 | + | |
| 10415 | + | |
| 10416 | + | |
| 10417 | + | |
| 10418 | + | |
| 10419 | + | |
| 10420 | + | |
| 10421 | + | |
| 10422 | + | |
| 10423 | + | |
| 10424 | + | |
| 10425 | + | |
| 10426 | + | |
| 10427 | + | |
| 10428 | + | |
| 10429 | + | |
| 10430 | + | |
| 10431 | + | |
| 10432 | + | |
| 10433 | + | |
| 10434 | + | |
| 10435 | + | |
| 10436 | + | |
| 10437 | + | |
| 10438 | + | |
| 10439 | + | |
| 10440 | + | |
| 10441 | + | |
| 10442 | + | |
| 10443 | + | |
| 10444 | + | |
| 10445 | + | |
| 10446 | + | |
| 10447 | + | |
| 10448 | + | |
| 10449 | + | |
| 10450 | + | |
| 10451 | + | |
| 10452 | + | |
| 10453 | + | |
| 10454 | + | |
| 10455 | + | |
| 10456 | + | |
| 10457 | + | |
| 10458 | + | |
| 10459 | + | |
| 10460 | + | |
| 10461 | + | |
| 10462 | + | |
| 10463 | + | |
| 10464 | + | |
| 10465 | + | |
| 10466 | + | |
| 10467 | + | |
10392 | 10468 |
| |
10393 | 10469 |
| |
10394 | 10470 |
| |
| |||
18154 | 18230 |
| |
18155 | 18231 |
| |
18156 | 18232 |
| |
| 18233 | + | |
18157 | 18234 |
| |
18158 | 18235 |
| |
18159 | 18236 |
| |
| |||
18168 | 18245 |
| |
18169 | 18246 |
| |
18170 | 18247 |
| |
18171 |
| - | |
18172 |
| - | |
| 18248 | + | |
| 18249 | + | |
18173 | 18250 |
| |
18174 | 18251 |
| |
18175 | 18252 |
| |
18176 | 18253 |
| |
18177 | 18254 |
| |
18178 | 18255 |
| |
18179 | 18256 |
| |
18180 |
| - | |
18181 | 18257 |
| |
18182 | 18258 |
| |
18183 | 18259 |
| |
| |||
18192 | 18268 |
| |
18193 | 18269 |
| |
18194 | 18270 |
| |
18195 |
| - | |
| 18271 | + | |
18196 | 18272 |
| |
18197 | 18273 |
| |
18198 | 18274 |
| |
18199 |
| - | |
18200 |
| - | |
18201 |
| - | |
| 18275 | + | |
| 18276 | + | |
| 18277 | + | |
| 18278 | + | |
| 18279 | + | |
| 18280 | + | |
18202 | 18281 |
| |
18203 |
| - | |
18204 |
| - | |
18205 |
| - | |
18206 |
| - | |
18207 |
| - | |
18208 |
| - | |
18209 |
| - | |
18210 |
| - | |
18211 |
| - | |
18212 |
| - | |
18213 |
| - | |
18214 |
| - | |
18215 |
| - | |
18216 |
| - | |
18217 |
| - | |
18218 |
| - | |
| 18282 | + | |
| 18283 | + | |
| 18284 | + | |
18219 | 18285 |
| |
18220 |
| - | |
18221 |
| - | |
18222 |
| - | |
| 18286 | + | |
| 18287 | + | |
| 18288 | + | |
| 18289 | + | |
| 18290 | + | |
| 18291 | + | |
| 18292 | + | |
| 18293 | + | |
| 18294 | + | |
| 18295 | + | |
| 18296 | + | |
| 18297 | + | |
| 18298 | + | |
| 18299 | + | |
| 18300 | + | |
| 18301 | + | |
| 18302 | + | |
| 18303 | + | |
| 18304 | + | |
| 18305 | + | |
| 18306 | + | |
| 18307 | + | |
| 18308 | + | |
| 18309 | + | |
| 18310 | + | |
| 18311 | + | |
| 18312 | + | |
| 18313 | + | |
| 18314 | + | |
| 18315 | + | |
| 18316 | + | |
| 18317 | + | |
| 18318 | + | |
| 18319 | + | |
| 18320 | + | |
| 18321 | + | |
| 18322 | + | |
| 18323 | + | |
| 18324 | + | |
| 18325 | + | |
| 18326 | + | |
18223 | 18327 |
| |
18224 | 18328 |
| |
18225 | 18329 |
| |
18226 | 18330 |
| |
18227 | 18331 |
| |
| 18332 | + | |
| 18333 | + | |
| 18334 | + | |
| 18335 | + | |
18228 | 18336 |
| |
18229 | 18337 |
| |
18230 | 18338 |
| |
|
Lines changed: 91 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
2665 | 2665 |
| |
2666 | 2666 |
| |
2667 | 2667 |
| |
| 2668 | + | |
| 2669 | + | |
| 2670 | + | |
| 2671 | + | |
| 2672 | + | |
| 2673 | + | |
| 2674 | + | |
| 2675 | + | |
| 2676 | + | |
| 2677 | + | |
| 2678 | + | |
| 2679 | + | |
| 2680 | + | |
| 2681 | + | |
| 2682 | + | |
| 2683 | + | |
| 2684 | + | |
| 2685 | + | |
| 2686 | + | |
| 2687 | + | |
| 2688 | + | |
| 2689 | + | |
| 2690 | + | |
| 2691 | + | |
| 2692 | + | |
| 2693 | + | |
| 2694 | + | |
| 2695 | + | |
| 2696 | + | |
| 2697 | + | |
| 2698 | + | |
| 2699 | + | |
| 2700 | + | |
| 2701 | + | |
| 2702 | + | |
| 2703 | + | |
| 2704 | + | |
| 2705 | + | |
| 2706 | + | |
| 2707 | + | |
| 2708 | + | |
| 2709 | + | |
| 2710 | + | |
| 2711 | + | |
| 2712 | + | |
| 2713 | + | |
| 2714 | + | |
| 2715 | + | |
| 2716 | + | |
| 2717 | + | |
| 2718 | + | |
| 2719 | + | |
| 2720 | + | |
| 2721 | + | |
| 2722 | + | |
| 2723 | + | |
| 2724 | + | |
| 2725 | + | |
| 2726 | + | |
| 2727 | + | |
| 2728 | + | |
| 2729 | + | |
| 2730 | + | |
| 2731 | + | |
| 2732 | + | |
| 2733 | + | |
| 2734 | + | |
| 2735 | + | |
| 2736 | + | |
| 2737 | + | |
| 2738 | + | |
| 2739 | + | |
| 2740 | + | |
| 2741 | + | |
| 2742 | + | |
| 2743 | + | |
| 2744 | + | |
| 2745 | + | |
| 2746 | + | |
| 2747 | + | |
| 2748 | + | |
| 2749 | + | |
| 2750 | + | |
| 2751 | + | |
| 2752 | + | |
| 2753 | + | |
| 2754 | + | |
| 2755 | + | |
| 2756 | + | |
| 2757 | + | |
| 2758 | + |
Lines changed: 56 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
1902 | 1902 |
| |
1903 | 1903 |
| |
1904 | 1904 |
| |
| 1905 | + | |
| 1906 | + | |
| 1907 | + | |
| 1908 | + | |
| 1909 | + | |
| 1910 | + | |
| 1911 | + | |
| 1912 | + | |
| 1913 | + | |
| 1914 | + | |
| 1915 | + | |
| 1916 | + | |
| 1917 | + | |
| 1918 | + | |
| 1919 | + | |
| 1920 | + | |
| 1921 | + | |
| 1922 | + | |
| 1923 | + | |
| 1924 | + | |
| 1925 | + | |
| 1926 | + | |
| 1927 | + | |
| 1928 | + | |
| 1929 | + | |
| 1930 | + | |
| 1931 | + | |
| 1932 | + | |
| 1933 | + | |
| 1934 | + | |
| 1935 | + | |
| 1936 | + | |
| 1937 | + | |
| 1938 | + | |
| 1939 | + | |
| 1940 | + | |
| 1941 | + | |
| 1942 | + | |
| 1943 | + | |
| 1944 | + | |
| 1945 | + | |
| 1946 | + | |
| 1947 | + | |
| 1948 | + | |
| 1949 | + | |
| 1950 | + | |
| 1951 | + | |
| 1952 | + | |
| 1953 | + | |
| 1954 | + | |
| 1955 | + | |
| 1956 | + | |
| 1957 | + | |
| 1958 | + | |
| 1959 | + | |
| 1960 | + |
0 commit comments
Comments
(0)