Skip to content

Commit 3007f79

Browse files
committed
auto partitions creation parameter added
1 parent bea88a9 commit 3007f79

File tree

8 files changed

+113
-25
lines changed

8 files changed

+113
-25
lines changed

expected/pg_pathman.out

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,46 @@ CREATE TABLE test.range_rel_test2 (
11091109
dt TIMESTAMP);
11101110
SELECT pathman.attach_range_partition('test.range_rel', 'test.range_rel_test2', '2013-01-01'::DATE, '2014-01-01'::DATE);
11111111
ERROR: Partition must have the exact same structure as parent
1112+
/*
1113+
* Zero partitions count and adding partitions with specified name
1114+
*/
1115+
CREATE TABLE test.zero(
1116+
id SERIAL PRIMARY KEY,
1117+
value INT NOT NULL);
1118+
INSERT INTO test.zero SELECT g, g FROM generate_series(1, 100) as g;
1119+
SELECT pathman.create_range_partitions('test.zero', 'value', 50, 10, 0);
1120+
NOTICE: sequence "zero_seq" does not exist, skipping
1121+
create_range_partitions
1122+
-------------------------
1123+
0
1124+
(1 row)
1125+
1126+
SELECT pathman.append_range_partition('test.zero', 'test.zero_0');
1127+
ERROR: Cannot append to empty partitions set
1128+
SELECT pathman.prepend_range_partition('test.zero', 'test.zero_1');
1129+
ERROR: Cannot prepend to empty partitions set
1130+
SELECT pathman.add_range_partition('test.zero', 50, 70, 'test.zero_50');
1131+
add_range_partition
1132+
---------------------
1133+
test.zero_50
1134+
(1 row)
1135+
1136+
SELECT pathman.append_range_partition('test.zero', 'test.zero_appended');
1137+
ERROR: Partition #-1 does not exist (total amount is 1)
1138+
SELECT pathman.prepend_range_partition('test.zero', 'test.zero_prepended');
1139+
prepend_range_partition
1140+
-------------------------
1141+
test.zero_prepended
1142+
(1 row)
1143+
1144+
SELECT pathman.split_range_partition('test.zero_50', 60, 'test.zero_60');
1145+
split_range_partition
1146+
-----------------------
1147+
{50,70}
1148+
(1 row)
1149+
1150+
DROP TABLE test.zero CASCADE;
1151+
NOTICE: drop cascades to 3 other objects
11121152
/*
11131153
* Check that altering table columns doesn't break trigger
11141154
*/

init.sql

Lines changed: 56 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ CREATE TABLE IF NOT EXISTS @extschema@.pathman_config (
2929

3030
CREATE TABLE IF NOT EXISTS @extschema@.pathman_config_params (
3131
partrel REGCLASS NOT NULL,
32-
enable_parent BOOLEAN NOT NULL DEFAULT TRUE
32+
enable_parent BOOLEAN NOT NULL DEFAULT TRUE,
33+
auto BOOLEAN NOT NULL DEFAULT TRUE
3334
);
3435
CREATE UNIQUE INDEX i_pathman_config_params
3536
ON @extschema@.pathman_config_params(partrel);
@@ -81,32 +82,73 @@ END
8182
$$
8283
LANGUAGE plpgsql;
8384

84-
/* Include parent relation into query plan's for specified relation */
85+
/*
86+
* Set additional param
87+
*/
88+
CREATE OR REPLACE FUNCTION @extschema@.pathman_set_param(
89+
relation REGCLASS,
90+
param TEXT,
91+
value BOOLEAN)
92+
RETURNS VOID AS
93+
$$
94+
BEGIN
95+
EXECUTE format(
96+
'INSERT INTO @extschema@.pathman_config_params (partrel, %1$s)
97+
VALUES ($1, $2)
98+
ON CONFLICT (partrel) DO
99+
UPDATE SET %1$s = $2',
100+
param)
101+
USING
102+
relation,
103+
value;
104+
END
105+
$$
106+
LANGUAGE plpgsql;
107+
108+
/*
109+
* Include parent relation into query plan's for specified relation
110+
*/
85111
CREATE OR REPLACE FUNCTION @extschema@.enable_parent(relation REGCLASS)
86112
RETURNS VOID AS
87113
$$
88114
BEGIN
89-
INSERT INTO @extschema@.pathman_config_params values (relation, True)
90-
ON CONFLICT (partrel) DO
91-
UPDATE SET enable_parent = True;
115+
PERFORM @extschema@.pathman_set_param(relation, 'enable_parent', True);
116+
END
117+
$$
118+
LANGUAGE plpgsql;
92119

93-
-- PERFORM @extschema@.invalidate_relcache(relation::oid);
94-
-- PERFORM @extschema@.on_enable_parent(relation::oid);
120+
/*
121+
* Do not include parent relation into query plan's for specified relation
122+
*/
123+
CREATE OR REPLACE FUNCTION @extschema@.disable_parent(relation REGCLASS)
124+
RETURNS VOID AS
125+
$$
126+
BEGIN
127+
PERFORM @extschema@.pathman_set_param(relation, 'enable_parent', False);
95128
END
96129
$$
97130
LANGUAGE plpgsql;
98131

99-
/* Do not include parent relation into query plan's for specified relation */
100-
CREATE OR REPLACE FUNCTION @extschema@.disable_parent(relation REGCLASS)
132+
/*
133+
* Enable auto partition creation
134+
*/
135+
CREATE OR REPLACE FUNCTION @extschema@.enable_auto(relation REGCLASS)
101136
RETURNS VOID AS
102137
$$
103138
BEGIN
104-
INSERT INTO @extschema@.pathman_config_params values (relation, False)
105-
ON CONFLICT (partrel) DO
106-
UPDATE SET enable_parent = False;
139+
PERFORM @extschema@.pathman_set_param(relation, 'auto', True);
140+
END
141+
$$
142+
LANGUAGE plpgsql;
107143

108-
-- PERFORM @extschema@.invalidate_relcache(relation::oid);
109-
-- PERFORM @extschema@.on_disable_parent(relation::oid);
144+
/*
145+
* Disable auto partition creation
146+
*/
147+
CREATE OR REPLACE FUNCTION @extschema@.disable_auto(relation REGCLASS)
148+
RETURNS VOID AS
149+
$$
150+
BEGIN
151+
PERFORM @extschema@.pathman_set_param(relation, 'auto', False);
110152
END
111153
$$
112154
LANGUAGE plpgsql;

sql/pg_pathman.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,7 @@ SELECT pathman.add_range_partition('test.zero', 50, 70, 'test.zero_50');
485485
SELECT pathman.append_range_partition('test.zero', 'test.zero_appended');
486486
SELECT pathman.prepend_range_partition('test.zero', 'test.zero_prepended');
487487
SELECT pathman.split_range_partition('test.zero_50', 60, 'test.zero_60');
488+
DROP TABLE test.zero CASCADE;
488489

489490
/*
490491
* Check that altering table columns doesn't break trigger

src/init.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ pathman_config_contains_relation(Oid relid, Datum *values, bool *isnull,
570570
* Return 'enable_parent' parameter of relation
571571
*/
572572
bool
573-
read_enable_parent_parameter(Oid relid)
573+
read_pathman_params(Oid relid, Datum *values, bool *isnull)
574574
{
575575
Relation rel;
576576
HeapScanDesc scan;
@@ -590,12 +590,9 @@ read_enable_parent_parameter(Oid relid)
590590

591591
if ((htup = heap_getnext(scan, ForwardScanDirection)) != NULL)
592592
{
593-
Datum values[Natts_pathman_config_params];
594-
bool isnull[Natts_pathman_config_params];
595-
596593
/* Extract data if necessary */
597594
heap_deform_tuple(htup, RelationGetDescr(rel), values, isnull);
598-
result = values[Anum_pathman_config_params_enable_parent - 1];
595+
result = true;
599596
}
600597

601598
/* Clean resources */

src/init.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ bool pathman_config_contains_relation(Oid relid,
4747
bool *isnull,
4848
TransactionId *xmin);
4949

50-
bool read_enable_parent_parameter(Oid relid);
50+
bool read_pathman_params(Oid relid,
51+
Datum *values,
52+
bool *isnull);
5153

5254
#endif

src/pathman.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@
5757
* Definitions for the "pathman_config_params" table
5858
*/
5959
#define PATHMAN_CONFIG_PARAMS "pathman_config_params"
60-
// #define PATHMAN_CONFIG_PARAMS_INDEX "i_pathman_config_params"
61-
#define Natts_pathman_config_params 2
60+
#define Natts_pathman_config_params 3
6261
#define Anum_pathman_config_params_partrel 1 /* primary key */
6362
#define Anum_pathman_config_params_enable_parent 2 /* include parent into plan */
63+
#define Anum_pathman_config_params_auto 3 /* auto partitions creation */
6464

6565
/*
6666
* Cache current PATHMAN_CONFIG relid (set during load_config()).

src/relation_info.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ refresh_pathman_relation_info(Oid relid,
7474
i;
7575
bool found;
7676
PartRelationInfo *prel;
77+
Datum param_values[Natts_pathman_config_params];
78+
bool param_isnull[Natts_pathman_config_params];
7779

7880
prel = (PartRelationInfo *) hash_search(partitioned_rels,
7981
(const void *) &relid,
@@ -158,10 +160,13 @@ refresh_pathman_relation_info(Oid relid,
158160
pfree(prel_children);
159161

160162
/*
161-
* Read additional parameter ('enable_parent' is the only one at
162-
* the moment)
163+
* Read additional parameters ('enable_parent' and 'auto' at the moment)
163164
*/
164-
prel->enable_parent = read_enable_parent_parameter(relid);
165+
if (read_pathman_params(relid, param_values, param_isnull))
166+
{
167+
prel->enable_parent = param_values[Anum_pathman_config_params_enable_parent - 1];
168+
prel->auto_partition = param_values[Anum_pathman_config_params_auto - 1];
169+
}
165170

166171
/* We've successfully built a cache entry */
167172
prel->valid = true;

src/relation_info.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ typedef struct
4848
Oid key; /* partitioned table's Oid */
4949
bool valid; /* is this entry valid? */
5050
bool enable_parent; /* include parent to the plan */
51+
bool auto_partition; /* auto partition creation */
5152

5253
uint32 children_count;
5354
Oid *children; /* Oids of child partitions */

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