Skip to content

Commit d184171

Browse files
committed
huge refactoring, fix code style and various mistakes
1 parent 495e036 commit d184171

17 files changed

+643
-679
lines changed

expected/pathman_basic.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1276,7 +1276,7 @@ SELECT pathman.replace_hash_partition('test.hash_rel_0', 'test.hash_rel_extern')
12761276
Indexes:
12771277
"hash_rel_0_pkey" PRIMARY KEY, btree (id)
12781278
Triggers:
1279-
hash_rel_upd_trig BEFORE UPDATE OF value ON test.hash_rel_0 FOR EACH ROW EXECUTE PROCEDURE pathman.update_trigger_func()
1279+
hash_rel_upd_trig BEFORE UPDATE OF value ON test.hash_rel_0 FOR EACH ROW EXECUTE PROCEDURE pathman.pathman_update_trigger_func()
12801280

12811281
\d+ test.hash_rel_extern
12821282
Table "test.hash_rel_extern"

expected/pathman_calamity.out

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,13 @@ DELETE FROM calamity.part_test;
105105
/* test function create_hash_partitions() */
106106
SELECT create_hash_partitions('calamity.part_test', 'val', 2,
107107
partition_names := ARRAY[]::TEXT[]); /* not ok */
108-
ERROR: 'partition_names' and 'tablespaces' may not be empty
108+
ERROR: array should not be empty
109109
SELECT create_hash_partitions('calamity.part_test', 'val', 2,
110110
partition_names := ARRAY[ 'p1', NULL ]::TEXT[]); /* not ok */
111-
ERROR: 'partition_names' and 'tablespaces' may not contain NULLs
111+
ERROR: array should not contain NULLs
112112
SELECT create_hash_partitions('calamity.part_test', 'val', 2,
113113
partition_names := ARRAY[ ['p1'], ['p2'] ]::TEXT[]); /* not ok */
114-
ERROR: 'partition_names' and 'tablespaces' may contain only 1 dimension
114+
ERROR: array should contain only 1 dimension
115115
SELECT create_hash_partitions('calamity.part_test', 'val', 2,
116116
partition_names := ARRAY['calamity.p1']::TEXT[]); /* not ok */
117117
ERROR: size of 'partition_names' must be equal to 'partitions_count'

hash.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ BEGIN
110110
END IF;
111111

112112
/* Check that new partition has an equal structure as parent does */
113-
IF NOT @extschema@.tuple_format_is_convertable(parent_relid, new_partition) THEN
113+
IF NOT @extschema@.is_tuple_convertible(parent_relid, new_partition) THEN
114114
RAISE EXCEPTION 'partition must have a compatible tuple format';
115115
END IF;
116116

init.sql

Lines changed: 64 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -508,15 +508,6 @@ END
508508
$$
509509
LANGUAGE plpgsql STRICT;
510510

511-
/*
512-
* Check that tuple from first relation could be converted to fit the second one
513-
*/
514-
CREATE OR REPLACE FUNCTION @extschema@.tuple_format_is_convertable(
515-
relation1 OID,
516-
relation2 OID)
517-
RETURNS BOOL AS 'pg_pathman', 'tuple_format_is_convertable'
518-
LANGUAGE C;
519-
520511
/*
521512
* DDL trigger that removes entry from pathman_config table.
522513
*/
@@ -545,30 +536,6 @@ END
545536
$$
546537
LANGUAGE plpgsql;
547538

548-
/*
549-
* Function for update triggers
550-
*/
551-
CREATE OR REPLACE FUNCTION @extschema@.update_trigger_func()
552-
RETURNS TRIGGER AS 'pg_pathman', 'update_trigger_func'
553-
LANGUAGE C;
554-
555-
/*
556-
* Creates an update trigger
557-
*/
558-
CREATE OR REPLACE FUNCTION @extschema@.create_update_triggers(parent_relid REGCLASS)
559-
RETURNS VOID AS 'pg_pathman', 'create_update_triggers'
560-
LANGUAGE C STRICT;
561-
562-
CREATE OR REPLACE FUNCTION @extschema@.create_single_update_trigger(
563-
parent_relid REGCLASS,
564-
partition_relid REGCLASS)
565-
RETURNS VOID AS 'pg_pathman', 'create_single_update_trigger'
566-
LANGUAGE C STRICT;
567-
568-
CREATE OR REPLACE FUNCTION @extschema@.is_update_trigger_enabled(parent_relid REGCLASS)
569-
RETURNS BOOL AS 'pg_pathman', 'is_update_trigger_enabled'
570-
LANGUAGE C STRICT;
571-
572539
/*
573540
* Drop triggers
574541
*/
@@ -742,6 +709,49 @@ END
742709
$$ LANGUAGE plpgsql;
743710

744711

712+
/*
713+
* Check if tuple from first relation can be converted to fit the second one.
714+
*/
715+
CREATE OR REPLACE FUNCTION @extschema@.is_tuple_convertible(
716+
relation1 REGCLASS,
717+
relation2 REGCLASS)
718+
RETURNS BOOL AS 'pg_pathman', 'is_tuple_convertible'
719+
LANGUAGE C STRICT;
720+
721+
722+
/*
723+
* Function for UPDATE triggers.
724+
*/
725+
CREATE OR REPLACE FUNCTION @extschema@.pathman_update_trigger_func()
726+
RETURNS TRIGGER AS 'pg_pathman', 'pathman_update_trigger_func'
727+
LANGUAGE C STRICT;
728+
729+
/*
730+
* Creates UPDATE triggers.
731+
*/
732+
CREATE OR REPLACE FUNCTION @extschema@.create_update_triggers(
733+
parent_relid REGCLASS)
734+
RETURNS VOID AS 'pg_pathman', 'create_update_triggers'
735+
LANGUAGE C STRICT;
736+
737+
/*
738+
* Creates single UPDATE trigger.
739+
*/
740+
CREATE OR REPLACE FUNCTION @extschema@.create_single_update_trigger(
741+
parent_relid REGCLASS,
742+
partition_relid REGCLASS)
743+
RETURNS VOID AS 'pg_pathman', 'create_single_update_trigger'
744+
LANGUAGE C STRICT;
745+
746+
/*
747+
* Check if relation has pg_pathman's UPDATE trigger.
748+
*/
749+
CREATE OR REPLACE FUNCTION @extschema@.has_update_trigger(
750+
parent_relid REGCLASS)
751+
RETURNS BOOL AS 'pg_pathman', 'has_update_trigger'
752+
LANGUAGE C STRICT;
753+
754+
745755
/*
746756
* Partitioning key
747757
*/
@@ -845,6 +855,15 @@ CREATE OR REPLACE FUNCTION @extschema@.is_date_type(
845855
RETURNS BOOLEAN AS 'pg_pathman', 'is_date_type'
846856
LANGUAGE C STRICT;
847857

858+
/*
859+
* Check if TYPE supports the specified operator.
860+
*/
861+
CREATE OR REPLACE FUNCTION @extschema@.is_operator_supported(
862+
type_oid OID,
863+
opname TEXT)
864+
RETURNS BOOLEAN AS 'pg_pathman', 'is_operator_supported'
865+
LANGUAGE C;
866+
848867

849868
/*
850869
* Build check constraint name for a specified relation's column.
@@ -862,13 +881,16 @@ RETURNS TEXT AS 'pg_pathman', 'build_check_constraint_name_attname'
862881
LANGUAGE C STRICT;
863882

864883
/*
865-
* Build update trigger and its underlying function's names.
884+
* Build UPDATE trigger's name.
866885
*/
867886
CREATE OR REPLACE FUNCTION @extschema@.build_update_trigger_name(
868887
relid REGCLASS)
869888
RETURNS TEXT AS 'pg_pathman', 'build_update_trigger_name'
870889
LANGUAGE C STRICT;
871890

891+
/*
892+
* Buld UPDATE trigger function's name.
893+
*/
872894
CREATE OR REPLACE FUNCTION @extschema@.build_update_trigger_func_name(
873895
relid REGCLASS)
874896
RETURNS TEXT AS 'pg_pathman', 'build_update_trigger_func_name'
@@ -904,18 +926,6 @@ RETURNS VOID AS 'pg_pathman', 'prevent_relation_modification'
904926
LANGUAGE C STRICT;
905927

906928

907-
/*
908-
* DEBUG: Place this inside some plpgsql fuction and set breakpoint.
909-
*/
910-
CREATE OR REPLACE FUNCTION @extschema@.debug_capture()
911-
RETURNS VOID AS 'pg_pathman', 'debug_capture'
912-
LANGUAGE C STRICT;
913-
914-
CREATE OR REPLACE FUNCTION @extschema@.get_pathman_lib_version()
915-
RETURNS CSTRING AS 'pg_pathman', 'get_pathman_lib_version'
916-
LANGUAGE C STRICT;
917-
918-
919929
/*
920930
* Invoke init_callback on RANGE partition.
921931
*/
@@ -938,12 +948,14 @@ CREATE OR REPLACE FUNCTION @extschema@.invoke_on_partition_created_callback(
938948
RETURNS VOID AS 'pg_pathman', 'invoke_on_partition_created_callback'
939949
LANGUAGE C;
940950

951+
941952
/*
942-
*
953+
* DEBUG: Place this inside some plpgsql fuction and set breakpoint.
943954
*/
944-
CREATE OR REPLACE FUNCTION @extschema@.is_operator_supported(
945-
type_oid OID,
946-
opname TEXT)
947-
RETURNS BOOLEAN AS 'pg_pathman', 'is_operator_supported'
948-
LANGUAGE C;
955+
CREATE OR REPLACE FUNCTION @extschema@.debug_capture()
956+
RETURNS VOID AS 'pg_pathman', 'debug_capture'
957+
LANGUAGE C STRICT;
949958

959+
CREATE OR REPLACE FUNCTION @extschema@.get_pathman_lib_version()
960+
RETURNS CSTRING AS 'pg_pathman', 'get_pathman_lib_version'
961+
LANGUAGE C STRICT;

range.sql

Lines changed: 40 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ BEGIN
164164
IF p_count != 0 THEN
165165
part_count := @extschema@.create_range_partitions_internal(
166166
parent_relid,
167-
@extschema@.generate_bounds(start_value, p_interval, p_count),
167+
@extschema@.generate_range_bounds(start_value, p_interval, p_count),
168168
NULL,
169169
NULL);
170170
END IF;
@@ -263,7 +263,7 @@ BEGIN
263263
IF p_count != 0 THEN
264264
part_count := @extschema@.create_range_partitions_internal(
265265
parent_relid,
266-
@extschema@.generate_bounds(start_value, p_interval, p_count),
266+
@extschema@.generate_range_bounds(start_value, p_interval, p_count),
267267
NULL,
268268
NULL);
269269
END IF;
@@ -465,31 +465,6 @@ END
465465
$$
466466
LANGUAGE plpgsql;
467467

468-
469-
CREATE OR REPLACE FUNCTION @extschema@.create_range_partitions_internal(
470-
parent_relid REGCLASS,
471-
bounds ANYARRAY,
472-
relnames TEXT[],
473-
tablespaces TEXT[])
474-
RETURNS REGCLASS AS 'pg_pathman', 'create_range_partitions_internal'
475-
LANGUAGE C;
476-
477-
478-
CREATE OR REPLACE FUNCTION @extschema@.generate_bounds(
479-
p_start ANYELEMENT,
480-
p_interval INTERVAL,
481-
p_count INTEGER)
482-
RETURNS ANYARRAY AS 'pg_pathman', 'generate_bounds'
483-
LANGUAGE C;
484-
485-
CREATE OR REPLACE FUNCTION @extschema@.generate_bounds(
486-
p_start ANYELEMENT,
487-
p_interval ANYELEMENT,
488-
p_count INTEGER)
489-
RETURNS ANYARRAY AS 'pg_pathman', 'generate_bounds'
490-
LANGUAGE C;
491-
492-
493468
/*
494469
* Split RANGE partition
495470
*/
@@ -584,15 +559,6 @@ END
584559
$$
585560
LANGUAGE plpgsql;
586561

587-
/*
588-
* Merge multiple partitions. All data will be copied to the first one.
589-
* The rest of partitions will be dropped.
590-
*/
591-
CREATE OR REPLACE FUNCTION @extschema@.merge_range_partitions(
592-
partitions REGCLASS[])
593-
RETURNS VOID AS 'pg_pathman', 'merge_range_partitions'
594-
LANGUAGE C STRICT;
595-
596562
/*
597563
* The special case of merging two partitions
598564
*/
@@ -630,7 +596,7 @@ BEGIN
630596

631597
IF NOT @extschema@.is_date_type(v_atttype) AND
632598
NOT @extschema@.is_operator_supported(v_atttype, '+') THEN
633-
RAISE EXCEPTION 'Type % doesn''t support ''+'' operator', v_atttype::regtype;
599+
RAISE EXCEPTION 'type % does not support ''+'' operator', v_atttype::REGTYPE;
634600
END IF;
635601

636602
SELECT range_interval
@@ -740,7 +706,7 @@ BEGIN
740706

741707
IF NOT @extschema@.is_date_type(v_atttype) AND
742708
NOT @extschema@.is_operator_supported(v_atttype, '-') THEN
743-
RAISE EXCEPTION 'Type % doesn''t support ''-'' operator', v_atttype::regtype;
709+
RAISE EXCEPTION 'type % does not support ''-'' operator', v_atttype::REGTYPE;
744710
END IF;
745711

746712
SELECT range_interval
@@ -1004,7 +970,7 @@ BEGIN
1004970
/* check range overlap */
1005971
PERFORM @extschema@.check_range_available(parent_relid, start_value, end_value);
1006972

1007-
IF NOT @extschema@.tuple_format_is_convertable(parent_relid, partition_relid) THEN
973+
IF NOT @extschema@.is_tuple_convertible(parent_relid, partition_relid) THEN
1008974
RAISE EXCEPTION 'partition must have a compatible tuple format';
1009975
END IF;
1010976

@@ -1035,7 +1001,7 @@ BEGIN
10351001
INTO v_init_callback;
10361002

10371003
/* If update trigger is enabled then create one for this partition */
1038-
if @extschema@.is_update_trigger_enabled(parent_relid) THEN
1004+
if @extschema@.has_update_trigger(parent_relid) THEN
10391005
PERFORM @extschema@.create_single_update_trigger(parent_relid, partition_relid);
10401006
END IF;
10411007

@@ -1102,9 +1068,18 @@ END
11021068
$$
11031069
LANGUAGE plpgsql;
11041070

1071+
11051072
/*
1106-
* Drops partition and expands the next partition so that it cover dropped
1107-
* one
1073+
* Merge multiple partitions. All data will be copied to the first one.
1074+
* The rest of partitions will be dropped.
1075+
*/
1076+
CREATE OR REPLACE FUNCTION @extschema@.merge_range_partitions(
1077+
partitions REGCLASS[])
1078+
RETURNS VOID AS 'pg_pathman', 'merge_range_partitions'
1079+
LANGUAGE C STRICT;
1080+
1081+
/*
1082+
* Drops partition and expands the next partition so that it cover dropped one
11081083
*
11091084
* This function was written in order to support Oracle-like ALTER TABLE ...
11101085
* DROP PARTITION. In Oracle partitions only have upper bound and when
@@ -1115,6 +1090,15 @@ CREATE OR REPLACE FUNCTION @extschema@.drop_range_partition_expand_next(
11151090
RETURNS VOID AS 'pg_pathman', 'drop_range_partition_expand_next'
11161091
LANGUAGE C STRICT;
11171092

1093+
1094+
CREATE OR REPLACE FUNCTION @extschema@.create_range_partitions_internal(
1095+
parent_relid REGCLASS,
1096+
bounds ANYARRAY,
1097+
relnames TEXT[],
1098+
tablespaces TEXT[])
1099+
RETURNS REGCLASS AS 'pg_pathman', 'create_range_partitions_internal'
1100+
LANGUAGE C;
1101+
11181102
/*
11191103
* Creates new RANGE partition. Returns partition name.
11201104
* NOTE: This function SHOULD NOT take xact_handling lock (BGWs in 9.5).
@@ -1145,6 +1129,7 @@ CREATE OR REPLACE FUNCTION @extschema@.build_sequence_name(
11451129
RETURNS TEXT AS 'pg_pathman', 'build_sequence_name'
11461130
LANGUAGE C;
11471131

1132+
11481133
/*
11491134
* Returns N-th range (as an array of two elements).
11501135
*/
@@ -1176,10 +1161,18 @@ RETURNS VOID AS 'pg_pathman', 'check_range_available_pl'
11761161
LANGUAGE C;
11771162

11781163
/*
1179-
* Needed for an UPDATE trigger.
1164+
* Generate range bounds starting with 'p_start' using 'p_interval'.
11801165
*/
1181-
CREATE OR REPLACE FUNCTION @extschema@.find_or_create_range_partition(
1182-
parent_relid REGCLASS,
1183-
value ANYELEMENT)
1184-
RETURNS REGCLASS AS 'pg_pathman', 'find_or_create_range_partition'
1166+
CREATE OR REPLACE FUNCTION @extschema@.generate_range_bounds(
1167+
p_start ANYELEMENT,
1168+
p_interval INTERVAL,
1169+
p_count INTEGER)
1170+
RETURNS ANYARRAY AS 'pg_pathman', 'generate_range_bounds_pl'
1171+
LANGUAGE C;
1172+
1173+
CREATE OR REPLACE FUNCTION @extschema@.generate_range_bounds(
1174+
p_start ANYELEMENT,
1175+
p_interval ANYELEMENT,
1176+
p_count INTEGER)
1177+
RETURNS ANYARRAY AS 'pg_pathman', 'generate_range_bounds_pl'
11851178
LANGUAGE C;

src/include/init.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,18 @@ find_children_status find_inheritance_children_array(Oid parentrelId,
191191
uint32 *children_size,
192192
Oid **children);
193193

194+
194195
char *build_check_constraint_name_relid_internal(Oid relid,
195196
AttrNumber attno);
196-
197197
char *build_check_constraint_name_relname_internal(const char *relname,
198198
AttrNumber attno);
199199

200200
char *build_sequence_name_internal(Oid relid);
201201

202+
char *build_update_trigger_name_internal(Oid relid);
203+
char *build_update_trigger_func_name_internal(Oid relid);
204+
205+
202206
bool pathman_config_contains_relation(Oid relid,
203207
Datum *values,
204208
bool *isnull,

0 commit comments

Comments
 (0)