Skip to content

Commit bce18a5

Browse files
committed
rename: get_hash() -> get_hash_part_idx(), make_hash() -> hash_to_part_index(), parent_of_partition() -> get_parent_of_partition(), get_range_condition() -> build_range_condition(); qualify some catalog's relations, change behavior of get_schema_qualified_name() & validate_relname(), fixes
1 parent 1a0f23e commit bce18a5

File tree

9 files changed

+248
-214
lines changed

9 files changed

+248
-214
lines changed

expected/pg_pathman.out

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ begin
616616
'wrong plan provider');
617617

618618
perform test.pathman_equal((plan->0->'Plan'->'Plans'->1->'Relation Name')::text,
619-
format('"runtime_test_1_%s"', pathman.get_hash(hashint4(1), 6)),
619+
format('"runtime_test_1_%s"', pathman.get_hash_part_idx(hashint4(1), 6)),
620620
'wrong partition');
621621

622622
select count(*) from jsonb_array_elements_text(plan->0->'Plan'->'Plans') into num;
@@ -649,7 +649,7 @@ begin
649649

650650
for i in 0..3 loop
651651
perform test.pathman_equal((plan->0->'Plan'->'Plans'->1->'Plans'->i->'Relation Name')::text,
652-
format('"runtime_test_1_%s"', pathman.get_hash(hashint4(i + 1), 6)),
652+
format('"runtime_test_1_%s"', pathman.get_hash_part_idx(hashint4(i + 1), 6)),
653653
'wrong partition');
654654

655655
num = plan->0->'Plan'->'Plans'->1->'Plans'->i->'Actual Loops';
@@ -715,7 +715,7 @@ begin
715715

716716
for i in 0..3 loop
717717
perform test.pathman_equal((plan->0->'Plan'->'Plans'->1->'Plans'->0->'Plans'->i->'Relation Name')::text,
718-
format('"runtime_test_2_%s"', pathman.get_hash(hashint4(i + 1), 6)),
718+
format('"runtime_test_2_%s"', pathman.get_hash_part_idx(hashint4(i + 1), 6)),
719719
'wrong partition');
720720

721721
num = plan->0->'Plan'->'Plans'->1->'Plans'->0->'Plans'->i->'Actual Loops';

hash.sql

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,10 @@ BEGIN
4949

5050
EXECUTE format('CREATE TABLE %1$s (LIKE %2$s INCLUDING ALL) INHERITS (%2$s)',
5151
v_child_relname,
52-
parent_relid::text);
52+
@extschema@.get_schema_qualified_name(parent_relid));
5353

54-
EXECUTE format('ALTER TABLE %s ADD CONSTRAINT %s CHECK (@extschema@.get_hash(%s(%s), %s) = %s)',
54+
EXECUTE format('ALTER TABLE %s ADD CONSTRAINT %s
55+
CHECK (@extschema@.get_hash_part_idx(%s(%s), %s) = %s)',
5556
v_child_relname,
5657
@extschema@.build_check_constraint_name(v_child_relname::regclass,
5758
attribute),
@@ -84,22 +85,22 @@ DECLARE
8485
RETURNS TRIGGER AS
8586
$body$
8687
DECLARE
87-
old_hash INTEGER;
88-
new_hash INTEGER;
88+
old_idx INTEGER; /* partition indices */
89+
new_idx INTEGER;
8990
q TEXT;
9091
9192
BEGIN
92-
old_hash := @extschema@.get_hash(%9$s(OLD.%2$s), %3$s);
93-
new_hash := @extschema@.get_hash(%9$s(NEW.%2$s), %3$s);
93+
old_idx := @extschema@.get_hash_part_idx(%9$s(OLD.%2$s), %3$s);
94+
new_idx := @extschema@.get_hash_part_idx(%9$s(NEW.%2$s), %3$s);
9495
95-
IF old_hash = new_hash THEN
96+
IF old_idx = new_idx THEN
9697
RETURN NEW;
9798
END IF;
9899
99-
q := format(''DELETE FROM %8$s WHERE %4$s'', old_hash);
100+
q := format(''DELETE FROM %8$s WHERE %4$s'', old_idx);
100101
EXECUTE q USING %5$s;
101102
102-
q := format(''INSERT INTO %8$s VALUES (%6$s)'', new_hash);
103+
q := format(''INSERT INTO %8$s VALUES (%6$s)'', new_idx);
103104
EXECUTE q USING %7$s;
104105
105106
RETURN NULL;
@@ -138,7 +139,7 @@ BEGIN
138139
attname || ' IS NULL END',
139140
' AND '),
140141
string_agg('$' || attnum, ', ')
141-
FROM pg_attribute
142+
FROM pg_catalog.pg_attribute
142143
WHERE attrelid = parent_relid AND attnum > 0
143144
INTO att_names,
144145
old_fields,
@@ -152,7 +153,8 @@ BEGIN
152153
RAISE EXCEPTION 'Table % is not partitioned', quote_ident(parent_relid::TEXT);
153154
END IF;
154155

155-
partitions_count := COUNT(*) FROM pg_inherits WHERE inhparent = parent_relid::oid;
156+
partitions_count := COUNT(*) FROM pg_catalog.pg_inherits
157+
WHERE inhparent = parent_relid::oid;
156158

157159
/* Function name, trigger name and child relname template */
158160
funcname := plain_schema || '.' || quote_ident(format('%s_update_trigger_func', plain_relname));
@@ -189,6 +191,6 @@ LANGUAGE C STRICT;
189191
/*
190192
* Calculates hash for integer value
191193
*/
192-
CREATE OR REPLACE FUNCTION @extschema@.get_hash(INTEGER, INTEGER)
193-
RETURNS INTEGER AS 'pg_pathman', 'get_hash'
194+
CREATE OR REPLACE FUNCTION @extschema@.get_hash_part_idx(INTEGER, INTEGER)
195+
RETURNS INTEGER AS 'pg_pathman', 'get_hash_part_idx'
194196
LANGUAGE C STRICT;

init.sql

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,8 @@ SELECT pg_catalog.pg_extension_config_dump('@extschema@.pathman_config', '');
3535
* Copy rows to partitions
3636
*/
3737
CREATE OR REPLACE FUNCTION @extschema@.partition_data(
38-
p_parent REGCLASS,
39-
p_invalidate_cache_on_error BOOLEAN DEFAULT FALSE,
40-
OUT p_total BIGINT)
38+
parent_relid REGCLASS,
39+
OUT p_total BIGINT)
4140
AS
4241
$$
4342
DECLARE
@@ -46,14 +45,12 @@ DECLARE
4645
cnt BIGINT := 0;
4746

4847
BEGIN
49-
relname := @extschema@.validate_relname(p_parent);
50-
5148
p_total := 0;
5249

5350
/* Create partitions and copy rest of the data */
5451
EXECUTE format('WITH part_data AS (DELETE FROM ONLY %1$s RETURNING *)
5552
INSERT INTO %1$s SELECT * FROM part_data',
56-
relname);
53+
@extschema@.get_schema_qualified_name(parent_relid));
5754

5855
/* Get number of inserted rows */
5956
GET DIAGNOSTICS p_total = ROW_COUNT;
@@ -66,17 +63,17 @@ LANGUAGE plpgsql;
6663
* Disable pathman partitioning for specified relation
6764
*/
6865
CREATE OR REPLACE FUNCTION @extschema@.disable_partitioning(
69-
relation REGCLASS)
66+
parent_relid REGCLASS)
7067
RETURNS VOID AS
7168
$$
7269
BEGIN
73-
relation := @extschema@.validate_relname(relation);
70+
PERFORM @extschema@.validate_relname(parent_relid);
7471

75-
DELETE FROM @extschema@.pathman_config WHERE partrel = relation;
76-
PERFORM @extschema@.drop_triggers(relation);
72+
DELETE FROM @extschema@.pathman_config WHERE partrel = parent_relid;
73+
PERFORM @extschema@.drop_triggers(parent_relid);
7774

7875
/* Notify backend about changes */
79-
PERFORM on_remove_partitions(relation::regclass::integer);
76+
PERFORM @extschema@.on_remove_partitions(parent_relid);
8077
END
8178
$$
8279
LANGUAGE plpgsql;
@@ -131,41 +128,51 @@ CREATE OR REPLACE FUNCTION @extschema@.get_plain_schema_and_relname(
131128
AS
132129
$$
133130
BEGIN
134-
SELECT relnamespace::regnamespace, pg_class.relname
135-
FROM pg_class WHERE oid = cls::oid
131+
SELECT pg_catalog.pg_class.relnamespace::regnamespace,
132+
pg_catalog.pg_class.relname
133+
FROM pg_catalog.pg_class WHERE oid = cls::oid
136134
INTO schema, relname;
137135
END
138136
$$
139137
LANGUAGE plpgsql;
140138

141139
/*
142-
* Validates relation name. It must be schema qualified
140+
* Returns schema-qualified name for table
143141
*/
144-
CREATE OR REPLACE FUNCTION @extschema@.validate_relname(
145-
cls REGCLASS)
142+
CREATE OR REPLACE FUNCTION @extschema@.get_schema_qualified_name(
143+
cls REGCLASS,
144+
delimiter TEXT DEFAULT '.',
145+
suffix TEXT DEFAULT '')
146146
RETURNS TEXT AS
147147
$$
148148
BEGIN
149-
RETURN @extschema@.get_schema_qualified_name(cls, '.');
149+
RETURN (SELECT quote_ident(relnamespace::regnamespace::text) ||
150+
delimiter ||
151+
quote_ident(relname || suffix)
152+
FROM pg_catalog.pg_class
153+
WHERE oid = cls::oid);
150154
END
151155
$$
152156
LANGUAGE plpgsql;
153157

154158
/*
155-
* Returns schema-qualified name for table
159+
* Validates relation name. It must be schema qualified
156160
*/
157-
CREATE OR REPLACE FUNCTION @extschema@.get_schema_qualified_name(
158-
cls REGCLASS,
159-
delimiter TEXT DEFAULT '_',
160-
suffix TEXT DEFAULT '')
161+
CREATE OR REPLACE FUNCTION @extschema@.validate_relname(
162+
cls REGCLASS)
161163
RETURNS TEXT AS
162164
$$
165+
DECLARE
166+
relname TEXT;
167+
163168
BEGIN
164-
RETURN (SELECT quote_ident(relnamespace::regnamespace::text) ||
165-
delimiter ||
166-
quote_ident(relname || suffix)
167-
FROM pg_class
168-
WHERE oid = cls::oid);
169+
relname = @extschema@.get_schema_qualified_name(cls);
170+
171+
IF relname IS NULL THEN
172+
RAISE EXCEPTION 'Relation %s does not exist', cls;
173+
END IF;
174+
175+
RETURN relname;
169176
END
170177
$$
171178
LANGUAGE plpgsql;
@@ -183,8 +190,10 @@ DECLARE
183190
BEGIN
184191
FOR rec IN (
185192
WITH
186-
a1 AS (select * from pg_attribute where attrelid = relation1 and attnum > 0),
187-
a2 AS (select * from pg_attribute where attrelid = relation2 and attnum > 0)
193+
a1 AS (select * from pg_catalog.pg_attribute
194+
where attrelid = relation1 and attnum > 0),
195+
a2 AS (select * from pg_catalog.pg_attribute
196+
where attrelid = relation2 and attnum > 0)
188197
SELECT a1.attname name1, a2.attname name2, a1.atttypid type1, a2.atttypid type2
189198
FROM a1
190199
FULL JOIN a2 ON a1.attnum = a2.attnum
@@ -211,7 +220,7 @@ DECLARE
211220
pg_class_oid oid;
212221

213222
BEGIN
214-
pg_class_oid = 'pg_class'::regclass;
223+
pg_class_oid = 'pg_catalog.pg_class'::regclass;
215224

216225
/* Handle 'DROP TABLE' events */
217226
WITH to_be_deleted AS (
@@ -261,11 +270,10 @@ DECLARE
261270
v_rec RECORD;
262271
v_rows INTEGER;
263272
v_part_count INTEGER := 0;
264-
v_relname TEXT;
265273
conf_num_del INTEGER;
266274

267275
BEGIN
268-
v_relname := @extschema@.validate_relname(parent_relid);
276+
PERFORM @extschema@.validate_relname(parent_relid);
269277

270278
/* Drop trigger first */
271279
PERFORM @extschema@.drop_triggers(parent_relid);
@@ -280,7 +288,8 @@ BEGIN
280288
END IF;
281289

282290
FOR v_rec IN (SELECT inhrelid::regclass::text AS tbl
283-
FROM pg_inherits WHERE inhparent::regclass = parent_relid)
291+
FROM pg_catalog.pg_inherits
292+
WHERE inhparent::regclass = parent_relid)
284293
LOOP
285294
IF NOT delete_data THEN
286295
EXECUTE format('WITH part_data AS (DELETE FROM %s RETURNING *)
@@ -376,6 +385,6 @@ LANGUAGE C STRICT;
376385
/*
377386
* Get parent of pg_pathman's partition.
378387
*/
379-
CREATE OR REPLACE FUNCTION @extschema@.parent_of_partition(REGCLASS)
380-
RETURNS REGCLASS AS 'pg_pathman', 'parent_of_partition'
388+
CREATE OR REPLACE FUNCTION @extschema@.get_parent_of_partition(REGCLASS)
389+
RETURNS REGCLASS AS 'pg_pathman', 'get_parent_of_partition_pl'
381390
LANGUAGE C STRICT;

0 commit comments

Comments
 (0)