Skip to content

Commit 7345c79

Browse files
committed
pathman: prepending issue
1 parent be7afb2 commit 7345c79

File tree

3 files changed

+48
-33
lines changed

3 files changed

+48
-33
lines changed

contrib/pg_pathman/expected/pg_pathman.out

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,13 @@ NOTICE: Done!
632632
test.num_range_rel_6
633633
(1 row)
634634

635+
EXPLAIN (COSTS OFF) SELECT * FROM test.num_range_rel WHERE id >= 4000;
636+
QUERY PLAN
637+
-----------------------------------
638+
Append
639+
-> Seq Scan on num_range_rel_6
640+
(2 rows)
641+
635642
SELECT pathman.prepend_range_partition('test.num_range_rel');
636643
NOTICE: Prepending new partition...
637644
NOTICE: Done!
@@ -640,6 +647,13 @@ NOTICE: Done!
640647
test.num_range_rel_7
641648
(1 row)
642649

650+
EXPLAIN (COSTS OFF) SELECT * FROM test.num_range_rel WHERE id < 0;
651+
QUERY PLAN
652+
-----------------------------------
653+
Append
654+
-> Seq Scan on num_range_rel_7
655+
(2 rows)
656+
643657
SELECT pathman.drop_range_partition('test.num_range_rel_7');
644658
drop_range_partition
645659
----------------------

contrib/pg_pathman/range.sql

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ $$ LANGUAGE plpgsql;
690690
* Append new partition
691691
*/
692692
CREATE OR REPLACE FUNCTION @extschema@.append_range_partition(
693-
p_relation TEXT)
693+
p_relation REGCLASS)
694694
RETURNS TEXT AS
695695
$$
696696
DECLARE
@@ -699,10 +699,8 @@ DECLARE
699699
v_part_name TEXT;
700700
v_interval TEXT;
701701
BEGIN
702-
p_relation := @extschema@.validate_relname(p_relation);
703-
704702
SELECT attname, range_interval INTO v_attname, v_interval
705-
FROM @extschema@.pathman_config WHERE relname = p_relation;
703+
FROM @extschema@.pathman_config WHERE relname::regclass = p_relation;
706704

707705
v_atttype := @extschema@.get_attribute_type_name(p_relation, v_attname);
708706

@@ -714,7 +712,7 @@ BEGIN
714712
USING p_relation, v_atttype, v_interval;
715713

716714
/* Invalidate cache */
717-
PERFORM @extschema@.on_update_partitions(p_relation::regclass::oid);
715+
PERFORM @extschema@.on_update_partitions(p_relation::oid);
718716

719717
/* Release lock */
720718
PERFORM @extschema@.release_partitions_lock();
@@ -731,7 +729,7 @@ LANGUAGE plpgsql;
731729

732730

733731
CREATE OR REPLACE FUNCTION @extschema@.append_partition_internal(
734-
p_relation TEXT
732+
p_relation REGCLASS
735733
, p_atttype TEXT
736734
, p_interval TEXT
737735
, p_range ANYARRAY DEFAULT NULL)
@@ -740,7 +738,7 @@ $$
740738
DECLARE
741739
v_part_name TEXT;
742740
BEGIN
743-
p_range := @extschema@.get_range_by_idx(p_relation::regclass::oid, -1, 0);
741+
p_range := @extschema@.get_range_by_idx(p_relation::oid, -1, 0);
744742
RAISE NOTICE 'Appending new partition...';
745743
IF @extschema@.is_date(p_atttype::regtype) THEN
746744
v_part_name := @extschema@.create_single_range_partition(p_relation
@@ -761,7 +759,7 @@ LANGUAGE plpgsql;
761759
/*
762760
* Prepend new partition
763761
*/
764-
CREATE OR REPLACE FUNCTION @extschema@.prepend_range_partition(p_relation TEXT)
762+
CREATE OR REPLACE FUNCTION @extschema@.prepend_range_partition(p_relation REGCLASS)
765763
RETURNS TEXT AS
766764
$$
767765
DECLARE
@@ -770,10 +768,8 @@ DECLARE
770768
v_part_name TEXT;
771769
v_interval TEXT;
772770
BEGIN
773-
p_relation := @extschema@.validate_relname(p_relation);
774-
775771
SELECT attname, range_interval INTO v_attname, v_interval
776-
FROM @extschema@.pathman_config WHERE relname = p_relation;
772+
FROM @extschema@.pathman_config WHERE relname::regclass = p_relation;
777773
v_atttype := @extschema@.get_attribute_type_name(p_relation, v_attname);
778774

779775
/* Prevent concurrent partition creation */
@@ -784,7 +780,7 @@ BEGIN
784780
USING p_relation, v_atttype, v_interval;
785781

786782
/* Invalidate cache */
787-
PERFORM @extschema@.on_update_partitions(p_relation::regclass::oid);
783+
PERFORM @extschema@.on_update_partitions(p_relation::oid);
788784

789785
/* Release lock */
790786
PERFORM @extschema@.release_partitions_lock();
@@ -801,7 +797,7 @@ LANGUAGE plpgsql;
801797

802798

803799
CREATE OR REPLACE FUNCTION @extschema@.prepend_partition_internal(
804-
p_relation TEXT
800+
p_relation REGCLASS
805801
, p_atttype TEXT
806802
, p_interval TEXT
807803
, p_range ANYARRAY DEFAULT NULL)
@@ -810,15 +806,15 @@ $$
810806
DECLARE
811807
v_part_name TEXT;
812808
BEGIN
813-
p_range := @extschema@.get_range_by_idx(p_relation::regclass::oid, 0, 0);
809+
p_range := @extschema@.get_range_by_idx(p_relation::oid, 0, 0);
814810
RAISE NOTICE 'Prepending new partition...';
815811

816812
IF @extschema@.is_date(p_atttype::regtype) THEN
817813
v_part_name := @extschema@.create_single_range_partition(p_relation
818814
, p_range[1] - p_interval::interval
819815
, p_range[1]);
820816
ELSE
821-
EXECUTE format('SELECT @extschema@.create_single_range_partition($1, $2, $2 - $3::%s)', p_atttype)
817+
EXECUTE format('SELECT @extschema@.create_single_range_partition($1, $2 - $3::%s, $2)', p_atttype)
822818
USING p_relation, p_range[1], p_interval
823819
INTO v_part_name;
824820
END IF;
@@ -833,7 +829,7 @@ LANGUAGE plpgsql;
833829
* Add new partition
834830
*/
835831
CREATE OR REPLACE FUNCTION @extschema@.add_range_partition(
836-
p_relation TEXT
832+
p_relation REGCLASS
837833
, p_start_value ANYELEMENT
838834
, p_end_value ANYELEMENT)
839835
RETURNS TEXT AS
@@ -844,10 +840,8 @@ BEGIN
844840
/* Prevent concurrent partition creation */
845841
PERFORM @extschema@.acquire_partitions_lock();
846842

847-
p_relation := @extschema@.validate_relname(p_relation);
848-
849843
/* check range overlap */
850-
IF @extschema@.check_overlap(p_relation::regclass::oid, p_start_value, p_end_value) != FALSE THEN
844+
IF @extschema@.check_overlap(p_relation::oid, p_start_value, p_end_value) != FALSE THEN
851845
RAISE EXCEPTION 'Specified range overlaps with existing partitions';
852846
END IF;
853847

@@ -857,7 +851,7 @@ BEGIN
857851

858852
/* Create new partition */
859853
v_part_name := @extschema@.create_single_range_partition(p_relation, p_start_value, p_end_value);
860-
PERFORM @extschema@.on_update_partitions(p_relation::regclass::oid);
854+
PERFORM @extschema@.on_update_partitions(p_relation::oid);
861855

862856
/* Release lock */
863857
PERFORM @extschema@.release_partitions_lock();
@@ -917,26 +911,27 @@ LANGUAGE plpgsql;
917911
* Attach range partition
918912
*/
919913
CREATE OR REPLACE FUNCTION @extschema@.attach_range_partition(
920-
p_relation TEXT
921-
, p_partition TEXT
922-
, p_start_value ANYELEMENT
923-
, p_end_value ANYELEMENT)
914+
p_relation REGCLASS
915+
, p_partition REGCLASS
916+
, p_start_value ANYELEMENT
917+
, p_end_value ANYELEMENT)
924918
RETURNS TEXT AS
925919
$$
926920
DECLARE
927-
v_attname TEXT;
928-
v_cond TEXT;
921+
v_attname TEXT;
922+
v_cond TEXT;
923+
v_plain_partname TEXT;
924+
v_plain_schema TEXT;
929925
BEGIN
930926
/* Prevent concurrent partition management */
931927
PERFORM @extschema@.acquire_partitions_lock();
932928

933-
p_relation := @extschema@.validate_relname(p_relation);
934929

935-
IF @extschema@.check_overlap(p_relation::regclass::oid, p_start_value, p_end_value) != FALSE THEN
930+
IF @extschema@.check_overlap(p_relation::oid, p_start_value, p_end_value) != FALSE THEN
936931
RAISE EXCEPTION 'Specified range overlaps with existing partitions';
937932
END IF;
938933

939-
IF NOT @extschema@.validate_relations_equality(p_relation::regclass, p_partition::regclass) THEN
934+
IF NOT @extschema@.validate_relations_equality(p_relation, p_partition) THEN
940935
RAISE EXCEPTION 'Partition must have the exact same structure as parent';
941936
END IF;
942937

@@ -946,15 +941,19 @@ BEGIN
946941
, p_relation);
947942

948943
/* Set check constraint */
949-
v_attname := attname FROM @extschema@.pathman_config WHERE relname = p_relation;
944+
v_attname := attname FROM @extschema@.pathman_config WHERE relname::regclass = p_relation;
950945
v_cond := @extschema@.get_range_condition(v_attname, p_start_value, p_end_value);
951-
EXECUTE format('ALTER TABLE %s ADD CONSTRAINT %s_check CHECK (%s)'
946+
947+
/* Plain partition name and schema */
948+
SELECT * INTO v_plain_schema, v_plain_partname FROM @extschema@.get_plain_schema_and_relname(p_partition);
949+
950+
EXECUTE format('ALTER TABLE %s ADD CONSTRAINT %s CHECK (%s)'
952951
, p_partition
953-
, @extschema@.get_schema_qualified_name(p_partition::regclass)
952+
, v_plain_schema || '_' || quote_ident(v_plain_partname || '_check')
954953
, v_cond);
955954

956955
/* Invalidate cache */
957-
PERFORM @extschema@.on_update_partitions(p_relation::regclass::oid);
956+
PERFORM @extschema@.on_update_partitions(p_relation::oid);
958957

959958
/* Release lock */
960959
PERFORM @extschema@.release_partitions_lock();

contrib/pg_pathman/sql/pg_pathman.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,9 @@ SELECT pathman.merge_range_partitions('test.range_rel_1', 'test.range_rel_' || c
155155

156156
/* Append and prepend partitions */
157157
SELECT pathman.append_range_partition('test.num_range_rel');
158+
EXPLAIN (COSTS OFF) SELECT * FROM test.num_range_rel WHERE id >= 4000;
158159
SELECT pathman.prepend_range_partition('test.num_range_rel');
160+
EXPLAIN (COSTS OFF) SELECT * FROM test.num_range_rel WHERE id < 0;
159161
SELECT pathman.drop_range_partition('test.num_range_rel_7');
160162

161163
SELECT pathman.append_range_partition('test.range_rel');

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