Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 0554fb7

Browse files
committed
added create_foreign_key() func to the documentation; added tests for attach_range_partition() and replace_hash_partition()
1 parent 1b883a8 commit 0554fb7

File tree

7 files changed

+57
-499
lines changed

7 files changed

+57
-499
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,14 @@ set_set_spawn_using_bgw(relation REGCLASS, value BOOLEAN)
290290
```
291291
When INSERTing new data beyond the partitioning range, use SpawnPartitionsWorker to create new partitions in a separate transaction.
292292

293+
```plpgsql
294+
create_foreign_key(fk_table REGCLASS,
295+
fk_attr TEXT,
296+
pk_table REGCLASS)
297+
```
298+
Create a foreign key constraint on `fk_table` for `fk_attr` column which references partitioning key on partitioned `pk_table`. `pk_table` must have UNIQUE indexes on parent and all partitions for key column. This constraint implements RESTRICT behaviour ON DELETE and ON UPDATE actions. You can remove FK constraint regular way by performing `ALTER TABLE ... DROP CONSTRAINT ...`
299+
300+
293301
## Views and tables
294302

295303
#### `pathman_config` --- main config storage

README.rus.md

Lines changed: 0 additions & 493 deletions
This file was deleted.

expected/pathman_ri.out

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,11 @@ SELECT append_range_partition('abc');
7777

7878
INSERT INTO abc VALUES (350);
7979
INSERT INTO xxx (abc_id) VALUES (350);
80-
/* Partition cannot be dropped unless there are references from FK table */
80+
/* Partition cannot be dropped or detached unless there are references from FK table */
8181
DROP TABLE abc_4;
8282
ERROR: update or delete on table "abc_4" violates foreign key constraint "xxx_abc_id_fkey" on table "xxx"
83+
SELECT detach_range_partition('abc_4');
84+
ERROR: update or delete on table "abc_4" violates foreign key constraint "xxx_abc_id_fkey" on table "xxx"
8385
/* Successful partition drop */
8486
DELETE FROM xxx WHERE abc_id = 350;
8587
DROP TABLE abc_4;
@@ -102,5 +104,31 @@ SELECT add_range_partition('abc', 301, 401, 'abc_x');
102104
abc_x
103105
(1 row)
104106

107+
/* Check replace_hash_partition() func */
108+
ALTER TABLE xxx DROP CONSTRAINT xxx_abc_id_fkey;
109+
DELETE FROM xxx;
110+
SELECT drop_partitions('abc', true);
111+
drop_partitions
112+
-----------------
113+
4
114+
(1 row)
115+
116+
SELECT create_hash_partitions('abc', 'id', 3);
117+
create_hash_partitions
118+
------------------------
119+
3
120+
(1 row)
121+
122+
SELECT create_foreign_key('xxx', 'abc_id', 'abc');
123+
create_foreign_key
124+
--------------------
125+
126+
(1 row)
127+
128+
INSERT INTO abc SELECT generate_series(1, 10);
129+
INSERT INTO xxx (abc_id) SELECT generate_series(1, 10);
130+
CREATE TABLE abc_x (LIKE abc INCLUDING ALL);
131+
SELECT replace_hash_partition('abc_1', 'abc_x');
132+
ERROR: update or delete on table "abc_1" violates foreign key constraint "xxx_abc_id_fkey" on table "xxx"
105133
DROP EXTENSION pg_pathman CASCADE;
106-
NOTICE: drop cascades to constraint xxx_abc_id_fkey on table xxx
134+
NOTICE: drop cascades to 4 other objects

hash.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ BEGIN
130130
INTO old_constr_def;
131131

132132
/* Detach old partition */
133+
PERFORM @extschema@.prepare_partition_drop(parent_relid, old_partition);
133134
EXECUTE format('ALTER TABLE %s NO INHERIT %s', old_partition, parent_relid);
134135
EXECUTE format('ALTER TABLE %s DROP CONSTRAINT %s',
135136
old_partition,

init.sql

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -613,8 +613,6 @@ BEGIN
613613
RETURNING *)
614614
SELECT count(*) from config_num_deleted INTO conf_num_del;
615615

616-
DELETE FROM @extschema@.pathman_config_params WHERE partrel = parent_relid;
617-
618616
IF conf_num_del = 0 THEN
619617
RAISE EXCEPTION 'relation "%" has no partitions', parent_relid::TEXT;
620618
END IF;
@@ -638,6 +636,8 @@ BEGIN
638636
WHERE oid = v_rec.tbl
639637
INTO v_relkind;
640638

639+
PERFORM @extschema@.prepare_partition_drop(parent_relid, v_rec.tbl);
640+
641641
/*
642642
* Determine the kind of child relation. It can be either regular
643643
* table (r) or foreign table (f). Depending on relkind we use
@@ -652,6 +652,8 @@ BEGIN
652652
v_part_count := v_part_count + 1;
653653
END LOOP;
654654

655+
DELETE FROM @extschema@.pathman_config_params WHERE partrel = parent_relid;
656+
655657
/* Notify backend about changes */
656658
PERFORM @extschema@.on_remove_partitions(parent_relid);
657659

sql/pathman_ri.sql

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,9 @@ SELECT append_range_partition('abc');
5555
INSERT INTO abc VALUES (350);
5656
INSERT INTO xxx (abc_id) VALUES (350);
5757

58-
/* Partition cannot be dropped unless there are references from FK table */
58+
/* Partition cannot be dropped or detached unless there are references from FK table */
5959
DROP TABLE abc_4;
60+
SELECT detach_range_partition('abc_4');
6061

6162
/* Successful partition drop */
6263
DELETE FROM xxx WHERE abc_id = 350;
@@ -67,4 +68,15 @@ SELECT merge_range_partitions('abc_2', 'abc_3');
6768
SELECT split_range_partition('abc_2', 200);
6869
SELECT add_range_partition('abc', 301, 401, 'abc_x');
6970

71+
/* Check replace_hash_partition() func */
72+
ALTER TABLE xxx DROP CONSTRAINT xxx_abc_id_fkey;
73+
DELETE FROM xxx;
74+
SELECT drop_partitions('abc', true);
75+
SELECT create_hash_partitions('abc', 'id', 3);
76+
SELECT create_foreign_key('xxx', 'abc_id', 'abc');
77+
INSERT INTO abc SELECT generate_series(1, 10);
78+
INSERT INTO xxx (abc_id) SELECT generate_series(1, 10);
79+
CREATE TABLE abc_x (LIKE abc INCLUDING ALL);
80+
SELECT replace_hash_partition('abc_1', 'abc_x');
81+
7082
DROP EXTENSION pg_pathman CASCADE;

src/ref_integrity.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
#include "catalog/indexing.h"
2020
#include "catalog/pg_am.h"
2121
#include "catalog/pg_constraint.h"
22-
#if PG_VERSION_NUM >= 90600
2322
/* Constraint function were moved to pg_constraint_fn.h in version 9.6 */
23+
#if PG_VERSION_NUM >= 90600
2424
#include "catalog/pg_constraint_fn.h"
2525
#endif
2626
#include "catalog/pg_type.h"

0 commit comments

Comments
 (0)