Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbce18a5

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 parent1a0f23e commitbce18a5

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_hashINTEGER;
88-
new_hashINTEGER;
88+
old_idxINTEGER; /* partition indices */
89+
new_idxINTEGER;
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-
IFold_hash =new_hash THEN
96+
IFold_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+
FROMpg_catalog.pg_attribute
142143
WHERE attrelid= parent_relidAND 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_inheritsWHERE inhparent= parent_relid::oid;
156+
partitions_count :=COUNT(*)FROMpg_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-
CREATEOR REPLACE FUNCTION @extschema@.get_hash(INTEGER,INTEGER)
193-
RETURNSINTEGERAS'pg_pathman','get_hash'
194+
CREATEOR REPLACE FUNCTION @extschema@.get_hash_part_idx(INTEGER,INTEGER)
195+
RETURNSINTEGERAS'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
CREATEOR REPLACE FUNCTION @extschema@.partition_data(
38-
p_parentREGCLASS,
39-
p_invalidate_cache_on_errorBOOLEAN DEFAULT FALSE,
40-
OUT p_totalBIGINT)
38+
parent_relidREGCLASS,
39+
OUT p_totalBIGINT)
4140
AS
4241
$$
4342
DECLARE
@@ -46,14 +45,12 @@ DECLARE
4645
cntBIGINT :=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
CREATEOR REPLACE FUNCTION @extschema@.disable_partitioning(
69-
relationREGCLASS)
66+
parent_relidREGCLASS)
7067
RETURNS VOIDAS
7168
$$
7269
BEGIN
73-
relation :=@extschema@.validate_relname(relation);
70+
PERFORM@extschema@.validate_relname(parent_relid);
7471

75-
DELETEFROM @extschema@.pathman_configWHERE partrel=relation;
76-
PERFORM @extschema@.drop_triggers(relation);
72+
DELETEFROM @extschema@.pathman_configWHERE 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_classWHEREoid= cls::oid
131+
SELECTpg_catalog.pg_class.relnamespace::regnamespace,
132+
pg_catalog.pg_class.relname
133+
FROMpg_catalog.pg_classWHEREoid= 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-
CREATEOR REPLACE FUNCTION @extschema@.validate_relname(
145-
clsREGCLASS)
142+
CREATEOR REPLACE FUNCTION @extschema@.get_schema_qualified_name(
143+
clsREGCLASS,
144+
delimiterTEXT DEFAULT'.',
145+
suffixTEXT DEFAULT'')
146146
RETURNSTEXTAS
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+
FROMpg_catalog.pg_class
153+
WHEREoid= 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-
CREATEOR REPLACE FUNCTION @extschema@.get_schema_qualified_name(
158-
clsREGCLASS,
159-
delimiterTEXT DEFAULT'_',
160-
suffixTEXT DEFAULT'')
161+
CREATEOR REPLACE FUNCTION @extschema@.validate_relname(
162+
clsREGCLASS)
161163
RETURNSTEXTAS
162164
$$
165+
DECLARE
166+
relnameTEXT;
167+
163168
BEGIN
164-
RETURN (SELECT quote_ident(relnamespace::regnamespace::text)||
165-
delimiter||
166-
quote_ident(relname|| suffix)
167-
FROM pg_class
168-
WHEREoid= cls::oid);
169+
relname= @extschema@.get_schema_qualified_name(cls);
170+
171+
IF relname ISNULL 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 recIN (
185192
WITH
186-
a1AS (select*from pg_attributewhere attrelid= relation1and attnum>0),
187-
a2AS (select*from pg_attributewhere attrelid= relation2and attnum>0)
193+
a1AS (select*frompg_catalog.pg_attribute
194+
where attrelid= relation1and attnum>0),
195+
a2AS (select*frompg_catalog.pg_attribute
196+
where attrelid= relation2and attnum>0)
188197
SELECTa1.attname name1,a2.attname name2,a1.atttypid type1,a2.atttypid type2
189198
FROM a1
190199
FULLJOIN a2ONa1.attnum=a2.attnum
@@ -211,7 +220,7 @@ DECLARE
211220
pg_class_oidoid;
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_deletedAS (
@@ -261,11 +270,10 @@ DECLARE
261270
v_recRECORD;
262271
v_rowsINTEGER;
263272
v_part_countINTEGER :=0;
264-
v_relnameTEXT;
265273
conf_num_delINTEGER;
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_recIN (SELECT inhrelid::regclass::textAS tbl
283-
FROM pg_inheritsWHERE inhparent::regclass= parent_relid)
291+
FROMpg_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-
CREATEOR REPLACE FUNCTION @extschema@.parent_of_partition(REGCLASS)
380-
RETURNS REGCLASSAS'pg_pathman','parent_of_partition'
388+
CREATEOR REPLACE FUNCTION @extschema@.get_parent_of_partition(REGCLASS)
389+
RETURNS REGCLASSAS'pg_pathman','get_parent_of_partition_pl'
381390
LANGUAGE C STRICT;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp