Skip to content

Commit 04b1b20

Browse files
committed
changes to plpgsql funcs for enterprise edition
1 parent 063c111 commit 04b1b20

File tree

4 files changed

+87
-48
lines changed

4 files changed

+87
-48
lines changed

init.sql

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,12 @@ LANGUAGE plpgsql STRICT;
182182
*/
183183
CREATE OR REPLACE FUNCTION @extschema@.show_partition_list()
184184
RETURNS TABLE (
185-
parent REGCLASS,
186-
partition REGCLASS,
187-
parttype INT4,
188-
partattr TEXT,
189-
range_min TEXT,
190-
range_max TEXT)
185+
parent REGCLASS,
186+
"partition" REGCLASS,
187+
parttype INT4,
188+
partattr TEXT,
189+
range_min TEXT,
190+
range_max TEXT)
191191
AS 'pg_pathman', 'show_partition_list_internal'
192192
LANGUAGE C STRICT;
193193

@@ -580,27 +580,38 @@ SET pg_pathman.enable_partitionfilter = off; /* ensures that PartitionFilter is
580580
*/
581581
CREATE OR REPLACE FUNCTION @extschema@.copy_foreign_keys(
582582
parent_relid REGCLASS,
583-
partition REGCLASS)
583+
partition_relid REGCLASS)
584584
RETURNS VOID AS
585585
$$
586586
DECLARE
587587
rec RECORD;
588588

589589
BEGIN
590590
PERFORM @extschema@.validate_relname(parent_relid);
591-
PERFORM @extschema@.validate_relname(partition);
591+
PERFORM @extschema@.validate_relname(partition_relid);
592592

593593
FOR rec IN (SELECT oid as conid FROM pg_catalog.pg_constraint
594594
WHERE conrelid = parent_relid AND contype = 'f')
595595
LOOP
596596
EXECUTE format('ALTER TABLE %s ADD %s',
597-
partition::TEXT,
597+
partition_relid::TEXT,
598598
pg_catalog.pg_get_constraintdef(rec.conid));
599599
END LOOP;
600600
END
601601
$$ LANGUAGE plpgsql STRICT;
602602

603603

604+
/*
605+
* Partitioning key
606+
*/
607+
CREATE OR REPLACE FUNCTION @extschema@.get_partition_key(relid REGCLASS)
608+
RETURNS TEXT AS
609+
$$
610+
SELECT attname FROM pathman_config WHERE partrel = relid;
611+
$$
612+
LANGUAGE sql STRICT;
613+
614+
604615
/*
605616
* Create DDL trigger to call pathman_ddl_trigger_func().
606617
*/
@@ -659,6 +670,14 @@ CREATE OR REPLACE FUNCTION @extschema@.get_attribute_type(
659670
RETURNS REGTYPE AS 'pg_pathman', 'get_attribute_type_pl'
660671
LANGUAGE C STRICT;
661672

673+
/*
674+
* Return partition key type
675+
*/
676+
CREATE OR REPLACE FUNCTION @extschema@.get_partition_key_type(
677+
relid REGCLASS)
678+
RETURNS REGTYPE AS 'pg_pathman', 'get_partition_key_type'
679+
LANGUAGE C STRICT;
680+
662681
/*
663682
* Return tablespace name for specified relation.
664683
*/
@@ -769,7 +788,7 @@ LANGUAGE C STRICT;
769788
*/
770789
CREATE OR REPLACE FUNCTION @extschema@.invoke_on_partition_created_callback(
771790
parent_relid REGCLASS,
772-
partition REGCLASS,
791+
"partition" REGCLASS,
773792
init_callback REGPROCEDURE,
774793
start_value ANYELEMENT,
775794
end_value ANYELEMENT)
@@ -781,7 +800,7 @@ LANGUAGE C;
781800
*/
782801
CREATE OR REPLACE FUNCTION @extschema@.invoke_on_partition_created_callback(
783802
parent_relid REGCLASS,
784-
partition REGCLASS,
803+
"partition" REGCLASS,
785804
init_callback REGPROCEDURE)
786805
RETURNS VOID AS 'pg_pathman', 'invoke_on_partition_created_callback'
787806
LANGUAGE C;

range.sql

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ $$ LANGUAGE plpgsql;
439439
* Split RANGE partition
440440
*/
441441
CREATE OR REPLACE FUNCTION @extschema@.split_range_partition(
442-
partition REGCLASS,
442+
partition_relid REGCLASS,
443443
split_value ANYELEMENT,
444444
partition_name TEXT DEFAULT NULL,
445445
tablespace TEXT DEFAULT NULL,
@@ -456,13 +456,13 @@ DECLARE
456456
v_check_name TEXT;
457457

458458
BEGIN
459-
v_parent = @extschema@.get_parent_of_partition(partition);
459+
v_parent = @extschema@.get_parent_of_partition(partition_relid);
460460

461461
/* Acquire lock on parent */
462462
PERFORM @extschema@.lock_partitioned_relation(v_parent);
463463

464464
/* Acquire data modification lock (prevent further modifications) */
465-
PERFORM @extschema@.prevent_relation_modification(partition);
465+
PERFORM @extschema@.prevent_relation_modification(partition_relid);
466466

467467
SELECT attname, parttype
468468
FROM @extschema@.pathman_config
@@ -475,15 +475,15 @@ BEGIN
475475

476476
/* Check if this is a RANGE partition */
477477
IF v_part_type != 2 THEN
478-
RAISE EXCEPTION '"%" is not a RANGE partition', partition::TEXT;
478+
RAISE EXCEPTION '"%" is not a RANGE partition', partition_relid::TEXT;
479479
END IF;
480480

481481
v_atttype = @extschema@.get_attribute_type(v_parent, v_attname);
482482

483483
/* Get partition values range */
484484
EXECUTE format('SELECT @extschema@.get_part_range($1, NULL::%s)',
485485
@extschema@.get_base_type(v_atttype)::TEXT)
486-
USING partition
486+
USING partition_relid
487487
INTO p_range;
488488

489489
IF p_range IS NULL THEN
@@ -509,21 +509,21 @@ BEGIN
509509
v_attname, split_value, p_range[2]);
510510
EXECUTE format('WITH part_data AS (DELETE FROM %s WHERE %s RETURNING *)
511511
INSERT INTO %s SELECT * FROM part_data',
512-
partition::TEXT,
512+
partition_relid::TEXT,
513513
v_cond,
514514
v_new_partition);
515515

516516
/* Alter original partition */
517-
v_cond := @extschema@.build_range_condition(partition::regclass,
517+
v_cond := @extschema@.build_range_condition(partition_relid::regclass,
518518
v_attname, p_range[1], split_value);
519-
v_check_name := @extschema@.build_check_constraint_name(partition, v_attname);
519+
v_check_name := @extschema@.build_check_constraint_name(partition_relid, v_attname);
520520

521521
EXECUTE format('ALTER TABLE %s DROP CONSTRAINT %s',
522-
partition::TEXT,
522+
partition_relid::TEXT,
523523
v_check_name);
524524

525525
EXECUTE format('ALTER TABLE %s ADD CONSTRAINT %s CHECK (%s)',
526-
partition::TEXT,
526+
partition_relid::TEXT,
527527
v_check_name,
528528
v_cond);
529529

@@ -958,7 +958,7 @@ LANGUAGE plpgsql;
958958
* Drop range partition
959959
*/
960960
CREATE OR REPLACE FUNCTION @extschema@.drop_range_partition(
961-
partition REGCLASS,
961+
partition_relid REGCLASS,
962962
delete_data BOOLEAN DEFAULT TRUE)
963963
RETURNS TEXT AS
964964
$$
@@ -970,8 +970,8 @@ DECLARE
970970
v_part_type INTEGER;
971971

972972
BEGIN
973-
parent_relid := @extschema@.get_parent_of_partition(partition);
974-
part_name := partition::TEXT; /* save the name to be returned */
973+
parent_relid := @extschema@.get_parent_of_partition(partition_relid);
974+
part_name := partition_relid::TEXT; /* save the name to be returned */
975975

976976
SELECT parttype
977977
FROM @extschema@.pathman_config
@@ -980,7 +980,7 @@ BEGIN
980980

981981
/* Check if this is a RANGE partition */
982982
IF v_part_type != 2 THEN
983-
RAISE EXCEPTION '"%" is not a RANGE partition', partition::TEXT;
983+
RAISE EXCEPTION '"%" is not a RANGE partition', partition_relid::TEXT;
984984
END IF;
985985

986986
/* Acquire lock on parent */
@@ -989,15 +989,15 @@ BEGIN
989989
IF NOT delete_data THEN
990990
EXECUTE format('INSERT INTO %s SELECT * FROM %s',
991991
parent_relid::TEXT,
992-
partition::TEXT);
992+
partition_relid::TEXT);
993993
GET DIAGNOSTICS v_rows = ROW_COUNT;
994994

995995
/* Show number of copied rows */
996-
RAISE NOTICE '% rows copied from %', v_rows, partition::TEXT;
996+
RAISE NOTICE '% rows copied from %', v_rows, partition_relid::TEXT;
997997
END IF;
998998

999999
SELECT relkind FROM pg_catalog.pg_class
1000-
WHERE oid = partition
1000+
WHERE oid = partition_relid
10011001
INTO v_relkind;
10021002

10031003
/*
@@ -1006,9 +1006,9 @@ BEGIN
10061006
* DROP TABLE or DROP FOREIGN TABLE.
10071007
*/
10081008
IF v_relkind = 'f' THEN
1009-
EXECUTE format('DROP FOREIGN TABLE %s', partition::TEXT);
1009+
EXECUTE format('DROP FOREIGN TABLE %s', partition_relid::TEXT);
10101010
ELSE
1011-
EXECUTE format('DROP TABLE %s', partition::TEXT);
1011+
EXECUTE format('DROP TABLE %s', partition_relid::TEXT);
10121012
END IF;
10131013

10141014
/* Invalidate cache */
@@ -1026,7 +1026,7 @@ SET pg_pathman.enable_partitionfilter = off; /* ensures that PartitionFilter is
10261026
*/
10271027
CREATE OR REPLACE FUNCTION @extschema@.attach_range_partition(
10281028
parent_relid REGCLASS,
1029-
partition REGCLASS,
1029+
partition_relid REGCLASS,
10301030
start_value ANYELEMENT,
10311031
end_value ANYELEMENT)
10321032
RETURNS TEXT AS
@@ -1038,29 +1038,29 @@ DECLARE
10381038

10391039
BEGIN
10401040
PERFORM @extschema@.validate_relname(parent_relid);
1041-
PERFORM @extschema@.validate_relname(partition);
1041+
PERFORM @extschema@.validate_relname(partition_relid);
10421042

10431043
/* Acquire lock on parent */
10441044
PERFORM @extschema@.lock_partitioned_relation(parent_relid);
10451045

10461046
/* Ignore temporary tables */
10471047
SELECT relpersistence FROM pg_catalog.pg_class
1048-
WHERE oid = partition INTO rel_persistence;
1048+
WHERE oid = partition_relid INTO rel_persistence;
10491049

10501050
IF rel_persistence = 't'::CHAR THEN
10511051
RAISE EXCEPTION 'temporary table "%" cannot be used as a partition',
1052-
partition::TEXT;
1052+
partition_relid::TEXT;
10531053
END IF;
10541054

10551055
/* check range overlap */
10561056
PERFORM @extschema@.check_range_available(parent_relid, start_value, end_value);
10571057

1058-
IF NOT @extschema@.validate_relations_equality(parent_relid, partition) THEN
1058+
IF NOT @extschema@.validate_relations_equality(parent_relid, partition_relid) THEN
10591059
RAISE EXCEPTION 'partition must have the exact same structure as parent';
10601060
END IF;
10611061

10621062
/* Set inheritance */
1063-
EXECUTE format('ALTER TABLE %s INHERIT %s', partition, parent_relid);
1063+
EXECUTE format('ALTER TABLE %s INHERIT %s', partition_relid, parent_relid);
10641064

10651065
v_attname := attname FROM @extschema@.pathman_config WHERE partrel = parent_relid;
10661066

@@ -1070,9 +1070,9 @@ BEGIN
10701070

10711071
/* Set check constraint */
10721072
EXECUTE format('ALTER TABLE %s ADD CONSTRAINT %s CHECK (%s)',
1073-
partition::TEXT,
1074-
@extschema@.build_check_constraint_name(partition, v_attname),
1075-
@extschema@.build_range_condition(partition,
1073+
partition_relid::TEXT,
1074+
@extschema@.build_check_constraint_name(partition_relid, v_attname),
1075+
@extschema@.build_range_condition(partition_relid,
10761076
v_attname,
10771077
start_value,
10781078
end_value));
@@ -1086,15 +1086,15 @@ BEGIN
10861086
INTO v_init_callback;
10871087

10881088
PERFORM @extschema@.invoke_on_partition_created_callback(parent_relid,
1089-
partition,
1089+
partition_relid,
10901090
v_init_callback,
10911091
start_value,
10921092
end_value);
10931093

10941094
/* Invalidate cache */
10951095
PERFORM @extschema@.on_update_partitions(parent_relid);
10961096

1097-
RETURN partition;
1097+
RETURN partition_relid;
10981098
END
10991099
$$
11001100
LANGUAGE plpgsql;
@@ -1104,15 +1104,15 @@ LANGUAGE plpgsql;
11041104
* Detach range partition
11051105
*/
11061106
CREATE OR REPLACE FUNCTION @extschema@.detach_range_partition(
1107-
partition REGCLASS)
1107+
partition_relid REGCLASS)
11081108
RETURNS TEXT AS
11091109
$$
11101110
DECLARE
11111111
v_attname TEXT;
11121112
parent_relid REGCLASS;
11131113

11141114
BEGIN
1115-
parent_relid := @extschema@.get_parent_of_partition(partition);
1115+
parent_relid := @extschema@.get_parent_of_partition(partition_relid);
11161116

11171117
/* Acquire lock on parent */
11181118
PERFORM @extschema@.lock_partitioned_relation(parent_relid);
@@ -1127,18 +1127,18 @@ BEGIN
11271127

11281128
/* Remove inheritance */
11291129
EXECUTE format('ALTER TABLE %s NO INHERIT %s',
1130-
partition::TEXT,
1130+
partition_relid::TEXT,
11311131
parent_relid::TEXT);
11321132

11331133
/* Remove check constraint */
11341134
EXECUTE format('ALTER TABLE %s DROP CONSTRAINT %s',
1135-
partition::TEXT,
1136-
@extschema@.build_check_constraint_name(partition, v_attname));
1135+
partition_relid::TEXT,
1136+
@extschema@.build_check_constraint_name(partition_relid, v_attname));
11371137

11381138
/* Invalidate cache */
11391139
PERFORM @extschema@.on_update_partitions(parent_relid);
11401140

1141-
RETURN partition;
1141+
RETURN partition_relid;
11421142
END
11431143
$$
11441144
LANGUAGE plpgsql;

src/partition_creation.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,9 @@ create_single_partition_internal(Oid parent_relid,
780780
create_stmt.oncommit = ONCOMMIT_NOOP;
781781
create_stmt.tablespacename = tablespace;
782782
create_stmt.if_not_exists = false;
783+
#ifdef PGPRO_VERSION
784+
create_stmt.partition_info = NULL;
785+
#endif
783786

784787
/* Do we have to escalate privileges? */
785788
if (need_priv_escalation)

src/pl_funcs.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ PG_FUNCTION_INFO_V1( get_number_of_partitions_pl );
4242
PG_FUNCTION_INFO_V1( get_parent_of_partition_pl );
4343
PG_FUNCTION_INFO_V1( get_base_type_pl );
4444
PG_FUNCTION_INFO_V1( get_attribute_type_pl );
45+
PG_FUNCTION_INFO_V1( get_partition_key_type );
4546
PG_FUNCTION_INFO_V1( get_tablespace_pl );
4647

4748
PG_FUNCTION_INFO_V1( show_partition_list_internal );
@@ -228,6 +229,23 @@ get_attribute_type_pl(PG_FUNCTION_ARGS)
228229
PG_RETURN_OID(get_attribute_type(relid, text_to_cstring(attname), false));
229230
}
230231

232+
/*
233+
* Return partition key type
234+
*/
235+
Datum
236+
get_partition_key_type(PG_FUNCTION_ARGS)
237+
{
238+
Oid relid = PG_GETARG_OID(0);
239+
const PartRelationInfo *prel = get_pathman_relation_info(relid);
240+
241+
if (!prel)
242+
elog(ERROR,
243+
"Relation '%s' isn't partitioned by pg_pathman",
244+
get_rel_name(relid));
245+
246+
PG_RETURN_OID(prel->atttype);
247+
}
248+
231249
/*
232250
* Return tablespace name for specified relation
233251
*/
@@ -254,7 +272,6 @@ get_tablespace_pl(PG_FUNCTION_ARGS)
254272
PG_RETURN_TEXT_P(cstring_to_text(result));
255273
}
256274

257-
258275
/*
259276
* ----------------------
260277
* Common purpose VIEWs

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy