Skip to content

Commit c9b5097

Browse files
committed
check that table is partitioned, create_range_update_trigger() now takes regclass
1 parent 2326e59 commit c9b5097

File tree

2 files changed

+82
-27
lines changed

2 files changed

+82
-27
lines changed

hash.sql

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,12 +243,10 @@ DECLARE
243243
atttype TEXT;
244244
hashfunc TEXT;
245245
BEGIN
246-
relation := @extschema@.validate_relname(relation);
247-
248246
SELECT * INTO plain_schema, plain_relname
249247
FROM @extschema@.get_plain_schema_and_relname(relation);
250248

251-
relid := relation::regclass::oid;
249+
relid := relation::oid;
252250
SELECT string_agg(attname, ', '),
253251
string_agg('OLD.' || attname, ', '),
254252
string_agg('NEW.' || attname, ', '),
@@ -264,6 +262,11 @@ BEGIN
264262
att_fmt;
265263

266264
attr := attname FROM @extschema@.pathman_config WHERE relname::regclass = relation;
265+
266+
IF attr IS NULL THEN
267+
RAISE EXCEPTION 'Table % is not partitioned', quote_ident(relation::TEXT);
268+
END IF;
269+
267270
partitions_count := COUNT(*) FROM pg_inherits WHERE inhparent = relation::oid;
268271

269272
/* Function name, trigger name and child relname template */

range.sql

Lines changed: 76 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,10 @@ BEGIN
423423
v_attname := attname FROM @extschema@.pathman_config
424424
WHERE relname::regclass = p_parent;
425425

426+
IF v_attname IS NULL THEN
427+
RAISE EXCEPTION 'Table % is not partitioned', quote_ident(p_parent::TEXT);
428+
END IF;
429+
426430
SELECT * INTO v_plain_schema, v_plain_relname
427431
FROM @extschema@.get_plain_schema_and_relname(p_parent);
428432

@@ -494,9 +498,15 @@ BEGIN
494498
FROM pg_inherits
495499
WHERE inhrelid = v_child_relid;
496500

497-
SELECT attname, parttype INTO v_attname, v_part_type
501+
SELECT attname, parttype
498502
FROM @extschema@.pathman_config
499-
WHERE relname::regclass = v_parent_relid::regclass;
503+
WHERE relname::regclass = v_parent_relid::regclass
504+
INTO v_attname, v_part_type;
505+
506+
IF v_attname IS NULL THEN
507+
RAISE EXCEPTION 'Table % is not partitioned',
508+
quote_ident(v_parent_relid::regclass::text);
509+
END IF;
500510

501511
SELECT * INTO v_plain_schema, v_plain_relname
502512
FROM @extschema@.get_plain_schema_and_relname(p_partition);
@@ -591,9 +601,15 @@ BEGIN
591601
RAISE EXCEPTION 'Cannot merge partitions having different parents';
592602
END IF;
593603

594-
SELECT attname, parttype INTO v_attname, v_part_type
604+
SELECT attname, parttype
595605
FROM @extschema@.pathman_config
596-
WHERE relname::regclass = v_parent_relid1::regclass;
606+
WHERE relname::regclass = v_parent_relid1::regclass
607+
INTO v_attname, v_part_type;
608+
609+
IF v_attname IS NULL THEN
610+
RAISE EXCEPTION 'Table % is not partitioned',
611+
quote_ident(v_parent_relid1::regclass::text);
612+
END IF;
597613

598614
/* Check if this is RANGE partition */
599615
IF v_part_type != 2 THEN
@@ -637,8 +653,14 @@ DECLARE
637653
v_child_relname TEXT;
638654
v_check_name TEXT;
639655
BEGIN
640-
SELECT attname INTO v_attname FROM @extschema@.pathman_config
641-
WHERE relname::regclass = p_parent_relid::regclass;
656+
SELECT attname FROM @extschema@.pathman_config
657+
WHERE relname::regclass = p_parent_relid::regclass
658+
INTO v_attname;
659+
660+
IF v_attname IS NULL THEN
661+
RAISE EXCEPTION 'Table % is not partitioned',
662+
quote_ident(p_parent_relid::regclass::text);
663+
END IF;
642664

643665
SELECT * INTO v_plain_schema, v_plain_relname
644666
FROM @extschema@.get_plain_schema_and_relname(p_part1);
@@ -694,16 +716,22 @@ CREATE OR REPLACE FUNCTION @extschema@.append_range_partition(
694716
RETURNS TEXT AS
695717
$$
696718
DECLARE
697-
v_attname TEXT;
698-
v_atttype TEXT;
699-
v_part_name TEXT;
700-
v_interval TEXT;
719+
v_attname TEXT;
720+
v_atttype TEXT;
721+
v_part_name TEXT;
722+
v_interval TEXT;
701723
BEGIN
702724
/* Prevent concurrent partition creation */
703725
PERFORM @extschema@.acquire_partitions_lock();
704726

705-
SELECT attname, range_interval INTO v_attname, v_interval
706-
FROM @extschema@.pathman_config WHERE relname::regclass = p_relation;
727+
SELECT attname, range_interval
728+
FROM @extschema@.pathman_config
729+
WHERE relname::regclass = p_relation
730+
INTO v_attname, v_interval;
731+
732+
IF v_attname IS NULL THEN
733+
RAISE EXCEPTION 'Table % is not partitioned', quote_ident(p_relation::TEXT);
734+
END IF;
707735

708736
v_atttype := @extschema@.get_attribute_type_name(p_relation, v_attname);
709737

@@ -770,8 +798,15 @@ BEGIN
770798
/* Prevent concurrent partition creation */
771799
PERFORM @extschema@.acquire_partitions_lock();
772800

773-
SELECT attname, range_interval INTO v_attname, v_interval
774-
FROM @extschema@.pathman_config WHERE relname::regclass = p_relation;
801+
SELECT attname, range_interval
802+
FROM @extschema@.pathman_config
803+
WHERE relname::regclass = p_relation
804+
INTO v_attname, v_interval;
805+
806+
IF v_attname IS NULL THEN
807+
RAISE EXCEPTION 'Table % is not partitioned', quote_ident(p_relation::TEXT);
808+
END IF;
809+
775810
v_atttype := @extschema@.get_attribute_type_name(p_relation, v_attname);
776811

777812
EXECUTE format('SELECT @extschema@.prepend_partition_internal($1, $2, $3, ARRAY[]::%s[])', v_atttype)
@@ -946,7 +981,14 @@ BEGIN
946981
, p_relation);
947982

948983
/* Set check constraint */
949-
v_attname := attname FROM @extschema@.pathman_config WHERE relname::regclass = p_relation;
984+
v_attname := attname
985+
FROM @extschema@.pathman_config
986+
WHERE relname::regclass = p_relation;
987+
988+
IF v_attname IS NULL THEN
989+
RAISE EXCEPTION 'Table % is not partitioned', quote_ident(p_relation::TEXT);
990+
END IF;
991+
950992
v_cond := @extschema@.get_range_condition(v_attname, p_start_value, p_end_value);
951993

952994
/* Plain partition name and schema */
@@ -1072,7 +1114,7 @@ $$ LANGUAGE plpgsql;
10721114
* Creates an update trigger
10731115
*/
10741116
CREATE OR REPLACE FUNCTION @extschema@.create_range_update_trigger(
1075-
IN relation TEXT)
1117+
IN relation REGCLASS)
10761118
RETURNS TEXT AS
10771119
$$
10781120
DECLARE
@@ -1107,8 +1149,7 @@ DECLARE
11071149
num INTEGER := 0;
11081150
attr TEXT;
11091151
BEGIN
1110-
relation := @extschema@.validate_relname(relation);
1111-
relid := relation::regclass::oid;
1152+
relid := relation::oid;
11121153
SELECT string_agg(attname, ', '),
11131154
string_agg('OLD.' || attname, ', '),
11141155
string_agg('NEW.' || attname, ', '),
@@ -1123,7 +1164,14 @@ BEGIN
11231164
att_val_fmt,
11241165
att_fmt;
11251166

1126-
attr := attname FROM @extschema@.pathman_config WHERE relname = relation;
1167+
attr := attname
1168+
FROM @extschema@.pathman_config
1169+
WHERE relname::regclass = relation;
1170+
1171+
IF attr IS NULL THEN
1172+
RAISE EXCEPTION 'Table % is not partitioned', quote_ident(relation::TEXT);
1173+
END IF;
1174+
11271175
EXECUTE format(func, relation, attr, 0, att_val_fmt,
11281176
old_fields, att_fmt, new_fields);
11291177
FOR rec in (SELECT * FROM pg_inherits WHERE inhparent = relation::regclass::oid)
@@ -1215,7 +1263,6 @@ CREATE OR REPLACE FUNCTION @extschema@.append_partitions_on_demand_internal(
12151263
RETURNS OID AS
12161264
$$
12171265
DECLARE
1218-
v_relation TEXT;
12191266
v_cnt INTEGER := 0;
12201267
i INTEGER := 0;
12211268
v_part TEXT;
@@ -1227,11 +1274,16 @@ DECLARE
12271274
v_next_value p_new_value%TYPE;
12281275
v_is_date BOOLEAN;
12291276
BEGIN
1230-
v_relation := @extschema@.validate_relname(p_relid::regclass::text);
1231-
12321277
/* get attribute name and interval */
1233-
SELECT attname, range_interval INTO v_attname, v_interval
1234-
FROM @extschema@.pathman_config WHERE relname = v_relation;
1278+
SELECT attname, range_interval
1279+
FROM @extschema@.pathman_config
1280+
WHERE relname::regclass = p_relid::regclass
1281+
INTO v_attname, v_interval;
1282+
1283+
IF v_attname IS NULL THEN
1284+
RAISE EXCEPTION 'Table % is not partitioned',
1285+
quote_ident(p_relid::regclass::text);
1286+
END IF;
12351287

12361288
v_min := @extschema@.get_min_range_value(p_relid::regclass::oid, p_new_value);
12371289
v_max := @extschema@.get_max_range_value(p_relid::regclass::oid, p_new_value);

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