Skip to content

Commit 1277897

Browse files
committed
constraint renaming and tests
1 parent cd3b01e commit 1277897

File tree

10 files changed

+259
-19
lines changed

10 files changed

+259
-19
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ MODULE_big = pg_pathman
44
OBJS = src/init.o src/relation_info.o src/utils.o src/partition_filter.o \
55
src/runtimeappend.o src/runtime_merge_append.o src/pg_pathman.o src/rangeset.o \
66
src/pl_funcs.o src/pl_range_funcs.o src/pl_hash_funcs.o src/pathman_workers.o \
7-
src/hooks.o src/nodes_common.o src/xact_handling.o src/copy_stmt_hooking.o \
7+
src/hooks.o src/nodes_common.o src/xact_handling.o src/utility_stmt_hooking.o \
88
src/planner_tree_modification.o src/debug_print.o src/pg_compat.o $(WIN32RES)
99

1010
EXTENSION = pg_pathman
@@ -20,7 +20,7 @@ REGRESS = pathman_basic \
2020
pathman_foreign_keys \
2121
pathman_permissions \
2222
pathman_rowmarks \
23-
pathman_copy_stmt_hooking \
23+
pathman_utility_stmt_hooking \
2424
pathman_calamity
2525
EXTRA_REGRESS_OPTS=--temp-config=$(top_srcdir)/$(subdir)/conf.add
2626
EXTRA_CLEAN = pg_pathman--$(EXTVERSION).sql ./isolation_output

expected/pathman_copy_stmt_hooking.out renamed to expected/pathman_utility_stmt_hooking.out

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
\set VERBOSITY terse
22
CREATE EXTENSION pg_pathman;
3+
/*
4+
* Test COPY
5+
*/
36
CREATE SCHEMA copy_stmt_hooking;
47
CREATE TABLE copy_stmt_hooking.test(
58
val int not null,
@@ -190,4 +193,92 @@ SELECT * FROM copy_stmt_hooking.test ORDER BY val;
190193

191194
DROP SCHEMA copy_stmt_hooking CASCADE;
192195
NOTICE: drop cascades to 7 other objects
196+
/*
197+
* Test auto check constraint renaming
198+
*/
199+
CREATE SCHEMA rename;
200+
CREATE TABLE rename.test(a serial, b int);
201+
SELECT create_hash_partitions('rename.test', 'a', 3);
202+
create_hash_partitions
203+
------------------------
204+
3
205+
(1 row)
206+
207+
ALTER TABLE rename.test_0 RENAME TO test_one;
208+
/* We expect to find check constraint renamed as well */
209+
\d+ rename.test_one
210+
Table "rename.test_one"
211+
Column | Type | Modifiers | Storage | Stats target | Description
212+
--------+---------+---------------------------------------------------------+---------+--------------+-------------
213+
a | integer | not null default nextval('rename.test_a_seq'::regclass) | plain | |
214+
b | integer | | plain | |
215+
Check constraints:
216+
"pathman_test_one_1_check" CHECK (get_hash_part_idx(hashint4(a), 3) = 0)
217+
Inherits: rename.test
218+
219+
/* Generates check constraint for relation */
220+
CREATE OR REPLACE FUNCTION add_constraint(rel regclass, att text)
221+
RETURNS VOID AS $$
222+
declare
223+
constraint_name text := build_check_constraint_name(rel, 'a');
224+
BEGIN
225+
EXECUTE format('ALTER TABLE %s ADD CONSTRAINT %s CHECK (a < 100);',
226+
rel, constraint_name);
227+
END
228+
$$
229+
LANGUAGE plpgsql;
230+
/*
231+
* Check that it doesn't affect regular inherited tables that aren't managed
232+
* by pg_pathman
233+
*/
234+
CREATE TABLE rename.test_inh (LIKE rename.test INCLUDING ALL);
235+
CREATE TABLE rename.test_inh_1 (LIKE rename.test INCLUDING ALL);
236+
ALTER TABLE rename.test_inh_1 INHERIT rename.test_inh;
237+
SELECT add_constraint('rename.test_inh_1', 'a');
238+
add_constraint
239+
----------------
240+
241+
(1 row)
242+
243+
ALTER TABLE rename.test_inh_1 RENAME TO test_inh_one;
244+
\d+ rename.test_inh_one
245+
Table "rename.test_inh_one"
246+
Column | Type | Modifiers | Storage | Stats target | Description
247+
--------+---------+---------------------------------------------------------+---------+--------------+-------------
248+
a | integer | not null default nextval('rename.test_a_seq'::regclass) | plain | |
249+
b | integer | | plain | |
250+
Check constraints:
251+
"pathman_test_inh_1_1_check" CHECK (a < 100)
252+
Inherits: rename.test_inh
253+
254+
/* Check that plain tables are not affected too */
255+
CREATE TABLE rename.plain_test(a serial, b int);
256+
ALTER TABLE rename.plain_test RENAME TO plain_test_renamed;
257+
SELECT add_constraint('rename.plain_test_renamed', 'a');
258+
add_constraint
259+
----------------
260+
261+
(1 row)
262+
263+
\d+ rename.plain_test_renamed
264+
Table "rename.plain_test_renamed"
265+
Column | Type | Modifiers | Storage | Stats target | Description
266+
--------+---------+---------------------------------------------------------------+---------+--------------+-------------
267+
a | integer | not null default nextval('rename.plain_test_a_seq'::regclass) | plain | |
268+
b | integer | | plain | |
269+
Check constraints:
270+
"pathman_plain_test_renamed_1_check" CHECK (a < 100)
271+
272+
ALTER TABLE rename.plain_test_renamed RENAME TO plain_test;
273+
\d+ rename.plain_test
274+
Table "rename.plain_test"
275+
Column | Type | Modifiers | Storage | Stats target | Description
276+
--------+---------+---------------------------------------------------------------+---------+--------------+-------------
277+
a | integer | not null default nextval('rename.plain_test_a_seq'::regclass) | plain | |
278+
b | integer | | plain | |
279+
Check constraints:
280+
"pathman_plain_test_renamed_1_check" CHECK (a < 100)
281+
282+
DROP SCHEMA rename CASCADE;
283+
NOTICE: drop cascades to 7 other objects
193284
DROP EXTENSION pg_pathman;

sql/pathman_copy_stmt_hooking.sql renamed to sql/pathman_utility_stmt_hooking.sql

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
\set VERBOSITY terse
22

33
CREATE EXTENSION pg_pathman;
4-
CREATE SCHEMA copy_stmt_hooking;
5-
64

5+
/*
6+
* Test COPY
7+
*/
8+
CREATE SCHEMA copy_stmt_hooking;
79
CREATE TABLE copy_stmt_hooking.test(
810
val int not null,
911
comment text,
@@ -89,6 +91,50 @@ COPY copy_stmt_hooking.test FROM stdin;
8991
SELECT count(*) FROM ONLY copy_stmt_hooking.test;
9092
SELECT * FROM copy_stmt_hooking.test ORDER BY val;
9193

92-
9394
DROP SCHEMA copy_stmt_hooking CASCADE;
95+
96+
/*
97+
* Test auto check constraint renaming
98+
*/
99+
CREATE SCHEMA rename;
100+
101+
CREATE TABLE rename.test(a serial, b int);
102+
SELECT create_hash_partitions('rename.test', 'a', 3);
103+
ALTER TABLE rename.test_0 RENAME TO test_one;
104+
/* We expect to find check constraint renamed as well */
105+
\d+ rename.test_one
106+
107+
/* Generates check constraint for relation */
108+
CREATE OR REPLACE FUNCTION add_constraint(rel regclass, att text)
109+
RETURNS VOID AS $$
110+
declare
111+
constraint_name text := build_check_constraint_name(rel, 'a');
112+
BEGIN
113+
EXECUTE format('ALTER TABLE %s ADD CONSTRAINT %s CHECK (a < 100);',
114+
rel, constraint_name);
115+
END
116+
$$
117+
LANGUAGE plpgsql;
118+
119+
/*
120+
* Check that it doesn't affect regular inherited tables that aren't managed
121+
* by pg_pathman
122+
*/
123+
CREATE TABLE rename.test_inh (LIKE rename.test INCLUDING ALL);
124+
CREATE TABLE rename.test_inh_1 (LIKE rename.test INCLUDING ALL);
125+
ALTER TABLE rename.test_inh_1 INHERIT rename.test_inh;
126+
SELECT add_constraint('rename.test_inh_1', 'a');
127+
ALTER TABLE rename.test_inh_1 RENAME TO test_inh_one;
128+
\d+ rename.test_inh_one
129+
130+
/* Check that plain tables are not affected too */
131+
CREATE TABLE rename.plain_test(a serial, b int);
132+
ALTER TABLE rename.plain_test RENAME TO plain_test_renamed;
133+
SELECT add_constraint('rename.plain_test_renamed', 'a');
134+
\d+ rename.plain_test_renamed
135+
ALTER TABLE rename.plain_test_renamed RENAME TO plain_test;
136+
\d+ rename.plain_test
137+
138+
DROP SCHEMA rename CASCADE;
139+
94140
DROP EXTENSION pg_pathman;

src/hooks.c

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* ------------------------------------------------------------------------
99
*/
1010

11-
#include "copy_stmt_hooking.h"
11+
#include "utility_stmt_hooking.h"
1212
#include "hooks.h"
1313
#include "init.h"
1414
#include "partition_filter.h"
@@ -24,6 +24,7 @@
2424
#include "optimizer/cost.h"
2525
#include "optimizer/restrictinfo.h"
2626
#include "utils/typcache.h"
27+
#include "utils/lsyscache.h"
2728

2829

2930
set_join_pathlist_hook_type set_join_pathlist_next = NULL;
@@ -34,6 +35,10 @@ shmem_startup_hook_type shmem_startup_hook_next = NULL;
3435
ProcessUtility_hook_type process_utility_hook_next = NULL;
3536

3637

38+
#define is_table_rename_statement(s) \
39+
IsA((s), RenameStmt) && ((RenameStmt *)(s))->renameType == OBJECT_TABLE
40+
41+
3742
/* Take care of joins */
3843
void
3944
pathman_join_pathlist_hook(PlannerInfo *root,
@@ -627,17 +632,29 @@ pathman_process_utility_hook(Node *parsetree,
627632
char *completionTag)
628633
{
629634
/* Override standard COPY statement if needed */
630-
if (IsPathmanReady() && is_pathman_related_copy(parsetree))
635+
if (IsPathmanReady())
631636
{
632-
uint64 processed;
637+
if (is_pathman_related_copy(parsetree))
638+
{
639+
uint64 processed;
640+
641+
/* Handle our COPY case (and show a special cmd name) */
642+
PathmanDoCopy((CopyStmt *) parsetree, queryString, &processed);
643+
if (completionTag)
644+
snprintf(completionTag, COMPLETION_TAG_BUFSIZE,
645+
"PATHMAN COPY " UINT64_FORMAT, processed);
633646

634-
/* Handle our COPY case (and show a special cmd name) */
635-
PathmanDoCopy((CopyStmt *) parsetree, queryString, &processed);
636-
if (completionTag)
637-
snprintf(completionTag, COMPLETION_TAG_BUFSIZE,
638-
"PATHMAN COPY " UINT64_FORMAT, processed);
647+
return; /* don't call standard_ProcessUtility() or hooks */
648+
}
639649

640-
return; /* don't call standard_ProcessUtility() or hooks */
650+
if (is_table_rename_statement(parsetree))
651+
{
652+
/*
653+
* Rename check constraint of a table if it is a partition managed
654+
* by pg_pathman
655+
*/
656+
PathmanDoRenameConstraint((RenameStmt *) parsetree);
657+
}
641658
}
642659

643660
/* Call hooks set by other extensions if needed */

src/init.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ find_inheritance_children_array(Oid parentrelId,
598598
char *
599599
build_check_constraint_name_internal(Oid relid, AttrNumber attno)
600600
{
601-
return psprintf("pathman_%s_%u_check", get_rel_name(relid), attno);
601+
return build_check_constraint_name_by_relname(get_rel_name(relid), attno);
602602
}
603603

604604
/*

src/init.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,15 @@ extern PathmanInitState pg_pathman_init_state;
8686
pg_pathman_init_state.initialization_needed = true; \
8787
} while (0)
8888

89+
/*
90+
* Generate check constraint name for given relname
91+
*/
92+
static inline char *
93+
build_check_constraint_name_by_relname(char *relname, AttrNumber attno)
94+
{
95+
return psprintf("pathman_%s_%u_check", relname, attno);
96+
}
97+
8998

9099
/*
91100
* Save and restore PathmanInitState.

src/copy_stmt_hooking.c renamed to src/utility_stmt_hooking.c

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/* ------------------------------------------------------------------------
22
*
3-
* copy_stmt_hooking.c
4-
* Override COPY TO/FROM statement for partitioned tables
3+
* utility_stmt_hooking.c
4+
* Override COPY TO/FROM and ALTER TABLE ... RENAME statements
5+
* for partitioned tables
56
*
67
* Copyright (c) 2016, Postgres Professional
78
* Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group
@@ -10,7 +11,7 @@
1011
* ------------------------------------------------------------------------
1112
*/
1213

13-
#include "copy_stmt_hooking.h"
14+
#include "utility_stmt_hooking.h"
1415
#include "init.h"
1516
#include "partition_filter.h"
1617
#include "relation_info.h"
@@ -22,6 +23,7 @@
2223
#include "catalog/pg_attribute.h"
2324
#include "commands/copy.h"
2425
#include "commands/trigger.h"
26+
#include "commands/tablecmds.h"
2527
#include "executor/executor.h"
2628
#include "foreign/fdwapi.h"
2729
#include "miscadmin.h"
@@ -622,3 +624,46 @@ prepare_rri_fdw_for_copy(EState *estate,
622624
elog(ERROR, "cannot copy to foreign partition \"%s\"",
623625
get_rel_name(RelationGetRelid(rri->ri_RelationDesc)));
624626
}
627+
628+
/*
629+
* Rename check constraint of table if it is a partition
630+
*/
631+
void
632+
PathmanDoRenameConstraint(const RenameStmt *stmt)
633+
{
634+
Oid partition = RangeVarGetRelid(stmt->relation, NoLock, true);
635+
Oid parent = get_rel_parent(partition);
636+
637+
if (partition != InvalidOid && parent != InvalidOid)
638+
{
639+
char *old_constraint_name,
640+
*new_constraint_name;
641+
const PartRelationInfo *prel = get_pathman_relation_info(parent);
642+
643+
if (prel)
644+
{
645+
RangeVar *rngVar;
646+
RenameStmt *s;
647+
648+
/* Generate old constraint name */
649+
old_constraint_name = build_check_constraint_name_by_relname(
650+
get_rel_name(partition),
651+
prel->attnum);
652+
653+
/* Generate new constraint name */
654+
new_constraint_name = build_check_constraint_name_by_relname(
655+
stmt->newname,
656+
prel->attnum);
657+
658+
/* Build check constraint RENAME statement */
659+
s = makeNode(RenameStmt);
660+
s->renameType = OBJECT_TABCONSTRAINT;
661+
s->relation = stmt->relation;
662+
s->subname = old_constraint_name;
663+
s->newname = new_constraint_name;
664+
s->missing_ok = false;
665+
666+
RenameConstraint(s);
667+
}
668+
}
669+
}

src/copy_stmt_hooking.h renamed to src/utility_stmt_hooking.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* ------------------------------------------------------------------------
22
*
3-
* copy_stmt_hooking.h
3+
* utility_stmt_hooking.h
44
* Transaction-specific locks and other functions
55
*
66
* Copyright (c) 2016, Postgres Professional
@@ -19,5 +19,6 @@
1919

2020
bool is_pathman_related_copy(Node *parsetree);
2121
void PathmanDoCopy(const CopyStmt *stmt, const char *queryString, uint64 *processed);
22+
void PathmanDoRenameConstraint(const RenameStmt *stmt);
2223

2324
#endif

0 commit comments

Comments
 (0)