@@ -68,15 +68,71 @@ DROP FUNCTION @
[email protected] _range_partition(REGCLASS, BOOLEAN);
68
68
DROP FUNCTION @
[email protected] _range_partition(REGCLASS, REGCLASS, ANYELEMENT, ANYELEMENT);
69
69
DROP FUNCTION @
[email protected] _range_partition(REGCLASS);
70
70
DROP FUNCTION @
[email protected] _range_partitions_internal(REGCLASS, REGCLASS, REGCLASS, ANYELEMENT);
71
+ DROP FUNCTION @
[email protected] _foreign_keys(REGCLASS, REGCLASS);
72
+ DROP FUNCTION @
[email protected] _on_partition_created_callback(REGCLASS, REGCLASS, REGPROCEDURE, ANYELEMENT, ANYELEMENT);
73
+ DROP FUNCTION @
[email protected] _on_partition_created_callback(REGCLASS, REGCLASS, REGPROCEDURE);
74
+
71
75
72
76
/* ------------------------------------------------------------------------
73
77
* Alter functions' modifiers
74
78
* ----------------------------------------------------------------------*/
75
79
ALTER FUNCTION @
[email protected] _set_param(REGCLASS,
TEXT , ANYELEMENT) STRICT;
76
80
81
+
77
82
/* ------------------------------------------------------------------------
78
83
* (Re)create functions
79
84
* ----------------------------------------------------------------------*/
85
+
86
+ /*
87
+ * Invoke init_callback on RANGE partition.
88
+ */
89
+ CREATE
OR REPLACE FUNCTION @
[email protected] _on_partition_created_callback(
90
+ parent_relid REGCLASS,
91
+ partition_relid REGCLASS,
92
+ init_callback REGPROCEDURE,
93
+ start_value ANYELEMENT,
94
+ end_value ANYELEMENT)
95
+ RETURNS VOID AS ' pg_pathman' , ' invoke_on_partition_created_callback'
96
+ LANGUAGE C;
97
+
98
+
99
+ /*
100
+ * Invoke init_callback on HASH partition.
101
+ */
102
+ CREATE
OR REPLACE FUNCTION @
[email protected] _on_partition_created_callback(
103
+ parent_relid REGCLASS,
104
+ partition_relid REGCLASS,
105
+ init_callback REGPROCEDURE)
106
+ RETURNS VOID AS ' pg_pathman' , ' invoke_on_partition_created_callback'
107
+ LANGUAGE C;
108
+
109
+
110
+ /*
111
+ * Copy all of parent's foreign keys.
112
+ */
113
+ CREATE
OR REPLACE FUNCTION @
[email protected] _foreign_keys(
114
+ parent_relid REGCLASS,
115
+ partition_relid REGCLASS)
116
+ RETURNS VOID AS
117
+ $$
118
+ DECLARE
119
+ rec RECORD;
120
+
121
+ BEGIN
122
+ PERFORM @
[email protected] _relname(parent_relid);
123
+ PERFORM @
[email protected] _relname(partition_relid);
124
+
125
+ FOR rec IN (SELECT oid as conid FROM pg_catalog .pg_constraint
126
+ WHERE conrelid = parent_relid AND contype = ' f' )
127
+ LOOP
128
+ EXECUTE format(' ALTER TABLE %s ADD %s' ,
129
+ partition_relid::TEXT ,
130
+ pg_catalog .pg_get_constraintdef (rec .conid ));
131
+ END LOOP;
132
+ END
133
+ $$ LANGUAGE plpgsql STRICT;
134
+
135
+
80
136
CREATE
OR REPLACE FUNCTION @
[email protected] _init_callback(
81
137
relation REGCLASS,
82
138
callback REGPROCEDURE DEFAULT 0 )
@@ -945,6 +1001,50 @@ RETURNS TEXT AS 'pg_pathman', 'build_range_condition'
945
1001
LANGUAGE C;
946
1002
947
1003
1004
+ /*
1005
+ * Old school way to distribute rows to partitions.
1006
+ */
1007
+ CREATE
OR REPLACE FUNCTION @
[email protected] _data(
1008
+ parent_relid REGCLASS,
1009
+ OUT p_total BIGINT )
1010
+ AS
1011
+ $$
1012
+ BEGIN
1013
+ p_total := 0 ;
1014
+
1015
+ /* Create partitions and copy rest of the data */
1016
+ EXECUTE format(' WITH part_data AS (DELETE FROM ONLY %1$s RETURNING *)
1017
+ INSERT INTO %1$s SELECT * FROM part_data' ,
1018
+ parent_relid::TEXT );
1019
+
1020
+ /* Get number of inserted rows */
1021
+ GET DIAGNOSTICS p_total = ROW_COUNT;
1022
+ RETURN;
1023
+ END
1024
+ $$
1025
+ LANGUAGE plpgsql STRICT
1026
+ SET pg_pathman .enable_partitionfilter = on ;
1027
+
1028
+ /*
1029
+ * Add a row describing the optional parameter to pathman_config_params.
1030
+ */
1031
+ CREATE
OR REPLACE FUNCTION @
[email protected] _set_param(
1032
+ relation REGCLASS,
1033
+ param TEXT ,
1034
+ value ANYELEMENT)
1035
+ RETURNS VOID AS
1036
+ $$
1037
+ BEGIN
1038
+ EXECUTE format(
' INSERT INTO @[email protected] _config_params
1039
+ (partrel, %1$s) VALUES ($1, $2)
1040
+ ON CONFLICT (partrel) DO UPDATE SET %1$s = $2' , param)
1041
+ USING relation, value;
1042
+ END
1043
+ $$
1044
+ LANGUAGE plpgsql;
1045
+
1046
+
1047
+
948
1048
/* ------------------------------------------------------------------------
949
1049
* Final words of wisdom
950
1050
* ----------------------------------------------------------------------*/
0 commit comments