@@ -2409,3 +2409,126 @@ reset session authorization;
2409
2409
revoke all on permtest_parent from regress_no_child_access;
2410
2410
drop role regress_no_child_access;
2411
2411
drop table permtest_parent;
2412
+ -- Verify that constraint errors across partition root / child are
2413
+ -- handled correctly (Bug #16293)
2414
+ CREATE TABLE errtst_parent (
2415
+ partid int not null,
2416
+ shdata int not null,
2417
+ data int NOT NULL DEFAULT 0,
2418
+ CONSTRAINT shdata_small CHECK(shdata < 3)
2419
+ ) PARTITION BY RANGE (partid);
2420
+ -- fast defaults lead to attribute mapping being used in one
2421
+ -- direction, but not the other
2422
+ CREATE TABLE errtst_child_fastdef (
2423
+ partid int not null,
2424
+ shdata int not null,
2425
+ CONSTRAINT shdata_small CHECK(shdata < 3)
2426
+ );
2427
+ -- no remapping in either direction necessary
2428
+ CREATE TABLE errtst_child_plaindef (
2429
+ partid int not null,
2430
+ shdata int not null,
2431
+ data int NOT NULL DEFAULT 0,
2432
+ CONSTRAINT shdata_small CHECK(shdata < 3),
2433
+ CHECK(data < 10)
2434
+ );
2435
+ -- remapping in both direction
2436
+ CREATE TABLE errtst_child_reorder (
2437
+ data int NOT NULL DEFAULT 0,
2438
+ shdata int not null,
2439
+ partid int not null,
2440
+ CONSTRAINT shdata_small CHECK(shdata < 3),
2441
+ CHECK(data < 10)
2442
+ );
2443
+ ALTER TABLE errtst_child_fastdef ADD COLUMN data int NOT NULL DEFAULT 0;
2444
+ ALTER TABLE errtst_child_fastdef ADD CONSTRAINT errtest_child_fastdef_data_check CHECK (data < 10);
2445
+ ALTER TABLE errtst_parent ATTACH PARTITION errtst_child_fastdef FOR VALUES FROM (0) TO (10);
2446
+ ALTER TABLE errtst_parent ATTACH PARTITION errtst_child_plaindef FOR VALUES FROM (10) TO (20);
2447
+ ALTER TABLE errtst_parent ATTACH PARTITION errtst_child_reorder FOR VALUES FROM (20) TO (30);
2448
+ -- insert without child check constraint error
2449
+ INSERT INTO errtst_parent(partid, shdata, data) VALUES ( '0', '1', '5');
2450
+ INSERT INTO errtst_parent(partid, shdata, data) VALUES ('10', '1', '5');
2451
+ INSERT INTO errtst_parent(partid, shdata, data) VALUES ('20', '1', '5');
2452
+ -- insert with child check constraint error
2453
+ INSERT INTO errtst_parent(partid, shdata, data) VALUES ( '0', '1', '10');
2454
+ ERROR: new row for relation "errtst_child_fastdef" violates check constraint "errtest_child_fastdef_data_check"
2455
+ DETAIL: Failing row contains (0, 1, 10).
2456
+ INSERT INTO errtst_parent(partid, shdata, data) VALUES ('10', '1', '10');
2457
+ ERROR: new row for relation "errtst_child_plaindef" violates check constraint "errtst_child_plaindef_data_check"
2458
+ DETAIL: Failing row contains (10, 1, 10).
2459
+ INSERT INTO errtst_parent(partid, shdata, data) VALUES ('20', '1', '10');
2460
+ ERROR: new row for relation "errtst_child_reorder" violates check constraint "errtst_child_reorder_data_check"
2461
+ DETAIL: Failing row contains (20, 1, 10).
2462
+ -- insert with child not null constraint error
2463
+ INSERT INTO errtst_parent(partid, shdata, data) VALUES ( '0', '1', NULL);
2464
+ ERROR: null value in column "data" of relation "errtst_child_fastdef" violates not-null constraint
2465
+ DETAIL: Failing row contains (0, 1, null).
2466
+ INSERT INTO errtst_parent(partid, shdata, data) VALUES ('10', '1', NULL);
2467
+ ERROR: null value in column "data" of relation "errtst_child_plaindef" violates not-null constraint
2468
+ DETAIL: Failing row contains (10, 1, null).
2469
+ INSERT INTO errtst_parent(partid, shdata, data) VALUES ('20', '1', NULL);
2470
+ ERROR: null value in column "data" of relation "errtst_child_reorder" violates not-null constraint
2471
+ DETAIL: Failing row contains (20, 1, null).
2472
+ -- insert with shared check constraint error
2473
+ INSERT INTO errtst_parent(partid, shdata, data) VALUES ( '0', '5', '5');
2474
+ ERROR: new row for relation "errtst_child_fastdef" violates check constraint "shdata_small"
2475
+ DETAIL: Failing row contains (0, 5, 5).
2476
+ INSERT INTO errtst_parent(partid, shdata, data) VALUES ('10', '5', '5');
2477
+ ERROR: new row for relation "errtst_child_plaindef" violates check constraint "shdata_small"
2478
+ DETAIL: Failing row contains (10, 5, 5).
2479
+ INSERT INTO errtst_parent(partid, shdata, data) VALUES ('20', '5', '5');
2480
+ ERROR: new row for relation "errtst_child_reorder" violates check constraint "shdata_small"
2481
+ DETAIL: Failing row contains (20, 5, 5).
2482
+ -- within partition update without child check constraint violation
2483
+ BEGIN;
2484
+ UPDATE errtst_parent SET data = data + 1 WHERE partid = 0;
2485
+ UPDATE errtst_parent SET data = data + 1 WHERE partid = 10;
2486
+ UPDATE errtst_parent SET data = data + 1 WHERE partid = 20;
2487
+ ROLLBACK;
2488
+ -- within partition update with child check constraint violation
2489
+ UPDATE errtst_parent SET data = data + 10 WHERE partid = 0;
2490
+ ERROR: new row for relation "errtst_child_fastdef" violates check constraint "errtest_child_fastdef_data_check"
2491
+ DETAIL: Failing row contains (0, 1, 15).
2492
+ UPDATE errtst_parent SET data = data + 10 WHERE partid = 10;
2493
+ ERROR: new row for relation "errtst_child_plaindef" violates check constraint "errtst_child_plaindef_data_check"
2494
+ DETAIL: Failing row contains (10, 1, 15).
2495
+ UPDATE errtst_parent SET data = data + 10 WHERE partid = 20;
2496
+ ERROR: new row for relation "errtst_child_reorder" violates check constraint "errtst_child_reorder_data_check"
2497
+ DETAIL: Failing row contains (15, 1, 20).
2498
+ -- direct leaf partition update, without partition id violation
2499
+ BEGIN;
2500
+ UPDATE errtst_child_fastdef SET partid = 1 WHERE partid = 0;
2501
+ UPDATE errtst_child_plaindef SET partid = 11 WHERE partid = 10;
2502
+ UPDATE errtst_child_reorder SET partid = 21 WHERE partid = 20;
2503
+ ROLLBACK;
2504
+ -- direct leaf partition update, with partition id violation
2505
+ UPDATE errtst_child_fastdef SET partid = partid + 10 WHERE partid = 0;
2506
+ ERROR: new row for relation "errtst_child_fastdef" violates partition constraint
2507
+ DETAIL: Failing row contains (10, 1, 5).
2508
+ UPDATE errtst_child_plaindef SET partid = partid + 10 WHERE partid = 10;
2509
+ ERROR: new row for relation "errtst_child_plaindef" violates partition constraint
2510
+ DETAIL: Failing row contains (20, 1, 5).
2511
+ UPDATE errtst_child_reorder SET partid = partid + 10 WHERE partid = 20;
2512
+ ERROR: new row for relation "errtst_child_reorder" violates partition constraint
2513
+ DETAIL: Failing row contains (5, 1, 30).
2514
+ -- partition move, without child check constraint violation
2515
+ BEGIN;
2516
+ UPDATE errtst_parent SET partid = 10, data = data + 1 WHERE partid = 0;
2517
+ UPDATE errtst_parent SET partid = 20, data = data + 1 WHERE partid = 10;
2518
+ UPDATE errtst_parent SET partid = 0, data = data + 1 WHERE partid = 20;
2519
+ ROLLBACK;
2520
+ -- partition move, with child check constraint violation
2521
+ UPDATE errtst_parent SET partid = 10, data = data + 10 WHERE partid = 0;
2522
+ ERROR: new row for relation "errtst_child_plaindef" violates check constraint "errtst_child_plaindef_data_check"
2523
+ DETAIL: Failing row contains (10, 1, 15).
2524
+ UPDATE errtst_parent SET partid = 20, data = data + 10 WHERE partid = 10;
2525
+ ERROR: new row for relation "errtst_child_reorder" violates check constraint "errtst_child_reorder_data_check"
2526
+ DETAIL: Failing row contains (20, 1, 15).
2527
+ UPDATE errtst_parent SET partid = 0, data = data + 10 WHERE partid = 20;
2528
+ ERROR: new row for relation "errtst_child_fastdef" violates check constraint "errtest_child_fastdef_data_check"
2529
+ DETAIL: Failing row contains (0, 1, 15).
2530
+ -- partition move, without target partition
2531
+ UPDATE errtst_parent SET partid = 30, data = data + 10 WHERE partid = 20;
2532
+ ERROR: no partition of relation "errtst_parent" found for row
2533
+ DETAIL: Partition key of the failing row contains (partid) = (30).
2534
+ DROP TABLE errtst_parent;
0 commit comments