Skip to content

Commit 4e2d48c

Browse files
committed
alter functions with renamed args (1.3)
1 parent 11faec2 commit 4e2d48c

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

pg_pathman--1.2--1.3.sql

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,71 @@ DROP FUNCTION @[email protected]_range_partition(REGCLASS, BOOLEAN);
6868
DROP FUNCTION @[email protected]_range_partition(REGCLASS, REGCLASS, ANYELEMENT, ANYELEMENT);
6969
DROP FUNCTION @[email protected]_range_partition(REGCLASS);
7070
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+
7175

7276
/* ------------------------------------------------------------------------
7377
* Alter functions' modifiers
7478
* ----------------------------------------------------------------------*/
7579
ALTER FUNCTION @[email protected]_set_param(REGCLASS, TEXT, ANYELEMENT) STRICT;
7680

81+
7782
/* ------------------------------------------------------------------------
7883
* (Re)create functions
7984
* ----------------------------------------------------------------------*/
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+
80136
CREATE OR REPLACE FUNCTION @[email protected]_init_callback(
81137
relation REGCLASS,
82138
callback REGPROCEDURE DEFAULT 0)
@@ -945,6 +1001,50 @@ RETURNS TEXT AS 'pg_pathman', 'build_range_condition'
9451001
LANGUAGE C;
9461002

9471003

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+
9481048
/* ------------------------------------------------------------------------
9491049
* Final words of wisdom
9501050
* ----------------------------------------------------------------------*/

0 commit comments

Comments
 (0)