You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: contrib/pg_pathman/README.md
+81-32Lines changed: 81 additions & 32 deletions
Original file line number
Diff line number
Diff line change
@@ -5,12 +5,12 @@
5
5
6
6
The `pg_pathman` module provides optimized partitioning mechanism and functions to manage partitions.
7
7
8
-
The extension is compatible with PostgreSQL 9.5 (9.6 support is coming soon).
8
+
The extension is compatible with PostgreSQL 9.5+.
9
9
10
10
## Overview
11
11
**Partitioning** means splitting one large table into smaller pieces. Each row in such table is moved to a single partition according to the partitioning key. PostgreSQL supports partitioning via table inheritance: each partition must be created as a child table with CHECK CONSTRAINT. For example:
12
12
13
-
```
13
+
```plpgsql
14
14
CREATETABLEtest (id SERIALPRIMARY KEY, title TEXT);
15
15
CREATETABLEtest_1 (CHECK ( id >=100AND id <200 )) INHERITS (test);
16
16
CREATETABLEtest_2 (CHECK ( id >=200AND id <300 )) INHERITS (test);
@@ -25,7 +25,7 @@ VARIABLE OP CONST
25
25
```
26
26
where `VARIABLE` is a partitioning key, `OP` is a comparison operator (supported operators are =, <, <=, >, >=), `CONST` is a scalar value. For example:
27
27
28
-
```
28
+
```plpgsql
29
29
WHERE id =150
30
30
```
31
31
@@ -36,9 +36,24 @@ Based on the partitioning type and condition's operator, `pg_pathman` searches f
36
36
37
37
More interesting features are yet to come. Stay tuned!
38
38
39
+
## Feature highlights
40
+
41
+
* HASH and RANGE partitioning schemes;
42
+
* Both automatic and manual partition management;
43
+
* Support for integer, floating point, date and other types, including domains;
44
+
* Effective query planning for partitioned tables (JOINs, subselects etc);
45
+
*`RuntimeAppend` & `RuntimeMergeAppend` custom plan nodes to pick partitions at runtime;
46
+
*`PartitionFilter`: an efficient drop-in replacement for INSERT triggers;
47
+
* Automatic partition creation for new INSERTed data (only for RANGE partitioning);
48
+
* Improved `COPY FROM\TO` statement that is able to insert rows directly into partitions;
49
+
* UPDATE triggers generation out of the box (will be replaced with custom nodes too);
50
+
* User-defined callbacks for partition creation event handling;
51
+
* Non-blocking concurrent table partitioning;
52
+
* FDW support (foreign partitions);
53
+
* Various GUC toggles and configurable settings.
54
+
39
55
## Roadmap
40
56
41
-
* Provide a way to create user-defined partition creation\destruction callbacks (issue [#22](https://github.com/postgrespro/pg_pathman/issues/22))
42
57
* Implement LIST partitioning scheme;
43
58
* Optimize hash join (both tables are partitioned by join key).
44
59
@@ -76,15 +91,15 @@ Performs HASH partitioning for `relation` by integer key `attribute`. The `parti
76
91
create_range_partitions(relation REGCLASS,
77
92
attribute TEXT,
78
93
start_value ANYELEMENT,
79
-
interval ANYELEMENT,
80
-
count INTEGER DEFAULT NULL
94
+
p_interval ANYELEMENT,
95
+
p_countINTEGER DEFAULT NULL
81
96
partition_data BOOLEAN DEFAULT TRUE)
82
97
83
98
create_range_partitions(relation REGCLASS,
84
99
attribute TEXT,
85
100
start_value ANYELEMENT,
86
-
interval INTERVAL,
87
-
count INTEGER DEFAULT NULL,
101
+
p_interval INTERVAL,
102
+
p_countINTEGER DEFAULT NULL,
88
103
partition_data BOOLEAN DEFAULT TRUE)
89
104
```
90
105
Performs RANGE partitioning for `relation` by partitioning key `attribute`. `start_value` argument specifies initial value, `interval` sets the range of values in a single partition, `count` is the number of premade partitions (if not set then pathman tries to determine it based on attribute values). Partition creation callback is invoked for each partition if set beforehand.
Performs RANGE-partitioning from specified range for `relation` by partitioning key `attribute`. Partition creation callback is invoked for each partition if set beforehand.
108
123
109
124
### Data migration
110
125
111
126
```plpgsql
112
-
partition_table_concurrently(relation REGCLASS)
127
+
partition_table_concurrently(relation REGCLASS,
128
+
batch_size INTEGER DEFAULT 1000,
129
+
sleep_time FLOAT8 DEFAULT 1.0)
113
130
```
114
-
Starts a background worker to move data from parent table to partitions. The worker utilizes short transactions to copy small batches of data (up to 10K rows per transaction) and thus doesn't significantly interfere with user's activity.
131
+
Starts a background worker to move data from parent table to partitions. The worker utilizes short transactions to copy small batches of data (up to 10K rows per transaction) and thus doesn't significantly interfere with user's activity. If the worker is unable to lock rows of a batch, it sleeps for `sleep_time` seconds before the next attempt and tries again up to 60 times, and quits if it's still unable to lock the batch.
115
132
116
133
```plpgsql
117
134
stop_concurrent_part_task(relation REGCLASS)
@@ -131,25 +148,25 @@ Same as above, but for a RANGE-partitioned table.
131
148
### Post-creation partition management
132
149
```plpgsql
133
150
split_range_partition(partition REGCLASS,
134
-
value ANYELEMENT,
151
+
split_value ANYELEMENT,
135
152
partition_name TEXT DEFAULT NULL)
136
153
```
137
-
Split RANGE `partition` in two by `value`. Partition creation callback is invoked for a new partition if available.
154
+
Split RANGE `partition` in two by `split_value`. Partition creation callback is invoked for a new partition if available.
Set partition creation callback to be invoked for each attached or created partition (both HASH and RANGE).
229
+
Set partition creation callback to be invoked for each attached or created partition (both HASH and RANGE). The callback must have the following signature: `part_init_callback(args JSONB) RETURNS VOID`. Parameter `arg` consists of several fields whose presence depends on partitioning type:
230
+
```json
231
+
/* RANGE-partitioned table abc (child abc_4) */
232
+
{
233
+
"parent": "abc",
234
+
"parttype": "2",
235
+
"partition": "abc_4",
236
+
"range_max": "401",
237
+
"range_min": "301"
238
+
}
239
+
240
+
/* HASH-partitioned table abc (child abc_0) */
241
+
{
242
+
"parent": "abc",
243
+
"parttype": "1",
244
+
"partition": "abc_0"
245
+
}
246
+
```
213
247
214
248
## Views and tables
215
249
@@ -221,8 +255,7 @@ CREATE TABLE IF NOT EXISTS pathman_config (
221
255
parttype INTEGERNOT NULL,
222
256
range_interval TEXT,
223
257
224
-
CHECK (parttype IN (1, 2)) /* check for allowed part types */
225
-
);
258
+
CHECK (parttype IN (1, 2)) /* check for allowed part types */ );
226
259
```
227
260
This table stores a list of partitioned tables.
228
261
@@ -232,8 +265,7 @@ CREATE TABLE IF NOT EXISTS pathman_config_params (
232
265
partrel REGCLASS NOT NULLPRIMARY KEY,
233
266
enable_parent BOOLEANNOT NULL DEFAULT TRUE,
234
267
auto BOOLEANNOT NULL DEFAULT TRUE,
235
-
init_callback REGPROCEDURE NOT NULL DEFAULT 0
236
-
);
268
+
init_callback REGPROCEDURE NOT NULL DEFAULT 0);
237
269
```
238
270
This table stores optional parameters which override standard behavior.
239
271
@@ -259,7 +291,7 @@ This view lists all currently running concurrent partitioning tasks.
259
291
#### `pathman_partition_list` --- list of all existing partitions
260
292
```plpgsql
261
293
-- helper SRF function
262
-
CREATE OR REPLACE FUNCTION @extschema@.show_partition_list()
294
+
CREATE OR REPLACEFUNCTIONshow_partition_list()
263
295
RETURNS TABLE (
264
296
parent REGCLASS,
265
297
partition REGCLASS,
@@ -432,6 +464,24 @@ SELECT * FROM pathman_concurrent_part_tasks;
432
464
(1 row)
433
465
```
434
466
467
+
-`pathman_partition_list` in conjunction with `drop_range_partition()` can be used to drop RANGE partitions in a more flexible way compared to good old `DROP TABLE`:
468
+
```plpgsql
469
+
SELECT drop_range_partition(partition, false) /* move data to parent */
470
+
FROM pathman_partition_list
471
+
WHERE parent ='part_test'::regclass AND range_min::int<500;
472
+
NOTICE: 1 rows copied from part_test_11
473
+
NOTICE: 100 rows copied from part_test_1
474
+
NOTICE: 100 rows copied from part_test_2
475
+
drop_range_partition
476
+
----------------------
477
+
dummy_test_11
478
+
dummy_test_1
479
+
dummy_test_2
480
+
(3 rows)
481
+
```
482
+
483
+
- You can turn foreign tables into partitions using the `attach_range_partition()` function. Rows that were meant to be inserted into parent will be redirected to foreign partitions (as usual, PartitionFilter will be involved), though by default it is prohibited to insert rows into partitions provided not by `postgres_fdw`. Only superuser is allowed to set `pg_pathman.insert_into_fdw` GUC variable.
484
+
435
485
### HASH partitioning
436
486
Consider an example of HASH partitioning. First create a table with some integer column:
437
487
```plpgsql
@@ -471,7 +521,7 @@ Notice that the `Append` node contains only one child scan which corresponds to
471
521
> **Important:** pay attention to the fact that `pg_pathman` excludes the parent table from the query plan.
@@ -558,7 +607,7 @@ EXPLAIN SELECT * FROM journal WHERE dt >= '2015-06-01' AND dt < '2015-06-03';
558
607
### Disabling `pg_pathman`
559
608
There are several user-accessible [GUC](https://www.postgresql.org/docs/9.5/static/config-setting.html) variables designed to toggle the whole module or specific custom nodes on and off:
0 commit comments