@@ -2324,70 +2324,90 @@ CREATE TABLE part33_self_fk (
2324
2324
id_abc bigint
2325
2325
);
2326
2326
ALTER TABLE part3_self_fk ATTACH PARTITION part33_self_fk FOR VALUES FROM (30) TO (40);
2327
- SELECT cr.relname, co.conname, co.contype, co.convalidated,
2327
+ -- verify that this constraint works
2328
+ INSERT INTO parted_self_fk VALUES (1, NULL), (2, NULL), (3, NULL);
2329
+ INSERT INTO parted_self_fk VALUES (10, 1), (11, 2), (12, 3) RETURNING tableoid::regclass;
2330
+ tableoid
2331
+ ---------------
2332
+ part2_self_fk
2333
+ part2_self_fk
2334
+ part2_self_fk
2335
+ (3 rows)
2336
+
2337
+ INSERT INTO parted_self_fk VALUES (4, 5); -- error: referenced doesn't exist
2338
+ ERROR: insert or update on table "part1_self_fk" violates foreign key constraint "parted_self_fk_id_abc_fkey"
2339
+ DETAIL: Key (id_abc)=(5) is not present in table "parted_self_fk".
2340
+ DELETE FROM parted_self_fk WHERE id = 1 RETURNING *; -- error: reference remains
2341
+ ERROR: update or delete on table "part1_self_fk" violates foreign key constraint "parted_self_fk_id_abc_fkey_1" on table "parted_self_fk"
2342
+ DETAIL: Key (id)=(1) is still referenced from table "parted_self_fk".
2343
+ SELECT cr.relname, co.conname, co.convalidated,
2328
2344
p.conname AS conparent, p.convalidated, cf.relname AS foreignrel
2329
2345
FROM pg_constraint co
2330
2346
JOIN pg_class cr ON cr.oid = co.conrelid
2331
2347
LEFT JOIN pg_class cf ON cf.oid = co.confrelid
2332
2348
LEFT JOIN pg_constraint p ON p.oid = co.conparentid
2333
- WHERE cr.oid IN (SELECT relid FROM pg_partition_tree('parted_self_fk'))
2334
- ORDER BY co.contype, cr.relname, co.conname, p.conname;
2335
- relname | conname | contype | convalidated | conparent | convalidated | foreignrel
2336
- ----------------+----------------------------+---------+--------------+----------------------------+--------------+----------------
2337
- part1_self_fk | parted_self_fk_id_abc_fkey | f | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
2338
- part2_self_fk | parted_self_fk_id_abc_fkey | f | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
2339
- part32_self_fk | parted_self_fk_id_abc_fkey | f | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
2340
- part33_self_fk | parted_self_fk_id_abc_fkey | f | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
2341
- part3_self_fk | parted_self_fk_id_abc_fkey | f | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
2342
- parted_self_fk | parted_self_fk_id_abc_fkey | f | t | | | parted_self_fk
2343
- part1_self_fk | part1_self_fk_id_not_null | n | t | | |
2344
- part2_self_fk | parted_self_fk_id_not_null | n | t | | |
2345
- part32_self_fk | part3_self_fk_id_not_null | n | t | | |
2346
- part33_self_fk | part33_self_fk_id_not_null | n | t | | |
2347
- part3_self_fk | part3_self_fk_id_not_null | n | t | | |
2348
- parted_self_fk | parted_self_fk_id_not_null | n | t | | |
2349
- part1_self_fk | part1_self_fk_pkey | p | t | parted_self_fk_pkey | t |
2350
- part2_self_fk | part2_self_fk_pkey | p | t | parted_self_fk_pkey | t |
2351
- part32_self_fk | part32_self_fk_pkey | p | t | part3_self_fk_pkey | t |
2352
- part33_self_fk | part33_self_fk_pkey | p | t | part3_self_fk_pkey | t |
2353
- part3_self_fk | part3_self_fk_pkey | p | t | parted_self_fk_pkey | t |
2354
- parted_self_fk | parted_self_fk_pkey | p | t | | |
2355
- (18 rows)
2349
+ WHERE co.contype = 'f' AND
2350
+ cr.oid IN (SELECT relid FROM pg_partition_tree('parted_self_fk'))
2351
+ ORDER BY cr.relname, co.conname, p.conname;
2352
+ relname | conname | convalidated | conparent | convalidated | foreignrel
2353
+ ----------------+--------------------------------+--------------+------------------------------+--------------+----------------
2354
+ part1_self_fk | parted_self_fk_id_abc_fkey | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
2355
+ part2_self_fk | parted_self_fk_id_abc_fkey | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
2356
+ part32_self_fk | parted_self_fk_id_abc_fkey | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
2357
+ part33_self_fk | parted_self_fk_id_abc_fkey | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
2358
+ part3_self_fk | parted_self_fk_id_abc_fkey | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
2359
+ parted_self_fk | parted_self_fk_id_abc_fkey | t | | | parted_self_fk
2360
+ parted_self_fk | parted_self_fk_id_abc_fkey_1 | t | parted_self_fk_id_abc_fkey | t | part1_self_fk
2361
+ parted_self_fk | parted_self_fk_id_abc_fkey_2 | t | parted_self_fk_id_abc_fkey | t | part2_self_fk
2362
+ parted_self_fk | parted_self_fk_id_abc_fkey_3 | t | parted_self_fk_id_abc_fkey | t | part3_self_fk
2363
+ parted_self_fk | parted_self_fk_id_abc_fkey_3_1 | t | parted_self_fk_id_abc_fkey_3 | t | part33_self_fk
2364
+ parted_self_fk | parted_self_fk_id_abc_fkey_4 | t | parted_self_fk_id_abc_fkey_3 | t | part32_self_fk
2365
+ (11 rows)
2356
2366
2357
2367
-- detach and re-attach multiple times just to ensure everything is kosher
2358
2368
ALTER TABLE parted_self_fk DETACH PARTITION part2_self_fk;
2369
+ INSERT INTO part2_self_fk VALUES (16, 9); -- error: referenced doesn't exist
2370
+ ERROR: insert or update on table "part2_self_fk" violates foreign key constraint "parted_self_fk_id_abc_fkey"
2371
+ DETAIL: Key (id_abc)=(9) is not present in table "parted_self_fk".
2372
+ DELETE FROM parted_self_fk WHERE id = 2 RETURNING *; -- error: reference remains
2373
+ ERROR: update or delete on table "part1_self_fk" violates foreign key constraint "parted_self_fk_id_abc_fkey_5" on table "part2_self_fk"
2374
+ DETAIL: Key (id)=(2) is still referenced from table "part2_self_fk".
2359
2375
ALTER TABLE parted_self_fk ATTACH PARTITION part2_self_fk FOR VALUES FROM (10) TO (20);
2376
+ INSERT INTO parted_self_fk VALUES (16, 9); -- error: referenced doesn't exist
2377
+ ERROR: insert or update on table "part2_self_fk" violates foreign key constraint "parted_self_fk_id_abc_fkey"
2378
+ DETAIL: Key (id_abc)=(9) is not present in table "parted_self_fk".
2379
+ DELETE FROM parted_self_fk WHERE id = 3 RETURNING *; -- error: reference remains
2380
+ ERROR: update or delete on table "part1_self_fk" violates foreign key constraint "parted_self_fk_id_abc_fkey_1" on table "parted_self_fk"
2381
+ DETAIL: Key (id)=(3) is still referenced from table "parted_self_fk".
2360
2382
ALTER TABLE parted_self_fk DETACH PARTITION part2_self_fk;
2361
2383
ALTER TABLE parted_self_fk ATTACH PARTITION part2_self_fk FOR VALUES FROM (10) TO (20);
2362
- SELECT cr.relname, co.conname, co.contype, co.convalidated,
2384
+ ALTER TABLE parted_self_fk DETACH PARTITION part3_self_fk;
2385
+ ALTER TABLE parted_self_fk ATTACH PARTITION part3_self_fk FOR VALUES FROM (30) TO (40);
2386
+ ALTER TABLE part3_self_fk DETACH PARTITION part33_self_fk;
2387
+ ALTER TABLE part3_self_fk ATTACH PARTITION part33_self_fk FOR VALUES FROM (30) TO (40);
2388
+ SELECT cr.relname, co.conname, co.convalidated,
2363
2389
p.conname AS conparent, p.convalidated, cf.relname AS foreignrel
2364
2390
FROM pg_constraint co
2365
2391
JOIN pg_class cr ON cr.oid = co.conrelid
2366
2392
LEFT JOIN pg_class cf ON cf.oid = co.confrelid
2367
2393
LEFT JOIN pg_constraint p ON p.oid = co.conparentid
2368
- WHERE cr.oid IN (SELECT relid FROM pg_partition_tree('parted_self_fk'))
2369
- ORDER BY co.contype, cr.relname, co.conname, p.conname;
2370
- relname | conname | contype | convalidated | conparent | convalidated | foreignrel
2371
- ----------------+----------------------------+---------+--------------+----------------------------+--------------+----------------
2372
- part1_self_fk | parted_self_fk_id_abc_fkey | f | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
2373
- part2_self_fk | parted_self_fk_id_abc_fkey | f | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
2374
- part32_self_fk | parted_self_fk_id_abc_fkey | f | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
2375
- part33_self_fk | parted_self_fk_id_abc_fkey | f | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
2376
- part3_self_fk | parted_self_fk_id_abc_fkey | f | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
2377
- parted_self_fk | parted_self_fk_id_abc_fkey | f | t | | | parted_self_fk
2378
- part1_self_fk | part1_self_fk_id_not_null | n | t | | |
2379
- part2_self_fk | parted_self_fk_id_not_null | n | t | | |
2380
- part32_self_fk | part3_self_fk_id_not_null | n | t | | |
2381
- part33_self_fk | part33_self_fk_id_not_null | n | t | | |
2382
- part3_self_fk | part3_self_fk_id_not_null | n | t | | |
2383
- parted_self_fk | parted_self_fk_id_not_null | n | t | | |
2384
- part1_self_fk | part1_self_fk_pkey | p | t | parted_self_fk_pkey | t |
2385
- part2_self_fk | part2_self_fk_pkey | p | t | parted_self_fk_pkey | t |
2386
- part32_self_fk | part32_self_fk_pkey | p | t | part3_self_fk_pkey | t |
2387
- part33_self_fk | part33_self_fk_pkey | p | t | part3_self_fk_pkey | t |
2388
- part3_self_fk | part3_self_fk_pkey | p | t | parted_self_fk_pkey | t |
2389
- parted_self_fk | parted_self_fk_pkey | p | t | | |
2390
- (18 rows)
2394
+ WHERE co.contype = 'f' AND
2395
+ cr.oid IN (SELECT relid FROM pg_partition_tree('parted_self_fk'))
2396
+ ORDER BY cr.relname, co.conname, p.conname;
2397
+ relname | conname | convalidated | conparent | convalidated | foreignrel
2398
+ ----------------+--------------------------------+--------------+------------------------------+--------------+----------------
2399
+ part1_self_fk | parted_self_fk_id_abc_fkey | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
2400
+ part2_self_fk | parted_self_fk_id_abc_fkey | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
2401
+ part32_self_fk | parted_self_fk_id_abc_fkey | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
2402
+ part33_self_fk | parted_self_fk_id_abc_fkey | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
2403
+ part3_self_fk | parted_self_fk_id_abc_fkey | t | parted_self_fk_id_abc_fkey | t | parted_self_fk
2404
+ parted_self_fk | parted_self_fk_id_abc_fkey | t | | | parted_self_fk
2405
+ parted_self_fk | parted_self_fk_id_abc_fkey_1 | t | parted_self_fk_id_abc_fkey | t | part1_self_fk
2406
+ parted_self_fk | parted_self_fk_id_abc_fkey_2 | t | parted_self_fk_id_abc_fkey | t | part2_self_fk
2407
+ parted_self_fk | parted_self_fk_id_abc_fkey_3 | t | parted_self_fk_id_abc_fkey | t | part3_self_fk
2408
+ parted_self_fk | parted_self_fk_id_abc_fkey_3_1 | t | parted_self_fk_id_abc_fkey_3 | t | part33_self_fk
2409
+ parted_self_fk | parted_self_fk_id_abc_fkey_4 | t | parted_self_fk_id_abc_fkey_3 | t | part32_self_fk
2410
+ (11 rows)
2391
2411
2392
2412
-- Leave this table around, for pg_upgrade/pg_dump tests
2393
2413
-- Test creating a constraint at the parent that already exists in partitions.
0 commit comments