是否可以使用 postgres/psql COPY 到 table 的特定分区?
Is it possible to use postgres/psql COPY into a specific partition of a table?
我目前正在研究一种高效 方法来将数据分配到分区table。是否可以使用 postgres/psql 将数据复制到特定的 table 分区(而不是使用 INSERT)?
根据 COPY here 上的文档:
COPY FROM can be used with plain, foreign, or partitioned tables or with views that have INSTEAD OF INSERT triggers.
根据分区文档here:
Be aware that COPY ignores rules. If you want to use COPY to insert data, you'll need to copy into the correct partition table rather than into the master. COPY does fire triggers, so you can use it normally if you use the trigger approach.
根据我对上述资源的了解,似乎可以复制到分区中;但是,我无法在网上找到任何示例或支持。
换句话说,我可以这样写吗:
COPY some_table_partition_one FROM '/some_dir/some_file'
COPY
到分区 table 是 introduced in v11:
Allow INSERT
, UPDATE
, and COPY
on partitioned tables to properly route rows to foreign partitions (Etsuro Fujita, Amit Langote)
但是 COPY
自 v10 以来的所有版本都可以直接分区,其中引入了声明性分区。
我们好像忘了从文档中删除第二个引文。
至少 PG 12.2 是可能的:
CREATE TABLE measurement (
city_id int not null,
logdate date not null,
peaktemp int,
unitsales int
) PARTITION BY RANGE (logdate);
CREATE TABLE
CREATE TABLE measurement_y2020m03 PARTITION OF measurement
FOR VALUES FROM ('2020-03-01') TO ('2020-03-31');
CREATE TABLE
CREATE TABLE measurement_y2020m04 PARTITION OF measurement
FOR VALUES FROM ('2020-04-01') TO ('2020-04-30');
CREATE TABLE
insert into measurement values (1, current_date, 10,100);
INSERT 0 1
select * from measurement;
city_id | logdate | peaktemp | unitsales
---------+------------+----------+-----------
1 | 2020-03-27 | 10 | 100
(1 row)
cat /tmp/m.dat
4,2020-04-01,40,400
copy measurement_y2020m04 from '/tmp/m.dat' delimiter ',';
COPY 1
select * from measurement;
city_id | logdate | peaktemp | unitsales
---------+------------+----------+-----------
1 | 2020-03-27 | 10 | 100
4 | 2020-04-01 | 40 | 400
(2 rows)
我目前正在研究一种高效 方法来将数据分配到分区table。是否可以使用 postgres/psql 将数据复制到特定的 table 分区(而不是使用 INSERT)?
根据 COPY here 上的文档:
COPY FROM can be used with plain, foreign, or partitioned tables or with views that have INSTEAD OF INSERT triggers.
根据分区文档here:
Be aware that COPY ignores rules. If you want to use COPY to insert data, you'll need to copy into the correct partition table rather than into the master. COPY does fire triggers, so you can use it normally if you use the trigger approach.
根据我对上述资源的了解,似乎可以复制到分区中;但是,我无法在网上找到任何示例或支持。
换句话说,我可以这样写吗:
COPY some_table_partition_one FROM '/some_dir/some_file'
COPY
到分区 table 是 introduced in v11:
Allow
INSERT
,UPDATE
, andCOPY
on partitioned tables to properly route rows to foreign partitions (Etsuro Fujita, Amit Langote)
但是 COPY
自 v10 以来的所有版本都可以直接分区,其中引入了声明性分区。
我们好像忘了从文档中删除第二个引文。
至少 PG 12.2 是可能的:
CREATE TABLE measurement (
city_id int not null,
logdate date not null,
peaktemp int,
unitsales int
) PARTITION BY RANGE (logdate);
CREATE TABLE
CREATE TABLE measurement_y2020m03 PARTITION OF measurement
FOR VALUES FROM ('2020-03-01') TO ('2020-03-31');
CREATE TABLE
CREATE TABLE measurement_y2020m04 PARTITION OF measurement
FOR VALUES FROM ('2020-04-01') TO ('2020-04-30');
CREATE TABLE
insert into measurement values (1, current_date, 10,100);
INSERT 0 1
select * from measurement;
city_id | logdate | peaktemp | unitsales
---------+------------+----------+-----------
1 | 2020-03-27 | 10 | 100
(1 row)
cat /tmp/m.dat
4,2020-04-01,40,400
copy measurement_y2020m04 from '/tmp/m.dat' delimiter ',';
COPY 1
select * from measurement;
city_id | logdate | peaktemp | unitsales
---------+------------+----------+-----------
1 | 2020-03-27 | 10 | 100
4 | 2020-04-01 | 40 | 400
(2 rows)