postgres=# CREATE SCHEMA IF NOT EXISTS partman; CREATE SCHEMA postgres=# create extension IF NOT EXISTS pg_partman SCHEMA partman; CREATE EXTENSION postgres=# select * from pg_available_extensions where name='pg_partman'; name | default_version | installed_version | comment ------------+-----------------+-------------------+------------------------------------------------------ pg_partman | 5.1.0 | 5.1.0 | Extension to manage partitioned tables by time or ID (1 行记录)
postgres=# \d+ public.t1 分区表 "public.t1" 栏位 | 类型 | 校对规则 | 可空的 | 预设 | 存储 | 压缩 | 统计目标 | 描述 ------+-----------------------------+----------+----------+--------------------------------+----------+------+----------+------ id | integer | | not null | nextval('t1_id_seq'::regclass) | plain | | | data | text | | | | extended | | | dt | timestamp without time zone | | not null | CURRENT_TIMESTAMP | plain | | | 分区键值: RANGE (dt) 索引: "t1_dt_idx" btree (dt) 分区: t1_p20240101 FOR VALUES FROM ('2024-01-01 00:00:00') TO ('2025-01-01 00:00:00'), t1_p20250101 FOR VALUES FROM ('2025-01-01 00:00:00') TO ('2026-01-01 00:00:00'), t1_p20260101 FOR VALUES FROM ('2026-01-01 00:00:00') TO ('2027-01-01 00:00:00'), t1_p20270101 FOR VALUES FROM ('2027-01-01 00:00:00') TO ('2028-01-01 00:00:00'), t1_p20280101 FOR VALUES FROM ('2028-01-01 00:00:00') TO ('2029-01-01 00:00:00'), t1_default DEFAULT
分区表信息:
1 2 3 4 5 6 7 8 9 10 11 12 13
postgres=# \d+ public.t1_p20240101 数据表 "public.t1_p20240101" 栏位 | 类型 | 校对规则 | 可空的 | 预设 | 存储 | 压缩 | 统计目标 | 描述 ------+-----------------------------+----------+----------+--------------------------------+----------+------+----------+------ id | integer | | not null | nextval('t1_id_seq'::regclass) | plain | | | data | text | | | | extended | | | dt | timestamp without time zone | | not null | CURRENT_TIMESTAMP | plain | | | 分区: t1 FOR VALUES FROM ('2024-01-01 00:00:00') TO ('2025-01-01 00:00:00') 分区约束: ((dt IS NOT NULL) AND (dt >= '2024-01-01 00:00:00'::timestamp without time zone) AND (dt < '2025-01-01 00:00:00'::timestamp without time zone)) 索引: "t1_p20240101_pkey" PRIMARY KEY, btree (id) "t1_p20240101_dt_idx" btree (dt) 访问方法 heap
postgres=# \d+ public.t2 分区表 "public.t2" 栏位 | 类型 | 校对规则 | 可空的 | 预设 | 存储 | 压缩 | 统计目标 | 描述 ------+-----------------------------+----------+----------+--------------------------------+----------+------+----------+------ id | integer | | not null | nextval('t2_id_seq'::regclass) | plain | | | data | text | | | | extended | | | dt | timestamp without time zone | | | CURRENT_TIMESTAMP | plain | | | 分区键值: RANGE (id) 索引: "t2_pkey" PRIMARY KEY, btree (id) 分区: t2_p0 FOR VALUES FROM (0) TO (10), t2_p10 FOR VALUES FROM (10) TO (20), t2_p20 FOR VALUES FROM (20) TO (30), t2_p30 FOR VALUES FROM (30) TO (40), t2_p40 FOR VALUES FROM (40) TO (50), t2_default DEFAULT
分区表信息:
1 2 3 4 5 6 7 8 9 10 11 12
postgres=# \d+ t2_p10 数据表 "public.t2_p10" 栏位 | 类型 | 校对规则 | 可空的 | 预设 | 存储 | 压缩 | 统计目标 | 描述 ------+-----------------------------+----------+----------+--------------------------------+----------+------+----------+------ id | integer | | not null | nextval('t2_id_seq'::regclass) | plain | | | data | text | | | | extended | | | dt | timestamp without time zone | | | CURRENT_TIMESTAMP | plain | | | 分区: t2 FOR VALUES FROM (10) TO (20) 分区约束: ((id IS NOT NULL) AND (id >= 10) AND (id < 20)) 索引: "t2_p10_pkey" PRIMARY KEY, btree (id) 访问方法 heap