Skip to content

Commit e38be2b

Browse files
committed
Fix migration script
1 parent 574f52d commit e38be2b

File tree

2 files changed

+61
-6
lines changed

2 files changed

+61
-6
lines changed

pg_pathman--1.3--1.4.sql

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* ------------------------------------------------------------------------
99
*/
1010

11-
DROP FUNCTION @extschema@.validate_interval_value(REGCLASS, TEXT, INTEGER, TEXT);
11+
DROP FUNCTION @extschema@.validate_interval_value(REGCLASS, TEXT, INTEGER, TEXT) CASCADE;
1212
CREATE OR REPLACE FUNCTION @extschema@.validate_interval_value(
1313
atttype OID,
1414
parttype INTEGER,
@@ -18,12 +18,11 @@ LANGUAGE C;
1818

1919
DROP FUNCTION @extschema@.is_attribute_nullable(REGCLASS, TEXT);
2020

21-
ALTER TABLE @extschema@.pathman_config ADD COLUMN expression_p TEXT NOT NULL;
22-
ALTER TABLE @extschema@.pathman_config ADD COLUMN atttype OID NOT NULL;
21+
ALTER TABLE @extschema@.pathman_config ADD COLUMN expression_p TEXT DEFAULT '--not set--';
22+
ALTER TABLE @extschema@.pathman_config ADD COLUMN atttype OID DEFAULT 1;
2323
ALTER TABLE @extschema@.pathman_config ADD COLUMN upd_expr BOOL DEFAULT FALSE;
2424

2525
/* update constraint */
26-
ALTER TABLE @extschema@.pathman_config DROP CONSTRAINT pathman_config_check;
2726
ALTER TABLE @extschema@.pathman_config
2827
ADD CONSTRAINT pathman_config_interval_check CHECK (@extschema@.validate_interval_value(atttype,
2928
parttype,
@@ -32,6 +31,7 @@ ALTER TABLE @extschema@.pathman_config
3231
/* mark 'expression_p' and 'atttype' to update on next start */
3332
UPDATE @extschema@.pathman_config SET upd_expr = TRUE;
3433

34+
DROP FUNCTION @extschema@.common_relation_checks(REGCLASS, TEXT);
3535
CREATE OR REPLACE FUNCTION @extschema@.common_relation_checks(
3636
relation REGCLASS,
3737
expression TEXT)
@@ -95,6 +95,9 @@ CREATE OR REPLACE FUNCTION @extschema@.add_to_pathman_config(
9595
RETURNS BOOLEAN AS 'pg_pathman', 'add_to_pathman_config'
9696
LANGUAGE C;
9797

98+
DROP FUNCTION @extschema@.create_hash_partitions(REGCLASS, TEXT, INT4, BOOLEAN,
99+
TEXT[], TEXT[]);
100+
98101
CREATE OR REPLACE FUNCTION @extschema@.create_hash_partitions(
99102
parent_relid REGCLASS,
100103
expression TEXT,
@@ -146,6 +149,7 @@ SET client_min_messages = WARNING;
146149

147150
DROP FUNCTION @extschema@.build_hash_condition(REGTYPE, TEXT, INT4, INT4);
148151

152+
DROP FUNCTION @extschema@.check_boundaries(REGCLASS, TEXT, ANYELEMENT, ANYELEMENT);
149153
CREATE OR REPLACE FUNCTION @extschema@.check_boundaries(
150154
parent_relid REGCLASS,
151155
expression TEXT,
@@ -183,6 +187,7 @@ END
183187
$$ LANGUAGE plpgsql;
184188

185189

190+
DROP FUNCTION @extschema@.prepare_for_partitioning(REGCLASS, TEXT, BOOLEAN);
186191
CREATE OR REPLACE FUNCTION @extschema@.prepare_for_partitioning(
187192
parent_relid REGCLASS,
188193
expression TEXT,
@@ -208,6 +213,9 @@ $$ LANGUAGE plpgsql;
208213
/*
209214
* Creates RANGE partitions for specified relation based on datetime attribute
210215
*/
216+
DROP FUNCTION @extschema@.create_range_partitions(REGCLASS, TEXT, ANYELEMENT,
217+
INTERVAL, INTEGER, BOOLEAN);
218+
211219
CREATE OR REPLACE FUNCTION @extschema@.create_range_partitions(
212220
parent_relid REGCLASS,
213221
expression TEXT,
@@ -311,6 +319,9 @@ $$ LANGUAGE plpgsql;
311319
/*
312320
* Creates RANGE partitions for specified relation based on numerical expression
313321
*/
322+
DROP FUNCTION @extschema@.create_range_partitions(REGCLASS, TEXT, ANYELEMENT,
323+
ANYELEMENT, INTEGER, BOOLEAN);
324+
314325
CREATE OR REPLACE FUNCTION @extschema@.create_range_partitions(
315326
parent_relid REGCLASS,
316327
expression TEXT,
@@ -410,6 +421,9 @@ $$ LANGUAGE plpgsql;
410421
/*
411422
* Creates RANGE partitions for specified relation based on bounds array
412423
*/
424+
DROP FUNCTION @extschema@.create_range_partitions(REGCLASS, TEXT, ANYARRAY,
425+
TEXT[], TEXT[], BOOLEAN);
426+
413427
CREATE OR REPLACE FUNCTION @extschema@.create_range_partitions(
414428
parent_relid REGCLASS,
415429
expression TEXT,
@@ -471,6 +485,9 @@ LANGUAGE plpgsql;
471485
/*
472486
* Creates RANGE partitions for specified range
473487
*/
488+
DROP FUNCTION @extschema@.create_partitions_from_range(REGCLASS, TEXT, ANYELEMENT,
489+
ANYELEMENT, ANYELEMENT, BOOLEAN);
490+
474491
CREATE OR REPLACE FUNCTION @extschema@.create_partitions_from_range(
475492
parent_relid REGCLASS,
476493
expression TEXT,
@@ -531,6 +548,9 @@ $$ LANGUAGE plpgsql;
531548
/*
532549
* Creates RANGE partitions for specified range based on datetime expression
533550
*/
551+
DROP FUNCTION @extschema@.create_partitions_from_range(REGCLASS, TEXT,
552+
ANYELEMENT, ANYELEMENT, INTERVAL, BOOLEAN);
553+
534554
CREATE OR REPLACE FUNCTION @extschema@.create_partitions_from_range(
535555
parent_relid REGCLASS,
536556
expression TEXT,
@@ -592,6 +612,9 @@ BEGIN
592612
END
593613
$$ LANGUAGE plpgsql;
594614

615+
DROP FUNCTION @extschema@.build_range_condition(REGCLASS, TEXT,
616+
ANYELEMENT, ANYELEMENT);
617+
595618
CREATE OR REPLACE FUNCTION @extschema@.build_range_condition(
596619
partition_relid REGCLASS,
597620
expression TEXT,

src/init.c

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,16 @@ read_pathman_config(void)
733733
HeapScanDesc scan;
734734
Snapshot snapshot;
735735
HeapTuple htup;
736+
Oid *relids = NULL;
737+
Size relids_index = 0,
738+
relids_count = 100,
739+
j;
740+
741+
/*
742+
* Initialize relids array, we keep here relations that require
743+
* update their expression.
744+
*/
745+
relids = (Oid *) palloc(sizeof(Oid) * relids_count);
736746

737747
/* Open PATHMAN_CONFIG with latest snapshot available */
738748
rel = heap_open(get_pathman_config_relid(false), AccessShareLock);
@@ -752,7 +762,8 @@ read_pathman_config(void)
752762
while((htup = heap_getnext(scan, ForwardScanDirection)) != NULL)
753763
{
754764
Datum values[Natts_pathman_config];
755-
bool isnull[Natts_pathman_config];
765+
bool upd_expr,
766+
isnull[Natts_pathman_config];
756767
Oid relid; /* partitioned table */
757768

758769
/* Extract Datums from tuple 'htup' */
@@ -763,6 +774,20 @@ read_pathman_config(void)
763774
Assert(!isnull[Anum_pathman_config_parttype - 1]);
764775
Assert(!isnull[Anum_pathman_config_expression - 1]);
765776
Assert(!isnull[Anum_pathman_config_expression_p - 1]);
777+
Assert(!isnull[Anum_pathman_config_upd_expression - 1]);
778+
779+
upd_expr = DatumGetBool(values[Anum_pathman_config_upd_expression - 1]);
780+
if (upd_expr)
781+
{
782+
if (relids_index >= relids_count)
783+
{
784+
relids_count += 100;
785+
relids = (Oid *) repalloc(relids, sizeof(Oid) * relids_count);
786+
}
787+
788+
relids[relids_index] = relid;
789+
relids_index += 1;
790+
}
766791

767792
/* Extract values from Datums */
768793
relid = DatumGetObjectId(values[Anum_pathman_config_partrel - 1]);
@@ -778,7 +803,8 @@ read_pathman_config(void)
778803
}
779804

780805
/* get_pathman_relation_info() will refresh this entry */
781-
refresh_pathman_relation_info(relid,
806+
if (!upd_expr)
807+
refresh_pathman_relation_info(relid,
782808
values,
783809
true); /* allow lazy prel loading */
784810
}
@@ -787,6 +813,12 @@ read_pathman_config(void)
787813
heap_endscan(scan);
788814
UnregisterSnapshot(snapshot);
789815
heap_close(rel, AccessShareLock);
816+
817+
/* Update expressions */
818+
for (j = 0; j < relids_index; j++)
819+
get_pathman_relation_info(relids[j]);
820+
821+
pfree(relids);
790822
}
791823

792824

0 commit comments

Comments
 (0)