Skip to content

Commit 33d2cf7

Browse files
committed
reimplement functions validate_relname(), get_number_of_partitions() in C language, refactoring & code cleanup
1 parent 000130d commit 33d2cf7

File tree

9 files changed

+774
-736
lines changed

9 files changed

+774
-736
lines changed

expected/pathman_calamity.out

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,28 @@ set client_min_messages = NOTICE;
1414
CREATE TABLE calamity.part_test(val serial);
1515
/* check function validate_relname() */
1616
SELECT validate_relname('calamity.part_test');
17-
validate_relname
18-
--------------------
19-
calamity.part_test
17+
validate_relname
18+
------------------
19+
20+
(1 row)
21+
22+
SELECT validate_relname(1::REGCLASS);
23+
ERROR: relation "1" does not exist
24+
SELECT validate_relname(NULL);
25+
ERROR: relation should not be NULL
26+
/* check function get_number_of_partitions() */
27+
SELECT get_number_of_partitions('calamity.part_test');
28+
get_number_of_partitions
29+
--------------------------
30+
0
31+
(1 row)
32+
33+
SELECT get_number_of_partitions(NULL) IS NULL;
34+
?column?
35+
----------
36+
t
2037
(1 row)
2138

22-
/* SELECT validate_relname(NULL); -- FIXME: %s */
2339
/* check function get_parent_of_partition() */
2440
SELECT get_parent_of_partition('calamity.part_test');
2541
ERROR: "part_test" is not a partition

hash.sql

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,7 @@ BEGIN
147147
att_val_fmt,
148148
att_fmt;
149149

150-
partitions_count := COUNT(*) FROM pg_catalog.pg_inherits
151-
WHERE inhparent = parent_relid::oid;
150+
partitions_count := @extschema@.get_number_of_partitions(parent_relid);
152151

153152
/* Build trigger & trigger function's names */
154153
funcname := @extschema@.build_update_trigger_func_name(parent_relid);

init.sql

Lines changed: 47 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,6 @@ SELECT pg_catalog.pg_extension_config_dump('@extschema@.pathman_config', '');
104104
SELECT pg_catalog.pg_extension_config_dump('@extschema@.pathman_config_params', '');
105105

106106

107-
CREATE OR REPLACE FUNCTION @extschema@.partitions_count(relation REGCLASS)
108-
RETURNS INT AS
109-
$$
110-
BEGIN
111-
RETURN count(*) FROM pg_inherits WHERE inhparent = relation;
112-
END
113-
$$
114-
LANGUAGE plpgsql STRICT;
115-
116107
/*
117108
* Add a row describing the optional parameter to pathman_config_params.
118109
*/
@@ -185,7 +176,8 @@ RETURNS TABLE (
185176
partattr TEXT,
186177
range_min TEXT,
187178
range_max TEXT)
188-
AS 'pg_pathman', 'show_partition_list_internal' LANGUAGE C STRICT;
179+
AS 'pg_pathman', 'show_partition_list_internal'
180+
LANGUAGE C STRICT;
189181

190182
/*
191183
* View for show_partition_list().
@@ -206,7 +198,8 @@ RETURNS TABLE (
206198
relid REGCLASS,
207199
processed INT,
208200
status TEXT)
209-
AS 'pg_pathman', 'show_concurrent_part_tasks_internal' LANGUAGE C STRICT;
201+
AS 'pg_pathman', 'show_concurrent_part_tasks_internal'
202+
LANGUAGE C STRICT;
210203

211204
/*
212205
* View for show_concurrent_part_tasks().
@@ -353,28 +346,6 @@ END
353346
$$
354347
LANGUAGE plpgsql STRICT;
355348

356-
/*
357-
* Validates relation name. It must be schema qualified.
358-
*/
359-
CREATE OR REPLACE FUNCTION @extschema@.validate_relname(
360-
cls REGCLASS)
361-
RETURNS TEXT AS
362-
$$
363-
DECLARE
364-
relname TEXT;
365-
366-
BEGIN
367-
relname = @extschema@.get_schema_qualified_name(cls);
368-
369-
IF relname IS NULL THEN
370-
RAISE EXCEPTION 'relation %s does not exist', cls;
371-
END IF;
372-
373-
RETURN relname;
374-
END
375-
$$
376-
LANGUAGE plpgsql;
377-
378349
/*
379350
* Aggregates several common relation checks before partitioning.
380351
* Suitable for every partitioning type.
@@ -444,25 +415,6 @@ END
444415
$$
445416
LANGUAGE plpgsql STRICT;
446417

447-
/*
448-
* Returns the schema-qualified name of table.
449-
*/
450-
CREATE OR REPLACE FUNCTION @extschema@.get_schema_qualified_name(
451-
cls REGCLASS,
452-
delimiter TEXT DEFAULT '.',
453-
suffix TEXT DEFAULT '')
454-
RETURNS TEXT AS
455-
$$
456-
BEGIN
457-
RETURN (SELECT quote_ident(relnamespace::regnamespace::text) ||
458-
delimiter ||
459-
quote_ident(relname || suffix)
460-
FROM pg_catalog.pg_class
461-
WHERE oid = cls::oid);
462-
END
463-
$$
464-
LANGUAGE plpgsql STRICT;
465-
466418
/*
467419
* Check if two relations have equal structures.
468420
*/
@@ -662,41 +614,62 @@ RETURNS VOID AS 'pg_pathman', 'on_partitions_removed'
662614
LANGUAGE C STRICT;
663615

664616

617+
/*
618+
* Get number of partitions managed by pg_pathman.
619+
*/
620+
CREATE OR REPLACE FUNCTION @extschema@.get_number_of_partitions(
621+
parent_relid REGCLASS)
622+
RETURNS INT4 AS 'pg_pathman', 'get_number_of_partitions_pl'
623+
LANGUAGE C STRICT;
624+
665625
/*
666626
* Get parent of pg_pathman's partition.
667627
*/
668-
CREATE OR REPLACE FUNCTION @extschema@.get_parent_of_partition(REGCLASS)
628+
CREATE OR REPLACE FUNCTION @extschema@.get_parent_of_partition(
629+
partition_relid REGCLASS)
669630
RETURNS REGCLASS AS 'pg_pathman', 'get_parent_of_partition_pl'
670631
LANGUAGE C STRICT;
671632

672633
/*
673634
* Extract basic type of a domain.
674635
*/
675-
CREATE OR REPLACE FUNCTION @extschema@.get_base_type(REGTYPE)
636+
CREATE OR REPLACE FUNCTION @extschema@.get_base_type(
637+
typid REGTYPE)
676638
RETURNS REGTYPE AS 'pg_pathman', 'get_base_type_pl'
677639
LANGUAGE C STRICT;
678640

679641
/*
680642
* Returns attribute type name for relation.
681643
*/
682644
CREATE OR REPLACE FUNCTION @extschema@.get_attribute_type(
683-
REGCLASS, TEXT)
645+
relid REGCLASS,
646+
attname TEXT)
684647
RETURNS REGTYPE AS 'pg_pathman', 'get_attribute_type_pl'
685648
LANGUAGE C STRICT;
686649

687650
/*
688651
* Return tablespace name for specified relation.
689652
*/
690-
CREATE OR REPLACE FUNCTION @extschema@.get_rel_tablespace_name(REGCLASS)
691-
RETURNS TEXT AS 'pg_pathman', 'get_rel_tablespace_name'
653+
CREATE OR REPLACE FUNCTION @extschema@.get_tablespace(
654+
relid REGCLASS)
655+
RETURNS TEXT AS 'pg_pathman', 'get_tablespace_pl'
692656
LANGUAGE C STRICT;
693657

694658

659+
/*
660+
* Check that relation exists.
661+
*/
662+
CREATE OR REPLACE FUNCTION @extschema@.validate_relname(
663+
relid REGCLASS)
664+
RETURNS VOID AS 'pg_pathman', 'validate_relname'
665+
LANGUAGE C;
666+
695667
/*
696668
* Checks if attribute is nullable
697669
*/
698670
CREATE OR REPLACE FUNCTION @extschema@.is_attribute_nullable(
699-
REGCLASS, TEXT)
671+
relid REGCLASS,
672+
attname TEXT)
700673
RETURNS BOOLEAN AS 'pg_pathman', 'is_attribute_nullable'
701674
LANGUAGE C STRICT;
702675

@@ -713,25 +686,27 @@ LANGUAGE C STRICT;
713686
* Build check constraint name for a specified relation's column.
714687
*/
715688
CREATE OR REPLACE FUNCTION @extschema@.build_check_constraint_name(
716-
REGCLASS, INT2)
689+
partition_relid REGCLASS,
690+
partitioned_col INT2)
717691
RETURNS TEXT AS 'pg_pathman', 'build_check_constraint_name_attnum'
718692
LANGUAGE C STRICT;
719693

720694
CREATE OR REPLACE FUNCTION @extschema@.build_check_constraint_name(
721-
REGCLASS, TEXT)
695+
partition_relid REGCLASS,
696+
partitioned_col TEXT)
722697
RETURNS TEXT AS 'pg_pathman', 'build_check_constraint_name_attname'
723698
LANGUAGE C STRICT;
724699

725700
/*
726701
* Build update trigger and its underlying function's names.
727702
*/
728703
CREATE OR REPLACE FUNCTION @extschema@.build_update_trigger_name(
729-
REGCLASS)
704+
relid REGCLASS)
730705
RETURNS TEXT AS 'pg_pathman', 'build_update_trigger_name'
731706
LANGUAGE C STRICT;
732707

733708
CREATE OR REPLACE FUNCTION @extschema@.build_update_trigger_func_name(
734-
REGCLASS)
709+
relid REGCLASS)
735710
RETURNS TEXT AS 'pg_pathman', 'build_update_trigger_func_name'
736711
LANGUAGE C STRICT;
737712

@@ -746,7 +721,8 @@ CREATE OR REPLACE FUNCTION @extschema@.add_to_pathman_config(
746721
RETURNS BOOLEAN AS 'pg_pathman', 'add_to_pathman_config'
747722
LANGUAGE C;
748723

749-
CREATE OR REPLACE FUNCTION @extschema@.invalidate_relcache(relid OID)
724+
CREATE OR REPLACE FUNCTION @extschema@.invalidate_relcache(
725+
OID)
750726
RETURNS VOID AS 'pg_pathman'
751727
LANGUAGE C STRICT;
752728

@@ -755,18 +731,18 @@ LANGUAGE C STRICT;
755731
* Lock partitioned relation to restrict concurrent
756732
* modification of partitioning scheme.
757733
*/
758-
CREATE OR REPLACE FUNCTION @extschema@.lock_partitioned_relation(
759-
REGCLASS)
760-
RETURNS VOID AS 'pg_pathman', 'lock_partitioned_relation'
761-
LANGUAGE C STRICT;
734+
CREATE OR REPLACE FUNCTION @extschema@.lock_partitioned_relation(
735+
parent_relid REGCLASS)
736+
RETURNS VOID AS 'pg_pathman', 'lock_partitioned_relation'
737+
LANGUAGE C STRICT;
762738

763739
/*
764740
* Lock relation to restrict concurrent modification of data.
765741
*/
766-
CREATE OR REPLACE FUNCTION @extschema@.prevent_relation_modification(
767-
REGCLASS)
768-
RETURNS VOID AS 'pg_pathman', 'prevent_relation_modification'
769-
LANGUAGE C STRICT;
742+
CREATE OR REPLACE FUNCTION @extschema@.prevent_relation_modification(
743+
parent_relid REGCLASS)
744+
RETURNS VOID AS 'pg_pathman', 'prevent_relation_modification'
745+
LANGUAGE C STRICT;
770746

771747

772748
/*

range.sql

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ BEGIN
158158
parent_relid,
159159
start_value,
160160
start_value + p_interval,
161-
@extschema@.get_rel_tablespace_name(parent_relid);
161+
@extschema@.get_tablespace(parent_relid);
162162

163163
start_value := start_value + p_interval;
164164
END LOOP;
@@ -270,7 +270,7 @@ BEGIN
270270
parent_relid,
271271
start_value,
272272
start_value + p_interval,
273-
tablespace := @extschema@.get_rel_tablespace_name(parent_relid));
273+
tablespace := @extschema@.get_tablespace(parent_relid));
274274

275275
start_value := start_value + p_interval;
276276
END LOOP;
@@ -343,7 +343,7 @@ BEGIN
343343
parent_relid,
344344
start_value,
345345
start_value + p_interval,
346-
tablespace := @extschema@.get_rel_tablespace_name(parent_relid));
346+
tablespace := @extschema@.get_tablespace(parent_relid));
347347

348348
start_value := start_value + p_interval;
349349
part_count := part_count + 1;
@@ -416,7 +416,7 @@ BEGIN
416416
parent_relid,
417417
start_value,
418418
start_value + p_interval,
419-
@extschema@.get_rel_tablespace_name(parent_relid);
419+
@extschema@.get_tablespace(parent_relid);
420420

421421
start_value := start_value + p_interval;
422422
part_count := part_count + 1;
@@ -733,7 +733,7 @@ DECLARE
733733
v_atttype REGTYPE;
734734

735735
BEGIN
736-
IF @extschema@.partitions_count(parent_relid) = 0 THEN
736+
IF @extschema@.get_number_of_partitions(parent_relid) = 0 THEN
737737
RAISE EXCEPTION 'cannot append to empty partitions set';
738738
END IF;
739739

@@ -843,7 +843,7 @@ DECLARE
843843
v_atttype REGTYPE;
844844

845845
BEGIN
846-
IF @extschema@.partitions_count(parent_relid) = 0 THEN
846+
IF @extschema@.get_number_of_partitions(parent_relid) = 0 THEN
847847
RAISE EXCEPTION 'cannot prepend to empty partitions set';
848848
END IF;
849849

@@ -907,7 +907,7 @@ BEGIN
907907
END IF;
908908

909909
/* check range overlap */
910-
IF @extschema@.partitions_count(parent_relid) > 0 THEN
910+
IF @extschema@.get_number_of_partitions(parent_relid) > 0 THEN
911911
PERFORM @extschema@.check_range_available(parent_relid,
912912
start_value,
913913
end_value);

sql/pathman_calamity.sql

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@ CREATE TABLE calamity.part_test(val serial);
1616

1717
/* check function validate_relname() */
1818
SELECT validate_relname('calamity.part_test');
19-
/* SELECT validate_relname(NULL); -- FIXME: %s */
19+
SELECT validate_relname(1::REGCLASS);
20+
SELECT validate_relname(NULL);
21+
22+
/* check function get_number_of_partitions() */
23+
SELECT get_number_of_partitions('calamity.part_test');
24+
SELECT get_number_of_partitions(NULL) IS NULL;
2025

2126
/* check function get_parent_of_partition() */
2227
SELECT get_parent_of_partition('calamity.part_test');

src/partition_creation.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ spawn_partitions_val(Oid parent_relid, /* parent's Oid */
556556

557557
/* What, again? Don't want to deal with this nightmare */
558558
if (move_bound_op_ret_type != range_bound_type)
559-
elog(ERROR, "error in spawn_partitions_val()");
559+
elog(ERROR, "error in function " CppAsString(spawn_partitions_val));
560560
}
561561

562562
/* Get operator's underlying function */

0 commit comments

Comments
 (0)