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

Commit 75fdbdf

Browse files
committed
remove dependencies on DROP TABLE
1 parent 8444775 commit 75fdbdf

File tree

8 files changed

+484
-67
lines changed

8 files changed

+484
-67
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ REGRESS = pathman_basic \
3838
pathman_rowmarks \
3939
pathman_runtime_nodes \
4040
pathman_utility_stmt \
41-
pathman_calamity
41+
pathman_calamity \
42+
pathman_ri
4243

4344
EXTRA_REGRESS_OPTS=--temp-config=$(top_srcdir)/$(subdir)/conf.add
4445

expected/pathman_ri.out

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
\set VERBOSITY terse
2+
CREATE EXTENSION pg_pathman;
3+
CREATE TABLE abc (id SERIAL);
4+
CREATE TABLE xxx (id SERIAL, abc_id INT);
5+
/* Cannot create FK if PK table isn't partitioned by pg_pathman */
6+
SELECT create_fk('xxx', 'abc_id', 'abc');
7+
ERROR: table abc isn't partitioned by pg_pathman
8+
/* Cannot create FK if there are no unique indexes */
9+
SELECT create_range_partitions('abc', 'id', 1, 100, 3);
10+
NOTICE: sequence "abc_seq" does not exist, skipping
11+
create_range_partitions
12+
-------------------------
13+
3
14+
(1 row)
15+
16+
SELECT create_fk('xxx', 'abc_id', 'abc');
17+
ERROR: there is no unique constraint matching given keys for referenced table "abc"
18+
/* Create index on parent but not on partitions. Expect an error */
19+
CREATE UNIQUE INDEX on abc(id);
20+
SELECT create_fk('xxx', 'abc_id', 'abc');
21+
ERROR: there is no unique constraint for partition "abc_1"
22+
/* Recreate partitioning setup with unique indexes */
23+
SELECT drop_partitions('abc');
24+
NOTICE: 0 rows copied from abc_1
25+
NOTICE: 0 rows copied from abc_2
26+
NOTICE: 0 rows copied from abc_3
27+
drop_partitions
28+
-----------------
29+
3
30+
(1 row)
31+
32+
CREATE UNIQUE INDEX on abc(id);
33+
SELECT create_range_partitions('abc', 'id', 1, 100, 3);
34+
create_range_partitions
35+
-------------------------
36+
3
37+
(1 row)
38+
39+
/* Existing row should violate FK constraint */
40+
INSERT INTO xxx (abc_id) VALUES (1);
41+
SELECT create_fk('xxx', 'abc_id', 'abc');
42+
ERROR: insert or update on table "xxx" violates foreign key constraint "xxx_abc_id_fkey"
43+
/* Expect successful execution */
44+
DELETE FROM xxx;
45+
SELECT create_fk('xxx', 'abc_id', 'abc');
46+
create_fk
47+
-----------
48+
49+
(1 row)
50+
51+
/* PK violation */
52+
INSERT INTO xxx (id, abc_id) VALUES (100, 1);
53+
ERROR: insert or update on table "xxx" violates foreign key constraint "xxx_abc_id_fkey"
54+
/* No violation */
55+
INSERT INTO abc VALUES (1);
56+
INSERT INTO xxx (id, abc_id) VALUES (100, 1);
57+
/* Restrict an update of FK table */
58+
UPDATE xxx SET abc_id=2 WHERE id=100;
59+
ERROR: insert or update on table "xxx" violates foreign key constraint "xxx_abc_id_fkey"
60+
/* No restriction */
61+
INSERT INTO abc VALUES (2);
62+
UPDATE xxx SET abc_id=2 WHERE id=100;
63+
/* Restrict an update or delete on PK table */
64+
UPDATE abc SET id = 3 WHERE id = 2;
65+
ERROR: update or delete on table "abc_1" violates foreign key constraint "xxx_abc_id_fkey" on table "xxx"
66+
DELETE FROM abc WHERE id = 2;
67+
ERROR: update or delete on table "abc_1" violates foreign key constraint "xxx_abc_id_fkey" on table "xxx"
68+
/* Unique indexes cannot be dropped */
69+
DROP INDEX abc_1_id_idx;
70+
ERROR: cannot drop index abc_1_id_idx because other objects depend on it
71+
/* Add partition */
72+
SELECT append_range_partition('abc');
73+
append_range_partition
74+
------------------------
75+
abc_4
76+
(1 row)
77+
78+
INSERT INTO abc VALUES (350);
79+
INSERT INTO xxx (abc_id) VALUES (350);
80+
DROP TABLE abc_4;
81+
DROP EXTENSION pg_pathman CASCADE;
82+
NOTICE: drop cascades to constraint xxx_abc_id_fkey on table xxx

sql/pathman_ri.sql

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,57 @@
33
CREATE EXTENSION pg_pathman;
44

55
CREATE TABLE abc (id SERIAL);
6-
CREATE TABLE xxx (id SERIAL);
6+
CREATE TABLE xxx (id SERIAL, abc_id INT);
77

8+
/* Cannot create FK if PK table isn't partitioned by pg_pathman */
9+
SELECT create_fk('xxx', 'abc_id', 'abc');
10+
11+
/* Cannot create FK if there are no unique indexes */
812
SELECT create_range_partitions('abc', 'id', 1, 100, 3);
9-
SELECT create_fk('xxx', 'id', 'abc', 'id');
13+
SELECT create_fk('xxx', 'abc_id', 'abc');
14+
15+
/* Create index on parent but not on partitions. Expect an error */
16+
CREATE UNIQUE INDEX on abc(id);
17+
SELECT create_fk('xxx', 'abc_id', 'abc');
18+
19+
/* Recreate partitioning setup with unique indexes */
20+
SELECT drop_partitions('abc');
21+
CREATE UNIQUE INDEX on abc(id);
22+
SELECT create_range_partitions('abc', 'id', 1, 100, 3);
23+
24+
/* Existing row should violate FK constraint */
25+
INSERT INTO xxx (abc_id) VALUES (1);
26+
SELECT create_fk('xxx', 'abc_id', 'abc');
1027

11-
INSERT INTO xxx VALUES (1);
28+
/* Expect successful execution */
29+
DELETE FROM xxx;
30+
SELECT create_fk('xxx', 'abc_id', 'abc');
1231

32+
/* PK violation */
33+
INSERT INTO xxx (id, abc_id) VALUES (100, 1);
34+
35+
/* No violation */
1336
INSERT INTO abc VALUES (1);
14-
INSERT INTO xxx VALUES (1);
37+
INSERT INTO xxx (id, abc_id) VALUES (100, 1);
38+
39+
/* Restrict an update of FK table */
40+
UPDATE xxx SET abc_id=2 WHERE id=100;
41+
42+
/* No restriction */
43+
INSERT INTO abc VALUES (2);
44+
UPDATE xxx SET abc_id=2 WHERE id=100;
45+
46+
/* Restrict an update or delete on PK table */
47+
UPDATE abc SET id = 3 WHERE id = 2;
48+
DELETE FROM abc WHERE id = 2;
49+
50+
/* Unique indexes cannot be dropped */
51+
DROP INDEX abc_1_id_idx;
1552

16-
UPDATE abc SET id=2 WHERE id=1;
17-
UPDATE xxx SET id=2 WHERE id=1;
53+
/* Add partition */
54+
SELECT append_range_partition('abc');
55+
INSERT INTO abc VALUES (350);
56+
INSERT INTO xxx (abc_id) VALUES (350);
57+
DROP TABLE abc_4;
1858

19-
DROP EXTENSION pg_pathman;
59+
DROP EXTENSION pg_pathman CASCADE;

src/hooks.c

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "xact_handling.h"
2727

2828
#include "access/transam.h"
29+
#include "access/xact.h"
2930
#include "catalog/dependency.h"
3031
#include "catalog/namespace.h"
3132
#include "catalog/objectaddress.h"
@@ -35,6 +36,10 @@
3536
#include "optimizer/restrictinfo.h"
3637
#include "utils/typcache.h"
3738
#include "utils/lsyscache.h"
39+
#include "utils/fmgroids.h"
40+
41+
#include "access/sysattr.h"
42+
#include "catalog/pg_trigger.h"
3843

3944

4045
set_join_pathlist_hook_type set_join_pathlist_next = NULL;
@@ -769,8 +774,9 @@ pathman_drop_fkeys(Node *parsetree)
769774
/* Walk through each table */
770775
foreach(cell, drop_stmt->objects)
771776
{
772-
RangeVar *rel = makeRangeVarFromNameList((List *) lfirst(cell));
773-
Oid relid = RangeVarGetRelid(rel, lockmode, true);
777+
RangeVar *rv = makeRangeVarFromNameList((List *) lfirst(cell));
778+
Oid relid = RangeVarGetRelid(rv, lockmode, true);
779+
Relation rel = heap_open(relid, lockmode);
774780

775781
if (!OidIsValid(relid))
776782
{
@@ -780,7 +786,7 @@ pathman_drop_fkeys(Node *parsetree)
780786
ereport(ERROR,
781787
(errcode(ERRCODE_UNDEFINED_TABLE),
782788
errmsg("table \"%s\" does not exist",
783-
rel->relname)));
789+
rv->relname)));
784790
}
785791

786792
/* Is it pg_pathman's table? */
@@ -805,7 +811,53 @@ pathman_drop_fkeys(Node *parsetree)
805811
performDeletion(&conobj, DROP_RESTRICT, 0);
806812

807813
}
814+
}
815+
/* Is it pg_pathman's partition? */
816+
else
817+
{
818+
PartParentSearch parent_search;
819+
Oid parent_relid;
820+
// Relation relation = heap_open(relid, NoLock);
821+
822+
/* Try fetching parent of this table */
823+
parent_relid = get_parent_of_partition(relid, &parent_search);
824+
if (parent_search == PPS_ENTRY_PART_PARENT)
825+
{
826+
ri_removePartitionDependencies(parent_relid, rel);
827+
// ri_removeTriggersDependency(parent_relid, relid);
828+
CommandCounterIncrement();
829+
830+
// {
831+
// const PartRelationInfo *prel;
832+
// const char * attname;
833+
// AttrNumber attnum;
834+
// Oid indexOid;
835+
// HeapTuple indexTuple;
836+
837+
// if ((prel = get_pathman_relation_info(parent_relid)) == NULL)
838+
// {
839+
// heap_close(relation, NoLock);
840+
// continue;
841+
// }
842+
843+
// attname = get_attname(parent_relid, prel->attnum);
844+
// attnum = get_attnum(relid, attname);
845+
// indexTuple = get_index_for_key(relation, attnum, &indexOid);
846+
847+
// /* If there is an index remove dependency */
848+
// if (HeapTupleIsValid(indexTuple))
849+
// {
850+
// deleteDependencyRecordsRef(RelationRelationId, indexOid);
851+
// ReleaseSysCache(indexTuple);
852+
853+
// /* Make changes visible */
854+
// CommandCounterIncrement();
855+
// }
856+
// }
857+
}
808858

859+
// heap_close(relation, NoLock);
809860
}
861+
heap_close(rel, lockmode);
810862
}
811863
}

src/include/ref_integrity.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
void pathman_get_fkeys(Oid parent_relid, List **constraints, List **refrelids);
44
void createSingleForeignKeyTrigger(Oid relOid, Oid refRelOid, List *funcname,
55
char *trigname, int16 events, Oid constraintOid,
6-
Oid indexOid);
6+
Oid indexOid, bool is_internal);
77
void createPartitionForeignKeyTriggers(Oid partition,
88
Oid fkrelid,
99
AttrNumber attnum,
1010
Oid constraintOid,
1111
List *upd_funcname,
12-
List *del_funcname);
12+
List *del_funcname);
13+
HeapTuple get_index_for_key(Relation rel, AttrNumber attnum, Oid *index_id);
14+
List *get_ri_triggers_list(Oid relid, Oid constr);
15+
void ri_removePartitionDependencies(Oid parent, Relation partition);

src/include/utils.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,12 @@ Datum extract_binary_interval_from_text(Datum interval_text,
6565
char **deconstruct_text_array(Datum array, int *array_size);
6666
RangeVar ** qualified_relnames_to_rangevars(char **relnames, size_t nrelnames);
6767

68+
/*
69+
* Objects dependency
70+
*/
71+
long deleteDependencyRecords(Oid classId, Oid objId,
72+
Oid refClassId, Oid refObjId);
73+
long deleteDependencyRecordsRef(Oid classId, Oid objectId);
74+
6875

6976
#endif /* PATHMAN_UTILS_H */

0 commit comments

Comments
 (0)