このトピックでは、pg_pathman拡張機能の使用方法について説明します。
背景情報
pg_pathmanは、 PolarDB for PostgreSQL (Compatible with Oracle) クラスターで使用できる拡張機能です。 pg_pathman拡張機能は、効率的なパーティショニング機能を提供し、テーブルパーティショニングを効果的に管理し、パーティションテーブルのパフォーマンスを向上させます。
拡張機能を作成するCreate the extension
test=# create extension pg_pathman;
CREATE EXTENSION拡張機能のバージョンを表示する
次のコマンドを実行して、インストールされている拡張機能と拡張機能のバージョンを表示します。 pg_pathman拡張がインストールされている場合、その名前とバージョン番号がコマンド出力に表示されます。
test=# \dx
List of installed extensions
Name | Version | Schema | Description
------------+---------+------------+----------------------------------
pg_pathman | 1.5 | public | Partitioning tool for PostgreSQL
plpgsql | 1.0 | pg_catalog | PL/pgSQL procedural language
(2 rows)拡張機能のアップグレード
PolarDB for PostgreSQL (Compatible with Oracle) クラスターは、より良いデータベースサービスのためにpg_pathman拡張機能を定期的にアップグレードします。 pg_pathman拡張機能を手動でアップグレードするには、クラスターを最新バージョンにアップグレードします。
特徴
ハッシュ分割と範囲分割をサポートします。
自動および手動のパーティション管理を提供します。 自動パーティション管理では、関数を使用して自動的にパーティションを作成し、親テーブルからパーティションにデータを移行します。 手動パーティション管理では、関数を使用して、既存のテーブルを親テーブルにアタッチするか、親テーブルからテーブルをデタッチします。
INT、FLOAT、DATE列タイプ、カスタムドメインなど、パーティション分割用の幅広い列タイプをサポートします。
パーティション分割テーブルのクエリに最適化されたプランを提供し、プランで結合やサブクエリなどの戦略を使用します。
カスタムプランノード
RuntimeAppendおよびRuntimeMergeAppendを使用して、動的パーティション選択を有効にします。PartitionFilter機能を使用して、クエリ条件に基づいてパーティションを動的にフィルタリングします。挿入されたデータが現在のパーティション境界を超えると、新しいパーティションが自動的に追加されます。 この機能は、範囲分割でのみ使用できます。
COPY from /TOステートメントを使用して、パーティションテーブルからデータを直接読み取り、パーティションテーブルにデータを書き込みます。トリガーを追加してパーティションキーを更新できます。 パーティションキーを更新する必要がない場合は、トリガーがパフォーマンスに悪影響を与える可能性があるため、トリガーを追加しないことをお勧めします。
パーティションの作成時に自動的に呼び出されるカスタムコールバック関数を設定できます。
パーティション分割テーブルを作成し、親テーブルからパーティションにデータを非ブロッキングで移行できます。
PostgreSQLのforeign data Wrappers (FDW) または他のFDWによって管理される外部テーブルにデータを挿入できます。 この機能を設定するには、
pg_pathman.insert_into_fdw=(disabled | postgres | any_fdw)パラメーターを使用します。
使用状況
詳細については、『GitHub』をご参照ください。
ビューおよびテーブル
pg_pathman拡張機能は、関数を使用してパーティションテーブルを管理し、パーティションテーブルのステータスを表示するためのビューを作成します。
pathman_config
CREATE TABLE IF NOT EXISTS pathman_config ( partrel REGCLASS NOT NULL PRIMARY KEY, -- The object identifier (OID) of the parent table. attname TEXT NOT NULL, -- The name of the partition key column. parttype INTEGER NOT NULL, -- The partitioning type (hash or range). range_interval TEXT, -- The range or span of values that each partition covers. CHECK (parttype IN (1, 2)) /* check for allowed part types */ );pathman_config_params
pathman_config_paramsが存在しない場合は
CREATE TABLE IF NOT EXISTS pathman_config_params ( partrel REGCLASS NOT NULL PRIMARY KEY, -- The OID of the parent table. enable_parent BOOLEAN NOT NULL DEFAULT TRUE, -- Specifies whether to filter the parent table in the optimizer. auto BOOLEAN NOT NULL DEFAULT TRUE, -- Specifies whether to automatically add new partitions. init_callback REGPROCEDURE NOT NULL DEFAULT 0); -- The OID of the initialization callback function that is invoked each time partitions are created.pathman_concurrent_part_tasks
-- helper SRF function CREATE OR REPLACE FUNCTION show_concurrent_part_tasks() RETURNS TABLE ( userid REGROLE, pid INT, dbid OID, relid REGCLASS, processed INT, status TEXT) AS 'pg_pathman', 'show_concurrent_part_tasks_internal' LANGUAGE C STRICT; CREATE OR REPLACE VIEW pathman_concurrent_part_tasks AS SELECT * FROM show_concurrent_part_tasks();pathman_partition_list
-- helper SRF function CREATE OR REPLACE FUNCTION show_partition_list() RETURNS TABLE ( parent REGCLASS, partition REGCLASS, parttype INT4, partattr TEXT, range_min TEXT, range_max TEXT) AS 'pg_pathman', 'show_partition_list_internal' LANGUAGE C STRICT; CREATE OR REPLACE VIEW pathman_partition_list AS SELECT * FROM show_partition_list();
パーティション管理
範囲分割を実行します。
4つの管理関数を使用して、範囲パーティションを作成できます。 2つの関数を使用して、開始値、間隔、およびパーティション数を指定できます。 以下のセクションでは、機能について説明する。
create_range_partitions(relation REGCLASS, -- The OID of the parent table. attribute TEXT, -- The name of the partition key column. start_value ANYELEMENT, -- The start value of the range for the first partition. p_interval ANYELEMENT, -- The range or span of values that each partition covers. ANYELEMENT allows you to specify a data type as needed. p_count INTEGER DEFAULT NULL, -- The number of partitions that you want to create. partition_data BOOLEAN DEFAULT TRUE) -- Specifies whether to immediately migrate data from the parent table to partitions. We recommend that you invoke the partition_table_concurrently() function to perform non-blocking data migration. create_range_partitions(relation REGCLASS, -- The OID of the parent table. attribute TEXT, -- The name of the partition key column. start_value ANYELEMENT, -- The start value of the range for the first partition. p_interval INTERVAL, -- The range or span of values that each partition covers. INTERVAL specifies a specific period of time, which is used to partition tables based on time-related attributes. p_count INTEGER DEFAULT NULL, -- The number of partitions that you want to create. partition_data BOOLEAN DEFAULT TRUE) -- Specifies whether to immediately migrate data from the parent table to partitions. We recommend that you invoke the partition_table_concurrently() function to perform non-blocking data migration.他の2つの関数を使用して、開始値、終了値、および間隔を指定できます。 次のセクションでは、関数について説明します。
create_partitions_from_range(relation REGCLASS, -- The OID of the parent table. attribute TEXT, -- The name of the partition key column. start_value ANYELEMENT, -- The start value of the range for the first partition. end_value ANYELEMENT, -- The end value of the range for the last partition. p_interval ANYELEMENT, -- The range or span of values that each partition covers. ANYELEMENT allows you to specify a data type as needed. partition_data BOOLEAN DEFAULT TRUE) -- Specifies whether to immediately migrate data from the parent table to partitions. We recommend that you invoke the partition_table_concurrently() function to perform non-blocking data migration. create_partitions_from_range(relation REGCLASS, -- The OID of the parent table. attribute TEXT, -- The name of the partition key column. start_value ANYELEMENT, -- The start value of the range for the first partition. end_value ANYELEMENT, -- The end value of the range for the last partition. p_interval INTERVAL, -- The range or span of values that each partition covers. INTERVAL specifies a specific period of time, which is used to partition tables based on time-related attributes. partition_data BOOLEAN DEFAULT TRUE) -- Specifies whether to immediately migrate data from the parent table to partitions. We recommend that you invoke the partition_table_concurrently() function to perform non-blocking data migration.例:
Create a parent table that you want to partition. postgres=# create table part_test(id int, info text, crt_time timestamp not null); -- The partition key column must contain the NOT NULL constraint. CREATE TABLE Insert test data into the parent table. postgres=# insert into part_test select id,md5(random()::text),clock_timestamp() + (id||' hour')::interval from generate_series(1,10000) t(id); INSERT 0 10000 postgres=# select * from part_test limit 10; id | info | crt_time ----+----------------------------------+---------------------------- 1 | 36fe1adedaa5b848caec4941f87d443a | 2016-10-25 10:27:13.206713 2 | c7d7358e196a9180efb4d0a10269c889 | 2016-10-25 11:27:13.206893 3 | 005bdb063550579333264b895df5b75e | 2016-10-25 12:27:13.206904 4 | 6c900a0fc50c6e4da1ae95447c89dd55 | 2016-10-25 13:27:13.20691 5 | 857214d8999348ed3cb0469b520dc8e5 | 2016-10-25 14:27:13.206916 6 | 4495875013e96e625afbf2698124ef5b | 2016-10-25 15:27:13.206921 7 | 82488cf7e44f87d9b879c70a9ed407d4 | 2016-10-25 16:27:13.20693 8 | a0b92547c8f17f79814dfbb12b8694a0 | 2016-10-25 17:27:13.206936 9 | 2ca09e0b85042b476fc235e75326b41b | 2016-10-25 18:27:13.206942 10 | 7eb762e1ef7dca65faf413f236dff93d | 2016-10-25 19:27:13.206947 (10 rows) Note: 1. The partition key column must contain the NOT NULL constraint. 2. The number of partitions must be sufficient to cover all existing records. Create partitions and ensure that each partition contains one month of data. postgres=# select create_range_partitions('part_test'::regclass, -- The OID of the parent table. 'crt_time', -- The name of the partition key column. '2016-10-25 00:00:00'::timestamp, -- The start value of the range for the first partition. interval '1 month', -- The range or span of values that each partition covers. 24, -- The number of partitions that you want to create. false) ; -- The data is not migrated. NOTICE: sequence "part_test_seq" does not exist, skipping create_range_partitions ------------------------- 24 (1 row) postgres-# \d+ part_test Table "public.part_test" Column | Type | Modifiers | Storage | Stats target | Description ----------+-----------------------------+-----------+----------+--------------+------------- id | integer | | plain | | info | text | | extended | | crt_time | timestamp without time zone | not null | plain | | Child tables: part_test_1, part_test_10, part_test_11, part_test_12, part_test_13, part_test_14, part_test_15, part_test_16, part_test_17, part_test_18, part_test_19, part_test_2, part_test_20, part_test_21, part_test_22, part_test_23, part_test_24, part_test_3, part_test_4, part_test_5, part_test_6, part_test_7, part_test_8, part_test_9 The data is still in the parent table because the data is not migrated. postgres=# select count(*) from only part_test; count ------- 10000 (1 row) Perform non-blocking data migration. partition_table_concurrently(relation REGCLASS, -- The OID of the parent table. batch_size INTEGER DEFAULT 1000, -- The number of records in a transaction that you want to migrate from the parent table. sleep_time FLOAT8 DEFAULT 1.0) -- The retry interval following a failed attempt to acquire a row lock. After 60 failed attempts, the task is ended. postgres=# select partition_table_concurrently('part_test'::regclass, 10000, 1.0); NOTICE: worker started, you can stop it with the following command: select stop_concurrent_part_task('part_test'); partition_table_concurrently ------------------------------ (1 row) After the migration is completed, all data is migrated to the partitions, and the parent table is empty. postgres=# select count(*) from only part_test; count ------- 0 (1 row) After you migrate the data, we recommend that you disable the parent table. This way, the parent table is not included in the execution plan. postgres=# select set_enable_parent('part_test'::regclass, false); set_enable_parent ------------------- (1 row) postgres=# explain select * from part_test where crt_time = '2016-10-25 00:00:00'::timestamp; QUERY PLAN --------------------------------------------------------------------------------- Append (cost=0.00..16.18 rows=1 width=45) -> Seq Scan on part_test_1 (cost=0.00..16.18 rows=1 width=45) Filter: (crt_time = '2016-10-25 00:00:00'::timestamp without time zone) (3 rows)説明範囲分割を使用する場合は、次の項目に注意してください。
パーティションキー列にNOT NULL制約を含める必要があります。
既存のすべてのレコードに十分対応できるパーティション数とする必要があります。
ノンブロッキングデータ移行を実行します。
データ移行が完了したら、親テーブルを無効にします。
ハッシュ分割を実行します。
次の管理機能を使用して範囲パーティションを作成し、パーティションの開始値、間隔、および数を指定できます。
create_hash_partitions(relation REGCLASS, -- The OID of the parent table. attribute TEXT, -- The name of the partition key column. partitions_count INTEGER, -- The number of partitions that you want to create. partition_data BOOLEAN DEFAULT TRUE) -- Specifies whether to immediately migrate data from the parent table to the partitions. We recommend that you invoke the partition_table_concurrently() function to perform non-blocking data migration.例:
-- Create a parent table that you want to partition. postgres=# create table part_test(id int, info text, crt_time timestamp not null); -- The partition key column must contain the NOT NULL constraint. CREATE TABLE Insert test data into the parent table. postgres=# insert into part_test select id,md5(random()::text),clock_timestamp() + (id||' hour')::interval from generate_series(1,10000) t(id); INSERT 0 10000 postgres=# select * from part_test limit 10; id | info | crt_time ----+----------------------------------+---------------------------- 1 | 29ce4edc70dbfbe78912beb7c4cc95c2 | 2016-10-25 10:47:32.873879 2 | e0990a6fb5826409667c9eb150fef386 | 2016-10-25 11:47:32.874048 3 | d25f577a01013925c203910e34470695 | 2016-10-25 12:47:32.874059 4 | 501419c3f7c218e562b324a1bebfe0ad | 2016-10-25 13:47:32.874065 5 | 5e5e22bdf110d66a5224a657955ba158 | 2016-10-25 14:47:32.87407 6 | 55d2d4fd5229a6595e0dd56e13d32be4 | 2016-10-25 15:47:32.874076 7 | 1dfb9a783af55b123c7a888afe1eb950 | 2016-10-25 16:47:32.874081 8 | 41eeb0bf395a4ab1e08691125ae74bff | 2016-10-25 17:47:32.874087 9 | 83783d69cc4f9bb41a3978fe9e13d7fa | 2016-10-25 18:47:32.874092 10 | affc9406d5b3412ae31f7d7283cda0dd | 2016-10-25 19:47:32.874097 (10 rows) Note: 1. The partition key column must contain the NOT NULL constraint. Create 128 partitions postgres=# select create_hash_partitions('part_test'::regclass, -- The OID of the parent table. 'crt_time', -- The name of the partition key column. 128, -- The number of partitions that you want to create. false) ; -- The data is not migrated. create_hash_partitions ------------------------ 128 (1 row) postgres=# \d+ part_test Table "public.part_test" Column | Type | Modifiers | Storage | Stats target | Description ----------+-----------------------------+-----------+----------+--------------+------------- id | integer | | plain | | info | text | | extended | | crt_time | timestamp without time zone | not null | plain | | Child tables: part_test_0, part_test_1, part_test_10, part_test_100, part_test_101, part_test_102, part_test_103, part_test_104, part_test_105, part_test_106, part_test_107, part_test_108, part_test_109, part_test_11, part_test_110, part_test_111, part_test_112, part_test_113, part_test_114, part_test_115, part_test_116, part_test_117, part_test_118, part_test_119, part_test_12, part_test_120, part_test_121, part_test_122, part_test_123, part_test_124, part_test_125, part_test_126, part_test_127, part_test_13, part_test_14, part_test_15, part_test_16, part_test_17, part_test_18, part_test_19, part_test_2, part_test_20, part_test_21, part_test_22, part_test_23, part_test_24, part_test_25, part_test_26, part_test_27, part_test_28, part_test_29, part_test_3, part_test_30, part_test_31, part_test_32, part_test_33, part_test_34, part_test_35, part_test_36, part_test_37, part_test_38, part_test_39, part_test_4, part_test_40, part_test_41, part_test_42, part_test_43, part_test_44, part_test_45, part_test_46, part_test_47, part_test_48, part_test_49, part_test_5, part_test_50, part_test_51, part_test_52, part_test_53, part_test_54, part_test_55, part_test_56, part_test_57, part_test_58, part_test_59, part_test_6, part_test_60, part_test_61, part_test_62, part_test_63, part_test_64, part_test_65, part_test_66, part_test_67, part_test_68, part_test_69, part_test_7, part_test_70, part_test_71, part_test_72, part_test_73, part_test_74, part_test_75, part_test_76, part_test_77, part_test_78, part_test_79, part_test_8, part_test_80, part_test_81, part_test_82, part_test_83, part_test_84, part_test_85, part_test_86, part_test_87, part_test_88, part_test_89, part_test_9, part_test_90, part_test_91, part_test_92, part_test_93, part_test_94, part_test_95, part_test_96, part_test_97, part_test_98, part_test_99 The data is still in the parent table because the data is not migrated. postgres=# select count(*) from only part_test; count ------- 10000 (1 row) Perform non-blocking data migration. partition_table_concurrently(relation REGCLASS, -- The OID of the parent table. batch_size INTEGER DEFAULT 1000, -- The number of records in a transaction that you want to migrate from the parent table. sleep_time FLOAT8 DEFAULT 1.0) -- The retry interval following a failed attempt to acquire a row lock. After 60 failed attempts, the task is ended. postgres=# select partition_table_concurrently('part_test'::regclass, 10000, 1.0); NOTICE: worker started, you can stop it with the following command: select stop_concurrent_part_task('part_test'); partition_table_concurrently ------------------------------ (1 row) After the migration is completed, all data is migrated to the partitions, and the parent table is empty. postgres=# select count(*) from only part_test; count ------- 0 (1 row) After you migrate the data, we recommend that you disable the parent table. This way, the parent table is not included in the execution plan. postgres=# select set_enable_parent('part_test'::regclass, false); set_enable_parent ------------------- (1 row) Query a single partition. postgres=# explain select * from part_test where crt_time = '2016-10-25 00:00:00'::timestamp; QUERY PLAN --------------------------------------------------------------------------------- Append (cost=0.00..1.91 rows=1 width=45) -> Seq Scan on part_test_122 (cost=0.00..1.91 rows=1 width=45) Filter: (crt_time = '2016-10-25 00:00:00'::timestamp without time zone) (3 rows) The pg_pathman extension automatically converts the statement. In traditional inheritance-based partitioning, you cannot filter partitions by using a statement similar to select * from part_test where crt_time = '2016-10-25 00:00:00'::timestamp;. postgres=# \d+ part_test_122 Table "public.part_test_122" Column | Type | Modifiers | Storage | Stats target | Description ----------+-----------------------------+-----------+----------+--------------+------------- id | integer | | plain | | info | text | | extended | | crt_time | timestamp without time zone | not null | plain | | The following section describes the constraints on the partitioned tables: Check constraints: "pathman_part_test_122_3_check" CHECK (get_hash_part_idx(timestamp_hash(crt_time), 128) = 122) Inherits: part_test説明ハッシュ分割を使用する場合は、次の項目に注意してください。
パーティションキー列にNOT NULL制約を含める必要があります。
ノンブロッキングデータ移行を実行します。
データ移行が完了したら、親テーブルを無効にします。
pg_pathman拡張は、ステートメントの書き方に関係なく機能します。 たとえば、
select * from part_test where crt_time = '2016-10-25 00:00:00:00 '::timestamp;などのステートメントは、ハッシュパーティショニングでも認識および実行できます。ハッシュパーティション列は、int 型の列に限定されません。 列の型はハッシュ関数によって自動的に変換されます。
データをパーティションに移行します。
親テーブルにデータが残っている場合は、ノンブロッキングデータ移行を実行してデータをパーティションに移行します。
tmpで
with tmp as (delete from <parent table> limit xx nowait returning *) insert into <partitions> select * from tmp select array_agg(ctid) from <parent table> limit xx for update nowati -- You can also use this statement to label rows. Then, perform DELETE and INSERT operationsを実行します。
機能:
partition_table_concurrently(relation REGCLASS, -- The OID of the parent table. batch_size INTEGER DEFAULT 1000, -- The number of records in a transaction that you want to migrate from the parent table. sleep_time FLOAT8 DEFAULT 1.0) -- The retry interval following a failed attempt to acquire a row lock. After 60 failed attempts, the task is ended.例:
postgres=# select partition_table_concurrent ('part_test'::regclass、 10000, 1.0); NOTICE: worker started、次のコマンドで停止できます。select stop_concurrent_part_task('part_test'); partition_table_concurrent ------------------------------ (1行)データ移行タスクを停止するには、次の関数を呼び出します。
stop_concurrent_part_task (リレーションREGCLASS)バックグラウンドで実行されているデータ移行タスクを確認します。
postgres=# select * from pathman_concurrent_part_tasks; userid | pid | dbid | relid | processed | status ------- ----- ------- ---------- ------------------------------ (0行)範囲パーティションを分割します。
過度に大きな範囲のパーティションを小さなパーティションに分割するには、次の関数を使用します。
split_range_partition(partition REGCLASS, -- パーティションのOID。 split_value ANYELEMENT, -- The split value. partition_name TEXT DEFAULT NULL) -- 追加されたパーティションの名前
例:
postgres=# \d + part_test Table "public.part_test" 列 | タイプ | モディファイア | ストレージ | 統計ターゲット | 説明 ---------- ----------------------------- ------------------------------------------------------------------ id | integer | | plain | | info | text | | extended | | crt_time | タイムゾーンなしのタイムスタンプ | nullではない | プレーン | | 子テーブル: part_test_1、 part_test_10, part_test_11, part_test_12, part_test_13, part_test_14, part_test_15, part_test_16, part_test_17, part_test_18, part_test_19, part_test_2, part_test_20, part_test_21, part_test_22, part_test_23, part_test_24, part_test_3, part_test_4, part_test_5, part_test_6, part_test_7, part_test_8, part_test_9 postgres=# \d + part_test_1 Table "public.part_test_1" 列 | タイプ | モディファイア | ストレージ | 統計ターゲット | 説明 ---------- ----------------------------- ------------------------------------------------------------------ id | integer | | plain | | info | text | | extended | | crt_time | タイムゾーンなしのタイムスタンプ | nullではない | プレーン | | チェック制約: "pathman_part_test_1_3_check" CHECK (crt_time >= '2016-10-25 00:00:00 ': タイムゾーンなしのタイムスタンプとcrt_time < '2016-11-25 00:00:00': タイムゾーンなしのタイムスタンプ) 継承: part_testパーティションを分割します。
postgres=# select split_range_partition('part_test_1 '::regclass, -- パーティションのOID。 '2016-11-10 00:00:00 '::timestamp, -- パーティションが分割された時点。 'part_test_1_2 '); -- 新しいパーティションの名前。 split_range_partition ----------------------------------------------- {"2016-10-25 00:00:00","2016-11-25 00:00:00"} (1行)パーティションは次のパーティションに分割されます。
postgres=# \d + part_test_1 Table "public.part_test_1" 列 | タイプ | モディファイア | ストレージ | 統計ターゲット | 説明 ---------- ----------------------------- ------------------------------------------------------------------ id | integer | | plain | | info | text | | extended | | crt_time | タイムゾーンなしのタイムスタンプ | nullではない | プレーン | | チェック制約: "pathman_part_test_1_3_check" CHECK (crt_time >= '2016-10-25 00:00:00 ': タイムスタンプなしタイムゾーンおよびcrt_time <''2016-11-10 00:00:00': タイムスタンプなしタイムゾーン) 継承: part_test postgres=# \d + part_test_1_2 Table "public.part_test_1_2" 列 | タイプ | モディファイア | ストレージ | 統計ターゲット | 説明 ---------- ----------------------------- ------------------------------------------------------------------ id | integer | | plain | | info | text | | extended | | crt_time | タイムゾーンなしのタイムスタンプ | nullではない | プレーン | | チェック制約: "pathman_part_test_1_2_3_check" CHECK (crt_time >= '2016-11-10 00:00:00 ': タイムスタンプなしタイムゾーンとcrt_time <''2016-11-25 00:00:00': タイムスタンプなしタイムゾーン) 継承: part_testデータは自動的に他のパーティションに移行されます。
postgres=# select count(*) from part_test_1; カウント ------- 373 (1行) postgres=# part_test_1_2からカウント (*) を選択します。 カウント ------- 360 (1行)次の例では、継承関係について説明します。
postgres=# \d + part_test Table "public.part_test" 列 | タイプ | モディファイア | ストレージ | 統計ターゲット | 説明 ---------- ----------------------------- ------------------------------------------------------------------ id | integer | | plain | | info | text | | extended | | crt_time | タイムゾーンなしのタイムスタンプ | nullではない | プレーン | | 子テーブル: part_test_1、 part_test_10, part_test_11, part_test_12, part_test_13, part_test_14, part_test_15, part_test_16, part_test_17, part_test_18, part_test_19, part_test_1_2, -- 追加されたパーティション。 part_test_2, part_test_20, part_test_21, part_test_22, part_test_23, part_test_24, part_test_3, part_test_4, part_test_5, part_test_6, part_test_7, part_test_8, part_test_9範囲パーティションをマージします。
次の関数を呼び出して、範囲パーティションをマージします。
-- マージする2つのパーティションを指定します。 パーティションは隣接している必要があります。 merge_range_partitions(partition1 REGCLASS, partition2 REGCLASS)例:
postgres=# select merge_range_partitions('part_test_2 '::regclass、'part_test_12'::regclass) ; エラー: マージに失敗しました。パーティションは隣接している必要があります コンテキスト: PL/pgSQL関数merge_range_partitions_internal(regclass、regclass、regclass、anyelement) 27行目 (RAISE) SQLステートメント「SELECT public.merge_range_partitions_internal($1、$2、$3、NULL:: タイムスタンプなしタイムゾーン) 」 EXECUTEのPL/pgSQL関数merge_range_partitions(regclass、regclass) 44行目 パーティションが隣接していないため、エラーが返されます。 隣接するパーティションをマージできます。 postgres=# select merge_range_partitions('part_test_1'::regclass, 'part_test_1_2'::regclass) ; merge_range_partitions ------------------------ (1行)パーティションをマージすると、いずれかのパーティションが削除されます。
postgres=# \d part_test_1_2 "part_test_1_2" というリレーションが見つかりませんでした。 postgres=# \d part_test_1 Table "public.part_test_1" 列 | タイプ | 修飾子 --------- ----------------------------- -------------- id | integer | info | text | crt_time | タイムゾーンなしのタイムスタンプ | nullではない チェック制約: "pathman_part_test_1_3_check" CHECK (crt_time >= '2016-10-25 00:00:00 ': タイムゾーンなしのタイムスタンプとcrt_time < '2016-11-25 00:00:00': タイムゾーンなしのタイムスタンプ) 継承: part_test postgres=# part_test_1からカウント (*) を選択します。 カウント ------- 733 (1行)新しい範囲パーティションを追加します。
パーティション分割された親テーブルにパーティションを追加するには、いくつかの方法を使用できます。 1つの方法は、親テーブルの最後にパーティションを追加することです。
新しいパーティションを親テーブルに追加する場合、パーティションテーブルの作成時に指定された間隔が使用されます。 pathman_configテーブルから間隔を照会できます。
postgres=# select * from pathman_config; partrel | attname | parttype | range_interval ---------- --------------------------- ---------------- part_test | crt_time | 2 | 1 mon (1行)新しいレンジパーティションを追加します (テーブルスペースを指定することはできません) 。
append_range_partition(parent REGCLASS, -- 親テーブルのOID。 partition_name TEXT DEFAULT NULL, -- 新しいパーティションの名前。 このパラメーターは必須ではありません。 tablespace TEXT DEFAULT NULL) -- 新しいパーティションのテーブルスペース。 このパラメーターは必要ありません。例:
postgres=# select append_range_partition('part_test'::regclass); append_range_partition ------------------------ public.part_test_25 (1行) postgres=# \d + part_test_25 Table "public.part_test_25" 列 | タイプ | モディファイア | ストレージ | 統計ターゲット | 説明 ---------- ----------------------------- ------------------------------------------------------------------ id | integer | | plain | | info | text | | extended | | crt_time | タイムゾーンなしのタイムスタンプ | nullではない | プレーン | | チェック制約: "pathman_part_test_25_3_check" CHECK (crt_time >= '2018-10-25 00:00:00 ': タイムスタンプなしタイムゾーンとcrt_time <''2018-11-25 00:00:00': タイムスタンプなしタイムゾーン) 継承: part_test postgres=# \d + part_test_24 Table "public.part_test_24" 列 | タイプ | モディファイア | ストレージ | 統計ターゲット | 説明 ---------- ----------------------------- ------------------------------------------------------------------ id | integer | | plain | | info | text | | extended | | crt_time | タイムゾーンなしのタイムスタンプ | nullではない | プレーン | | チェック制約: "pathman_part_test_24_3_check" CHECK (crt_time >= '2018-09-25 00:00:00 ': タイムスタンプなしタイムゾーンとcrt_time < '2018-10-25 00:00:00': タイムスタンプなしタイムゾーン) 継承: part_test新しい範囲パーティションを追加します。
次の関数を使用して、パーティションをテーブルに追加します。
prepend_range_partition (親REGCLASS、 partition_name TEXT DEFAULT NULL, tablespace TEXT DEFAULT NULL)例:
postgres=# select prepend_range_partition('part_test'::regclass); prepend_range_partition ------------------------- public.part_test_26 (1行) postgres=# \d + part_test_26 Table "public.part_test_26" 列 | タイプ | モディファイア | ストレージ | 統計ターゲット | 説明 ---------- ----------------------------- ------------------------------------------------------------------ id | integer | | plain | | info | text | | extended | | crt_time | タイムゾーンなしのタイムスタンプ | nullではない | プレーン | | チェック制約: "pathman_part_test_26_3_check" CHECK (crt_time >= '2016-09-25 00:00:00:00 ': タイムスタンプなしタイムゾーンとcrt_time < ' 2016-10-25 00:00:00 ': タイムスタンプなしタイムゾーン) 継承: part_test postgres=# \d + part_test_1 Table "public.part_test_1" 列 | タイプ | モディファイア | ストレージ | 統計ターゲット | 説明 ---------- ----------------------------- ------------------------------------------------------------------ id | integer | | plain | | info | text | | extended | | crt_time | タイムゾーンなしのタイムスタンプ | nullではない | プレーン | | チェック制約: "pathman_part_test_1_3_check" CHECK (crt_time >= '2016-10-25 00:00:00 ': タイムゾーンなしのタイムスタンプとcrt_time < '2016-11-25 00:00:00': タイムゾーンなしのタイムスタンプ) 継承: part_testパーティションを追加します。
パーティションの開始値を指定して、新しいパーティションを作成できます。 作成するパーティションの範囲が既存のパーティションと重ならない場合は、パーティションを作成できます。 この方法では、不連続なパーティションを作成できます。 たとえば、既存のパーティションの範囲が2010から2015の場合、2020からパーティションを作成できます。 2015 から 2020 の間のパーティションを作成する必要はありません。 機能:
add_range_partition(relation REGCLASS, -- 親テーブルのOID start_value ANYELEMENT, -- パーティションの範囲の開始値。 end_value ANYELEMENT, -- パーティションの範囲の終了値。 partition_name TEXT DEFAULT NULL, -- The name of the partition. tablespace TEXT DEFAULT NULL) -パーティションのテーブルスペース例:
postgres=# select add_range_partition('part_test'::regclass, -- 親テーブルのOID '2020-01-01 00:00:00 '::timestamp, -- パーティションの範囲の開始値。 '2020-02-01 00:00:00 '::timestamp); -パーティションの範囲の終了値。 add_range_partition --------------------- public.part_test_27 (1行) postgres=# \d + part_test_27 Table "public.part_test_27" 列 | タイプ | モディファイア | ストレージ | 統計ターゲット | 説明 ---------- ----------------------------- ------------------------------------------------------------------ id | integer | | plain | | info | text | | extended | | crt_time | タイムゾーンなしのタイムスタンプ | nullではない | プレーン | | チェック制約: "pathman_part_test_27_3_check" CHECK (crt_time >= '2020-01-01 00:00:00 ': タイムスタンプなしタイムゾーンとcrt_time < '2020-02-01 00:00:00': タイムスタンプなしタイムゾーン) 継承: part_testパーティションの削除
単一のパーティション範囲を削除するには、次の関数を呼び出します。
drop_range_partition(partition TEXT, -- パーティション名。 delete_data BOOLEAN DEFAULT TRUE) − パーティションのデータを削除するかどうかを指定する。 値をFALSEに設定すると、パーティションのデータが親テーブルに移行されます。 delete_dataがtrueの場合、RANGEパーティションとそのすべてのデータをドロップします。例:
-- パーティションを削除し、パーティションのデータを親テーブルに移行します。 postgres=# select drop_range_partition('part_test_1 '、false); 注意: part_test_1からコピーされた733行 drop_range_partition ---------------------- part_test_1 (1行) postgres=# select drop_range_partition('part_test_2 '、false); 注意: part_test_2からコピーされた720行 drop_range_partition ---------------------- part_test_2 (1行) postgres=# part_testからカウント (*) を選択します。 カウント ------- 10000 (1行) データを親テーブルに移行せずに、パーティションとパーティションのデータを削除します。 postgres=# select drop_range_partition('part_test_3',true); drop_range_partition ---------------------- part_test_3 (1行) postgres=# part_testからカウント (*) を選択します。 カウント ------- 9256 (1行) postgres=# part_testのみからカウント (*) を選択します。 カウント ------- 1453 (1行)すべてのパーティションを削除し、パーティションのデータを親テーブルに移行するかどうかを指定します。 機能:
drop_partitions (親REGCLASS、 delete_data BOOLEAN DEFAULT FALSE) 親テーブルのパーティション (外部関係とローカル関係の両方) を削除します。 If delete_data is false, the data is copied to the parent table first. デフォルトはfalseです。例:
postgres=# select drop_partitions('part_test'::regclass, false); -- すべてのパーティションを削除し、パーティションのデータを親テーブルに移行します。 注意: 関数public.part_test_upd_trig_func() が存在しません。注意: part_test_4からコピーされた744行 注意: part_test_5からコピーされた672行 注意: part_test_6からコピーされた744行 注意: part_test_7からコピーされた720行 注意: part_test_8からコピーされた744行 注意: part_test_9からコピーされた720行 注意: part_test_10からコピーされた744行 注意: part_test_11からコピーされた744行 注意: part_test_12からコピーされた720行 注意: part_test_13からコピーされた744行 注意: part_test_14からコピーされた507行 注意: part_test_15からコピーされた0行 注意: part_test_16からコピーされた0行 注意: part_test_17からコピーされた0行 注意: part_test_18からコピーされた0行 注意: part_test_19からコピーされた0行 注意: part_test_20からコピーされた0行 注意: part_test_21からコピーされた0行 注意: part_test_22からコピーされた0行 注意: part_test_23からコピーされた0行 注意: part_test_24からコピーされた0行 注意: part_test_25からコピーされた0行 注意: part_test_26からコピーされた0行 注意: part_test_27からコピーされた0行 drop_partitions ----------------- 24 (1行) postgres=# part_testからカウント (*) を選択します。 カウント ------- 9256 (1行) postgres=# \dt part_test_4 一致する関係は見つかりませんでした。既存のテーブルをパーティションテーブルにアタッチします。
既存のテーブルを親テーブルにアタッチします。 アタッチするテーブルは、親テーブルと同じスキーマを持つ必要があります。 たとえば、2つのテーブルは同じドロップ列を持つ必要があります。 pg_attributeパラメーターは、テーブルのスキーマを指定します。 機能:
attach_range_partition(relation REGCLASS, -- 親テーブルのOID。 パーティションREGCLASS, -- パーティションのOID。 start_value ANYELEMENT, -- パーティションの範囲の開始値。 end_value ANYELEMENT) − パーティションの範囲の終了値。例:
postgres=# createテーブルpart_test_1 (すべてを含むpart_testなど); テーブルの作成 postgres=# \d + part_test Table "public.part_test" 列 | タイプ | モディファイア | ストレージ | 統計ターゲット | 説明 ---------- ----------------------------- ------------------------------------------------------------------ id | integer | | plain | | info | text | | extended | | crt_time | タイムゾーンなしのタイムスタンプ | nullではない | プレーン | | postgres=# \d + part_test_1 Table "public.part_test_1" 列 | タイプ | モディファイア | ストレージ | 統計ターゲット | 説明 ---------- ----------------------------- ------------------------------------------------------------------ id | integer | | plain | | info | text | | extended | | crt_time | タイムゾーンなしのタイムスタンプ | nullではない | プレーン | | postgres=# select attach_range_partition('part_test'::regclass, 'part_test_1 '::regclass, '2019-01-01 00:00:00'::timestamp, '2019-02-01 00:00:00:00 ': timestamp); attach_range_partition ------------------------ part_test_1 (1行) テーブルを添付すると、継承関係と制約は自動的に作成されます。 postgres=# \d+ part_test_1 Table "public.part_test_1" 列 | タイプ | モディファイア | ストレージ | 統計ターゲット | 説明 ---------- ----------------------------- ------------------------------------------------------------------ id | integer | | plain | | info | text | | extended | | crt_time | タイムゾーンなしのタイムスタンプ | nullではない | プレーン | | チェック制約: "pathman_part_test_1_3_check" CHECK (crt_time >= '2019-01-01 00:00:00 ': タイムゾーンなしのタイムスタンプとcrt_time < '2019-02-01 00:00:00': タイムゾーンなしのタイムスタンプ) 継承: part_test親テーブルからパーティションをデタッチします (パーティションを通常のテーブルに変換します) 。
親テーブルの継承からパーティションを削除します。 データは削除されません。 継承と制約が削除されます。 機能:
detach_range_partition(partition REGCLASS) -- 通常のテーブルに変換するパーティションを指定します。例:
postgres=# select count(*) from part_test; カウント ------- 9256 (1行) postgres=# part_test_2からカウント (*) を選択します。 カウント ------- 733 (1行) postgres=# select detach_range_partition('part_test_2 '); detach_range_partition ------------------------ part_test_2 (1行) postgres=# part_test_2からカウント (*) を選択します。 カウント ------- 733 (1行) postgres=# part_testからカウント (*) を選択します。 カウント ------- 8523 (1行)親テーブルのpg_pathman拡張を完全に無効にします。
単一の親テーブルのpg_pathman拡張を無効にできます。 機能:
disable_pathman_for (リレーションTEXT) 指定された親テーブルのpg_pathmanパーティショニングメカニズムを完全に無効にし、挿入トリガーが存在する場合は削除します。 All partitions and data remain unchanged. postgres=# \sf disable_pathman_for CREATE OR REPLACE FUNCTION public.disable_pathman_for (parent_rerid regclass) RETURNS void LANGUAGE plpgsql 厳格 AS $function $ 開始 PERFORM public.validate_relname(parent_relid); DELETE FROM public.pathman_config WHERE partrel = parent_relid; PERFORM public.drop_triggers(parent_relid); /* Notify backend about changes */ PERFORM public.on_remove_partitions(parent_relid); 終了 $function$例:
postgres=# select disable_pathman_for('part_test'); 注意: カスケードを他の23個のオブジェクトにドロップする 詳細: カスケードをドロップして、テーブルpart_test_3でpart_test_upd_trigをトリガーします カスケードをドロップしてテーブルpart_test_4でpart_test_upd_trigをトリガーする カスケードをドロップしてテーブルpart_test_5でpart_test_upd_trigをトリガーする カスケードをドロップしてテーブルpart_test_6でpart_test_upd_trigをトリガーする カスケードをドロップしてテーブルpart_test_7でpart_test_upd_trigをトリガーする カスケードをドロップしてテーブルpart_test_8でpart_test_upd_trigをトリガーする カスケードをドロップしてテーブルpart_test_9でpart_test_upd_trigをトリガーする カスケードをドロップしてテーブルpart_test_10でpart_test_upd_trigをトリガーする カスケードをドロップしてテーブルpart_test_11でpart_test_upd_trigをトリガーする カスケードをドロップしてテーブルpart_test_12でpart_test_upd_trigをトリガーする カスケードをドロップしてテーブルpart_test_13にpart_test_upd_trigをトリガーする カスケードをドロップしてテーブルpart_test_14でpart_test_upd_trigをトリガーする カスケードをドロップしてテーブルpart_test_15にpart_test_upd_trigをトリガーする カスケードをドロップしてテーブルpart_test_16でpart_test_upd_trigをトリガーする カスケードをドロップしてテーブルpart_test_17でpart_test_upd_trigをトリガーする カスケードをドロップしてテーブルpart_test_18にpart_test_upd_trigをトリガーする カスケードをドロップしてテーブルpart_test_19でpart_test_upd_trigをトリガーする カスケードをドロップしてテーブルpart_test_20でpart_test_upd_trigをトリガーする カスケードをドロップしてテーブルpart_test_21でpart_test_upd_trigをトリガーする カスケードをドロップしてテーブルpart_test_22でpart_test_upd_trigをトリガーする カスケードをドロップしてテーブルpart_test_23にpart_test_upd_trigをトリガーする カスケードをドロップしてテーブルpart_test_24でpart_test_upd_trigをトリガーする カスケードをドロップしてテーブルpart_test_25でpart_test_upd_trigをトリガーする disable_pathman_for --------------------- (1行) postgres=# \d + part_test Table "public.part_test" 列 | タイプ | モディファイア | ストレージ | 統計ターゲット | 説明 ---------- ----------------------------- ------------------------------------------------------------------ id | integer | | plain | | info | text | | extended | | crt_time | タイムゾーンなしのタイムスタンプ | nullではない | プレーン | | 子テーブル: part_test_10、 part_test_11, part_test_12, part_test_13, part_test_14, part_test_15, part_test_16, part_test_17, part_test_18, part_test_19, part_test_20, part_test_21, part_test_22, part_test_23, part_test_24, part_test_25, part_test_26, part_test_27, part_test_28, part_test_29, part_test_3, part_test_30, part_test_31, part_test_32, part_test_33, part_test_34, part_test_35, part_test_4, part_test_5, part_test_6, part_test_7, part_test_8, part_test_9 postgres=# \d + part_test_10 Table "public.part_test_10" 列 | タイプ | モディファイア | ストレージ | 統計ターゲット | 説明 ---------- ----------------------------- ------------------------------------------------------------------ id | integer | | plain | | info | text | | extended | | crt_time | タイムゾーンなしのタイムスタンプ | nullではない | プレーン | | チェック制約: "pathman_part_test_10_3_check" CHECK (crt_time >= '2017-06-25 00:00:00 ': タイムスタンプなしタイムゾーンおよびcrt_time <''2017-07-25 00:00:00': タイムスタンプなしタイムゾーン) 継承: part_testpg_pathman拡張機能を無効にした後も、継承と制約は変更されません。 pg_pathman拡張は、カスタムスキャン実行プランに介入しません。 次の例では、pg_pathman拡張機能を無効にした後の実行計画について説明します。
postgres=# explain select * from part_test where crt_time='2017-06-25 00:00:00 '::timestamp; クエリ計画 --------------------------------------------------------------------------------- Append (cost=0.00..16.00 rows=2 width=45) -> Seq Scan on part_test (cost=0.00..0.00 rows=1 width=45) Filter: (crt_time = '2017-06-25 00:00:00'::timestamp without time zone) -> Seq Scan on part_test_10 (cost=0.00..16.00 rows=1 width=45) フィルター: (crt_time = '2017-06-25 00:00:00 ': タイムゾーンなしのタイムスタンプ) (5行)重要操作は不可逆的です。 慎重に進んでください。
高度なパーティション管理
親テーブルを無効にします。
親テーブルのすべてのデータをパーティションに移行した後、親テーブルを無効にできます。 機能:
set_enable_parent (リレーションREGCLASS, value BOOLEAN) 親テーブルをクエリプランに /から含める /除外します。 In original PostgreSQL planner parent table is always included into query plan even if it's empty which can lead to additional overhead. You can use disable_parent() if you are never going to use parent table as a storage. Default value depends on the partition_data parameter that was specified during initial partitioning in create_range_partitions() or create_partitions_from_range() functions. If the partition_data parameter was true then all data have already been migrated to partitions and parent table disabled. それ以外の場合は有効になります。例:
select set_enable_parent('part_test', false);レンジパーティションテーブルの自動パーティション伝播を有効にします。
レンジパーティションテーブルの自動パーティション伝播を有効にできます。 挿入されたデータが既存のパーティションの範囲を超える場合、パーティションが自動的に作成されます。
set_auto (リレーションREGCLASS, value BOOLEAN) 自動パーティションの伝播を有効 /無効にします (RANGEパーティション分割のみ) 。 デフォルトで有効になっています。例:
postgres=# \d + part_test Table "public.part_test" 列 | タイプ | モディファイア | ストレージ | 統計ターゲット | 説明 ---------- ----------------------------- ------------------------------------------------------------------ id | integer | | plain | | info | text | | extended | | crt_time | タイムゾーンなしのタイムスタンプ | nullではない | プレーン | | 子テーブル: part_test_10、 part_test_11, part_test_12, part_test_13, part_test_14, part_test_15, part_test_16, part_test_17, part_test_18, part_test_19, part_test_20, part_test_21, part_test_22, part_test_23, part_test_24, part_test_25, part_test_26, part_test_3, part_test_4, part_test_5, part_test_6, part_test_7, part_test_8, part_test_9 postgres=# \d + part_test_26 Table "public.part_test_26" 列 | タイプ | モディファイア | ストレージ | 統計ターゲット | 説明 ---------- ----------------------------- ------------------------------------------------------------------ id | integer | | plain | | info | text | | extended | | crt_time | タイムゾーンなしのタイムスタンプ | nullではない | プレーン | | チェック制約: "pathman_part_test_26_3_check" CHECK (crt_time >= '2018-09-25 00:00:00 ': タイムスタンプなしタイムゾーンとcrt_time < '2018-10-25 00:00:00': タイムスタンプなしタイムゾーン) 継承: part_test postgres=# \d + part_test_25 Table "public.part_test_25" 列 | タイプ | モディファイア | ストレージ | 統計ターゲット | 説明 ---------- ----------------------------- ------------------------------------------------------------------ id | integer | | plain | | info | text | | extended | | crt_time | タイムゾーンなしのタイムスタンプ | nullではない | プレーン | | チェック制約: "pathman_part_test_25_3_check" CHECK (crt_time >= '2018-08-25 00:00:00 ': タイムスタンプなしタイムゾーンとcrt_time < '2018-09-25 00:00:00': タイムスタンプなしタイムゾーン) 継承: part_test 既存のパーティションの範囲外のデータを挿入すると、テーブルのパーティション分割時に指定された間隔に基づいて新しいパーティションが作成されます。 この操作は、完了するのに長い時間を必要とし得る。 postgres=# part_test値に挿入 (1、'test' 、'2222-01-01 '::timestamp); 多数のパーティションが作成された場合、挿入されるデータの範囲は広くなります。 postgres=# \d+ part_test Table "public.part_test" 列 | タイプ | モディファイア | ストレージ | 統計ターゲット | 説明 ---------- ----------------------------- ------------------------------------------------------------------ id | integer | | plain | | info | text | | extended | | crt_time | タイムゾーンなしのタイムスタンプ | nullではない | プレーン | | 子テーブル: part_test_10、 part_test_100, part_test_1000, part_test_1001, ..................................... 省略済み説明不適切な自動パーティション伝播が完了するまでに時間がかかる場合があるため、範囲パーティションテーブルの自動パーティション伝播を無効にすることをお勧めします。
コールバック関数を設定します。
コールバック関数は、パーティションが作成されるたびに自動的に呼び出されます。 たとえば、DDL論理レプリケーションのコールバック関数を設定して、ステートメントをテーブルに格納できます。 機能:
set_init_callback (リレーションREGCLASS、コールバックREGPROC DEFAULT 0) アタッチされたパーティションまたは作成されたパーティション (HASHとRANGEの両方) ごとに呼び出されるパーティション作成コールバックを設定します。 コールバックには次の署名が必要です。part_init_callback(args JSONB) RETURNS VOID。 パラメータargは、パーティションの種類に応じて存在する複数のフィールドで構成されます。/* RANGEパーティション分割テーブルabc (子abc_4) * / { "parent": "abc", "parttype": "2", "partition": "abc_4", "range_max": "401", "range_min": "301" } /* HASHパーティション分割テーブルabc (子abc_0) * / { "parent": "abc", "parttype": "1", "partition": "abc_0" }例:
コールバック関数 postgres=# createまたはreplace関数f_callback_test(jsonb) は、$$ 宣言 始める create table if not exists rec_part_ddl(id serial primary key, parent name, parttype int, partition name, range_max text, range_min text); if ($1->>'parttype')::int = 1 then raise notice 'parent: %, parttype: %, partition: %', $1->>'parent', $1->>'parttype', $1->>'partition'; insert into rec_part_ddl(parent, parttype, partition) values (($1->>'parent')::name, ($1->>'parttype')::int, ($1->>'partition')::name); elsif ($1->>'parttype')::int = 2 then raise notice 'parent: %, parttype: %, partition: %, range_max: %, range_min: %', $1->>'parent', $1->>'parttype', $1->>'partition', $1->>'range_max', $1->>'range_min'; insert into rec_part_ddl(parent, parttype, partition, range_max, range_min) values (($1->>'parent')::name, ($1->>'parttype')::int, ($1->>'partition')::name, $1->>'range_max', $1->>'range_min'); 終了if; 終了; $$ language plpgsql strict; テストテーブル postgres=# create table tt(id int, info text, crt_time timestamp not null); テーブルの作成 テストテーブルのコールバック関数を設定します。 select set_init_callback('tt'::regclass, 'f_callback_test '::regproc); パーティションを作成します。 postgres=# select create_range_partitions('tt'::regclass, -- 親テーブルのOID。 'crt_time', -- パーティションキー列の名前。 '2016-10-25 00:00:00 '::timestamp, -- 最初のパーティションの範囲の開始値。 interval '1 month', -- 各パーティションがカバーする値の範囲またはスパン。 24, -- The number of partitions. false) ; create_range_partitions ------------------------- 24 (1行) コールバック関数が呼び出されているかどうかを確認します。 postgres=# select * from rec_part_ddl; id | 親 | parttype | パーティション | range_max | range_min --- -------- -------------------------------- --------------------- --------------------- 1 | tt | 2 | tt_1 | 2016-11-25 00:00:00 | 2016-10-25 00:00:00 2 | tt | 2 | tt_2 | 2016-12-25 00:00:00 | 2016-11-25 00:00:00 3 | tt | 2 | tt_3 | 2017-01-25 00:00:00 | 2016-12-25 00:00:00 4 | tt | 2 | tt_4 | 2017-02-25 00:00:00 | 2017-01-25 00:00:00 5 | tt | 2 | tt_5 | 2017-03-25 00:00:00 | 2017-02-25 00:00:00 6 | tt | 2 | tt_6 | 2017-04-25 00:00:00 | 2017-03-25 00:00:00 7 | tt | 2 | tt_7 | 2017-05-25 00:00:00 | 2017-04-25 00:00:00 8 | tt | 2 | tt_8 | 2017-06-25 00:00:00 | 2017-05-25 00:00:00 9 | tt | 2 | tt_9 | 2017-07-25 00:00:00 | 2017-06-25 00:00:00 10 | tt | 2 | tt_10 | 2017-08-25 00:00:00 | 2017-07-25 00:00:00 11 | tt | 2 | tt_11 | 2017-09-25 00:00:00 | 2017-08-25 00:00:00 12 | tt | 2 | tt_12 | 2017-10-25 00:00:00 | 2017-09-25 00:00:00 13 | tt | 2 | tt_13 | 2017-11-25 00:00:00 | 2017-10-25 00:00:00 14 | tt | 2 | tt_14 | 2017-12-25 00:00:00 | 2017-11-25 00:00:00 15 | tt | 2 | tt_15 | 2018-01-25 00:00:00 | 2017-12-25 00:00:00 16 | tt | 2 | tt_16 | 2018-02-25 00:00:00 | 2018-01-25 00:00:00 17 | tt | 2 | tt_17 | 2018-03-25 00:00:00 | 2018-02-25 00:00:00 18 | tt | 2 | tt_18 | 2018-04-25 00:00:00 | 2018-03-25 00:00:00 19 | tt | 2 | tt_19 | 2018-05-25 00:00:00 | 2018-04-25 00:00:00 20 | tt | 2 | tt_20 | 2018-06-25 00:00:00 | 2018-05-25 00:00:00 21 | tt | 2 | tt_21 | 2018-07-25 00:00:00 | 2018-06-25 00:00:00 22 | tt | 2 | tt_22 | 2018-08-25 00:00:00 | 2018-07-25 00:00:00 23 | tt | 2 | tt_23 | 2018-09-25 00:00:00 | 2018-08-25 00:00:00 24 | tt | 2 | tt_24 | 2018-10-25 00:00:00 | 2018-09-25 00:00:00 (24行)