Skip to content

Commit b5d6405

Browse files
committed
WIP refactoring, introduce has_pathman_relation_info()
1 parent 01d9ad6 commit b5d6405

10 files changed

+177
-166
lines changed

init.sql

+33-57
Original file line numberDiff line numberDiff line change
@@ -513,38 +513,6 @@ BEGIN
513513
END
514514
$$ LANGUAGE plpgsql;
515515

516-
517-
CREATE OR REPLACE FUNCTION @extschema@.create_naming_sequence(
518-
parent_relid REGCLASS)
519-
RETURNS TEXT AS $$
520-
DECLARE
521-
seq_name TEXT;
522-
523-
BEGIN
524-
seq_name := @extschema@.build_sequence_name(parent_relid);
525-
526-
EXECUTE format('DROP SEQUENCE IF EXISTS %s', seq_name);
527-
EXECUTE format('CREATE SEQUENCE %s START 1', seq_name);
528-
529-
RETURN seq_name;
530-
END
531-
$$ LANGUAGE plpgsql
532-
SET client_min_messages = WARNING; /* mute NOTICE message */
533-
534-
CREATE OR REPLACE FUNCTION @extschema@.drop_naming_sequence(
535-
parent_relid REGCLASS)
536-
RETURNS VOID AS $$
537-
DECLARE
538-
seq_name TEXT;
539-
540-
BEGIN
541-
seq_name := @extschema@.build_sequence_name(parent_relid);
542-
543-
EXECUTE format('DROP SEQUENCE IF EXISTS %s', seq_name);
544-
END
545-
$$ LANGUAGE plpgsql
546-
SET client_min_messages = WARNING; /* mute NOTICE message */
547-
548516
/*
549517
* Drop partitions. If delete_data set to TRUE, partitions
550518
* will be dropped with all the data.
@@ -686,43 +654,51 @@ EXECUTE PROCEDURE @extschema@.pathman_ddl_trigger_func();
686654

687655

688656
/*
689-
* Partitioning key.
657+
* Get partitioning key.
690658
*/
691659
CREATE OR REPLACE FUNCTION @extschema@.get_partition_key(
692-
relid REGCLASS)
660+
parent_relid REGCLASS)
693661
RETURNS TEXT AS
694662
$$
695-
SELECT expr FROM @extschema@.pathman_config WHERE partrel = relid;
663+
SELECT expr
664+
FROM @extschema@.pathman_config
665+
WHERE partrel = parent_relid;
696666
$$
697667
LANGUAGE sql STRICT;
698668

699669
/*
700-
* Partitioning key type.
670+
* Get partitioning key type.
701671
*/
702672
CREATE OR REPLACE FUNCTION @extschema@.get_partition_key_type(
703-
relid REGCLASS)
704-
RETURNS REGTYPE AS 'pg_pathman', 'get_partition_key_type'
673+
parent_relid REGCLASS)
674+
RETURNS REGTYPE AS 'pg_pathman', 'get_partition_key_type_pl'
705675
LANGUAGE C STRICT;
706676

707677
/*
708-
* Partitioning type.
678+
* Get partitioning type.
709679
*/
710680
CREATE OR REPLACE FUNCTION @extschema@.get_partition_type(
711-
relid REGCLASS)
681+
parent_relid REGCLASS)
712682
RETURNS INT4 AS
713683
$$
714-
SELECT parttype FROM @extschema@.pathman_config WHERE partrel = relid;
684+
SELECT parttype
685+
FROM @extschema@.pathman_config
686+
WHERE partrel = parent_relid;
715687
$$
716688
LANGUAGE sql STRICT;
717689

718-
719690
/*
720691
* Get number of partitions managed by pg_pathman.
721692
*/
722693
CREATE OR REPLACE FUNCTION @extschema@.get_number_of_partitions(
723-
parent_relid REGCLASS)
724-
RETURNS INT4 AS 'pg_pathman', 'get_number_of_partitions_pl'
725-
LANGUAGE C STRICT;
694+
parent_relid REGCLASS)
695+
RETURNS INT4 AS
696+
$$
697+
SELECT count(*)::INT4
698+
FROM pg_catalog.pg_inherits
699+
WHERE inhparent = parent_relid;
700+
$$
701+
LANGUAGE sql STRICT;
726702

727703
/*
728704
* Get parent of pg_pathman's partition.
@@ -806,18 +782,18 @@ LANGUAGE C STRICT;
806782
* Add record to pathman_config (RANGE) and validate partitions.
807783
*/
808784
CREATE OR REPLACE FUNCTION @extschema@.add_to_pathman_config(
809-
parent_relid REGCLASS,
810-
expression TEXT,
811-
range_interval TEXT)
785+
parent_relid REGCLASS,
786+
expression TEXT,
787+
range_interval TEXT)
812788
RETURNS BOOLEAN AS 'pg_pathman', 'add_to_pathman_config'
813789
LANGUAGE C;
814790

815791
/*
816792
* Add record to pathman_config (HASH) and validate partitions.
817793
*/
818794
CREATE OR REPLACE FUNCTION @extschema@.add_to_pathman_config(
819-
parent_relid REGCLASS,
820-
expression TEXT)
795+
parent_relid REGCLASS,
796+
expression TEXT)
821797
RETURNS BOOLEAN AS 'pg_pathman', 'add_to_pathman_config'
822798
LANGUAGE C;
823799

@@ -866,9 +842,9 @@ LANGUAGE C;
866842
* Get parent of pg_pathman's partition.
867843
*/
868844
CREATE OR REPLACE FUNCTION @extschema@.is_equal_to_partitioning_expression(
869-
parent_relid REGCLASS,
870-
expression TEXT,
871-
value_type OID)
845+
parent_relid REGCLASS,
846+
expression TEXT,
847+
value_type OID)
872848
RETURNS BOOL AS 'pg_pathman', 'is_equal_to_partitioning_expression_pl'
873849
LANGUAGE C STRICT;
874850

@@ -877,8 +853,8 @@ LANGUAGE C STRICT;
877853
* bound_value is used to determine the type of bound
878854
*/
879855
CREATE OR REPLACE FUNCTION @extschema@.get_lower_bound(
880-
relid REGCLASS,
881-
bound_value ANYELEMENT
856+
relid REGCLASS,
857+
bound_value ANYELEMENT
882858
)
883859
RETURNS ANYELEMENT AS 'pg_pathman', 'get_lower_bound_pl'
884860
LANGUAGE C STRICT;
@@ -887,8 +863,8 @@ LANGUAGE C STRICT;
887863
* Get upper bound of a partition
888864
*/
889865
CREATE OR REPLACE FUNCTION @extschema@.get_upper_bound(
890-
relid REGCLASS,
891-
bound_value ANYELEMENT
866+
relid REGCLASS,
867+
bound_value ANYELEMENT
892868
)
893869
RETURNS ANYELEMENT AS 'pg_pathman', 'get_upper_bound_pl'
894870
LANGUAGE C STRICT;

range.sql

+41-2
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,44 @@ END
10201020
$$ LANGUAGE plpgsql;
10211021

10221022

1023+
/*
1024+
* Create a naming sequence for partitioned table.
1025+
*/
1026+
CREATE OR REPLACE FUNCTION @extschema@.create_naming_sequence(
1027+
parent_relid REGCLASS)
1028+
RETURNS TEXT AS $$
1029+
DECLARE
1030+
seq_name TEXT;
1031+
1032+
BEGIN
1033+
seq_name := @extschema@.build_sequence_name(parent_relid);
1034+
1035+
EXECUTE format('DROP SEQUENCE IF EXISTS %s', seq_name);
1036+
EXECUTE format('CREATE SEQUENCE %s START 1', seq_name);
1037+
1038+
RETURN seq_name;
1039+
END
1040+
$$ LANGUAGE plpgsql
1041+
SET client_min_messages = WARNING; /* mute NOTICE message */
1042+
1043+
/*
1044+
* Drop a naming sequence for partitioned table.
1045+
*/
1046+
CREATE OR REPLACE FUNCTION @extschema@.drop_naming_sequence(
1047+
parent_relid REGCLASS)
1048+
RETURNS VOID AS $$
1049+
DECLARE
1050+
seq_name TEXT;
1051+
1052+
BEGIN
1053+
seq_name := @extschema@.build_sequence_name(parent_relid);
1054+
1055+
EXECUTE format('DROP SEQUENCE IF EXISTS %s', seq_name);
1056+
END
1057+
$$ LANGUAGE plpgsql
1058+
SET client_min_messages = WARNING; /* mute NOTICE message */
1059+
1060+
10231061
/*
10241062
* Merge multiple partitions. All data will be copied to the first one.
10251063
* The rest of partitions will be dropped.
@@ -1041,7 +1079,6 @@ CREATE OR REPLACE FUNCTION @extschema@.drop_range_partition_expand_next(
10411079
RETURNS VOID AS 'pg_pathman', 'drop_range_partition_expand_next'
10421080
LANGUAGE C STRICT;
10431081

1044-
10451082
CREATE OR REPLACE FUNCTION @extschema@.create_range_partitions_internal(
10461083
parent_relid REGCLASS,
10471084
bounds ANYARRAY,
@@ -1075,12 +1112,14 @@ CREATE OR REPLACE FUNCTION @extschema@.build_range_condition(
10751112
RETURNS TEXT AS 'pg_pathman', 'build_range_condition'
10761113
LANGUAGE C;
10771114

1115+
/*
1116+
* Generate a name for naming sequence.
1117+
*/
10781118
CREATE OR REPLACE FUNCTION @extschema@.build_sequence_name(
10791119
parent_relid REGCLASS)
10801120
RETURNS TEXT AS 'pg_pathman', 'build_sequence_name'
10811121
LANGUAGE C STRICT;
10821122

1083-
10841123
/*
10851124
* Returns N-th range (as an array of two elements).
10861125
*/

src/hooks.c

+19-20
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ allow_star_schema_join(PlannerInfo *root,
6363
}
6464

6565

66-
set_join_pathlist_hook_type set_join_pathlist_next = NULL;
67-
set_rel_pathlist_hook_type set_rel_pathlist_hook_next = NULL;
68-
planner_hook_type planner_hook_next = NULL;
69-
post_parse_analyze_hook_type post_parse_analyze_hook_next = NULL;
70-
shmem_startup_hook_type shmem_startup_hook_next = NULL;
71-
ProcessUtility_hook_type process_utility_hook_next = NULL;
72-
ExecutorRun_hook_type executor_run_hook_next = NULL;
66+
set_join_pathlist_hook_type set_join_pathlist_next = NULL;
67+
set_rel_pathlist_hook_type set_rel_pathlist_hook_next = NULL;
68+
planner_hook_type planner_hook_next = NULL;
69+
post_parse_analyze_hook_type post_parse_analyze_hook_next = NULL;
70+
shmem_startup_hook_type shmem_startup_hook_next = NULL;
71+
ProcessUtility_hook_type process_utility_hook_next = NULL;
72+
ExecutorRun_hook_type executor_run_hook_next = NULL;
7373

7474

7575
/* Take care of joins */
@@ -101,7 +101,7 @@ pathman_join_pathlist_hook(PlannerInfo *root,
101101
if (!IsPathmanReady() || !pg_pathman_enable_runtimeappend)
102102
return;
103103

104-
/* We should only consider base relations */
104+
/* We should only consider base inner relations */
105105
if (innerrel->reloptkind != RELOPT_BASEREL)
106106
return;
107107

@@ -113,9 +113,13 @@ pathman_join_pathlist_hook(PlannerInfo *root,
113113
if (jointype == JOIN_FULL || jointype == JOIN_RIGHT)
114114
return;
115115

116-
/* Check that innerrel is a BASEREL with PartRelationInfo */
117-
if (innerrel->reloptkind != RELOPT_BASEREL ||
118-
!(inner_prel = get_pathman_relation_info(inner_rte->relid)))
116+
/* Skip if inner table is not allowed to act as parent (e.g. FROM ONLY) */
117+
if (PARENTHOOD_DISALLOWED == get_rel_parenthood_status(root->parse->queryId,
118+
inner_rte))
119+
return;
120+
121+
/* Proceed iff relation 'innerrel' is partitioned */
122+
if ((inner_prel = get_pathman_relation_info(inner_rte->relid)) == NULL)
119123
return;
120124

121125
/*
@@ -142,7 +146,7 @@ pathman_join_pathlist_hook(PlannerInfo *root,
142146
Oid outer_baserel = root->simple_rte_array[rti]->relid;
143147

144148
/* Is it partitioned? */
145-
if (get_pathman_relation_info(outer_baserel))
149+
if (has_pathman_relation_info(outer_baserel))
146150
count++;
147151
}
148152

@@ -153,11 +157,6 @@ pathman_join_pathlist_hook(PlannerInfo *root,
153157
"of partitioned tables are not supported")));
154158
}
155159

156-
/* Skip if inner table is not allowed to act as parent (e.g. FROM ONLY) */
157-
if (PARENTHOOD_DISALLOWED == get_rel_parenthood_status(root->parse->queryId,
158-
inner_rte))
159-
return;
160-
161160
/*
162161
* These codes are used internally in the planner, but are not supported
163162
* by the executor (nor, indeed, by most of the planner).
@@ -223,7 +222,7 @@ pathman_join_pathlist_hook(PlannerInfo *root,
223222
Assert(outer);
224223
}
225224

226-
/* No way to do this in a parameterized inner path */
225+
/* No way to do this in a parameterized inner path */
227226
if (saved_jointype == JOIN_UNIQUE_INNER)
228227
return;
229228

@@ -607,7 +606,7 @@ pathman_enable_assign_hook(bool newval, void *extra)
607606
/*
608607
* Planner hook. It disables inheritance for tables that have been partitioned
609608
* by pathman to prevent standart PostgreSQL partitioning mechanism from
610-
* handling that tables.
609+
* handling those tables.
611610
*/
612611
PlannedStmt *
613612
pathman_planner_hook(Query *parse, int cursorOptions, ParamListInfo boundParams)
@@ -679,7 +678,7 @@ pathman_planner_hook(Query *parse, int cursorOptions, ParamListInfo boundParams)
679678

680679
/*
681680
* Post parse analysis hook. It makes sure the config is loaded before executing
682-
* any statement, including utility commands
681+
* any statement, including utility commands.
683682
*/
684683
void
685684
pathman_post_parse_analysis_hook(ParseState *pstate, Query *query)

src/include/relation_info.h

+5-4
Original file line numberDiff line numberDiff line change
@@ -311,10 +311,11 @@ void refresh_pathman_relation_info(Oid relid);
311311
void invalidate_pathman_relation_info(Oid relid);
312312
void invalidate_pathman_relation_info_cache(void);
313313
void close_pathman_relation_info(PartRelationInfo *prel);
314-
const PartRelationInfo *get_pathman_relation_info(Oid relid);
315-
const PartRelationInfo *get_pathman_relation_info_after_lock(Oid relid,
316-
bool unlock_if_not_found,
317-
LockAcquireResult *lock_result);
314+
bool has_pathman_relation_info(Oid relid);
315+
PartRelationInfo *get_pathman_relation_info(Oid relid);
316+
PartRelationInfo *get_pathman_relation_info_after_lock(Oid relid,
317+
bool unlock_if_not_found,
318+
LockAcquireResult *lock_result);
318319

319320
void shout_if_prel_is_invalid(const Oid parent_oid,
320321
const PartRelationInfo *prel,

src/include/xact_handling.h

-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,5 @@ bool xact_is_set_stmt(Node *stmt, const char *name);
3232
bool xact_is_alter_pathman_stmt(Node *stmt);
3333
bool xact_object_is_visible(TransactionId obj_xmin);
3434

35-
void prevent_data_modification_internal(Oid relid);
36-
3735

3836
#endif /* XACT_HANDLING_H */

src/pathman_workers.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ bgw_main_concurrent_part(Datum main_arg)
547547
}
548548

549549
/* Make sure that relation has partitions */
550-
if (get_pathman_relation_info(part_slot->relid) == NULL)
550+
if (!has_pathman_relation_info(part_slot->relid))
551551
{
552552
/* Exit after we raise ERROR */
553553
failures_count = PART_WORKER_MAX_ATTEMPTS;

0 commit comments

Comments
 (0)