过时的行不会从外部 table 分区 postgres 移出

Outdated row doesn't move out from foreign table partition postgres

我正在尝试了解如何在 Postgres 中配置分片。

我的 Postgres 设置有一个 temperature table,它有 4 个分区,每个分区涵盖不同范围的 "timestamp" 值。

postgres=# \d+ temperature
                                                     Partitioned table "public.temperature"
  Column   |            Type             | Collation | Nullable |                 Default                 | Storage | Stats target | Description 
-----------+-----------------------------+-----------+----------+-----------------------------------------+---------+--------------+-------------
 id        | bigint                      |           | not null | nextval('temperature_id_seq'::regclass) | plain   |              | 
 city_id   | integer                     |           | not null |                                         | plain   |              | 
 timestamp | timestamp without time zone |           | not null |                                         | plain   |              | 
 temp      | numeric(5,2)                |           | not null |                                         | main    |              | 
Partition key: RANGE ("timestamp")
Partitions: temperature_201901 FOR VALUES FROM ('2019-01-01 00:00:00') TO ('2019-02-01 00:00:00'),
            temperature_201902 FOR VALUES FROM ('2019-02-01 00:00:00') TO ('2019-03-01 00:00:00'),
            temperature_201903 FOR VALUES FROM ('2019-03-01 00:00:00') TO ('2019-04-01 00:00:00'),
            temperature_201904 FOR VALUES FROM ('2019-04-01 00:00:00') TO ('2019-05-01 00:00:00')

temperature_201904table,特别是外国table

postgres=# \d+ temperature_201904 
                                                           Foreign table "public.temperature_201904"
  Column   |            Type             | Collation | Nullable |                 Default                 | FDW options | Storage | Stats target | Description 
-----------+-----------------------------+-----------+----------+-----------------------------------------+-------------+---------+--------------+-------------
 id        | bigint                      |           | not null | nextval('temperature_id_seq'::regclass) |             | plain   |              | 
 city_id   | integer                     |           | not null |                                         |             | plain   |              | 
 timestamp | timestamp without time zone |           | not null |                                         |             | plain   |              | 
 temp      | numeric(5,2)                |           | not null |                                         |             | main    |              | 
Partition of: temperature FOR VALUES FROM ('2019-04-01 00:00:00') TO ('2019-05-01 00:00:00')
Partition constraint: (("timestamp" IS NOT NULL) AND ("timestamp" >= '2019-04-01 00:00:00'::timestamp without time zone) AND ("timestamp" < '2019-05-01 00:00:00'::timestamp without time zone))
Server: shard02

插入按预期工作。如果我插入以下值并从远程主机 shard02 检查,则该值存在。太棒了!

postgres=# select * from temperature_201904;
 id | city_id |      timestamp      | temp  
----+---------+---------------------+-------
  1 |       1 | 2019-04-02 00:00:00 | 12.30
(1 row)

但是,如果我更新此行的时间戳,使其在为分区定义的范围内不再有效,我希望它被移出并放入正确的分区中,temperature_201901 ,但事实并非如此。

postgres=# update temperature set timestamp =  '2019-01-04' where id=1;
UPDATE 1
postgres=# select * from temperature_201904 ;
 id | city_id |      timestamp      | temp  
----+---------+---------------------+-------
  1 |       1 | 2019-01-04 00:00:00 | 12.30

再次重申,这个 table 有一个范围 temperature_201904 FOR VALUES FROM ('2019-04-01 00:00:00') TO ('2019-05-01 00:00:00') 并且是一个外国 table.

感觉好像漏掉了什么。

这是预期的行为吗?如果是这样,是否有一种方法可以配置为随着分区约束的更改自动在节点之间移动数据?

提前致谢!

postgres=# SELECT version();
                                                     version                                                      
------------------------------------------------------------------------------------------------------------------
 PostgreSQL 12.2 (Debian 12.2-2.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit

这似乎是预料之中的。来自 the docs

While rows can be moved from local partitions to a foreign-table partition (provided the foreign data wrapper supports tuple routing), they cannot be moved from a foreign-table partition to another partition.

现在我本以为会出现错误,而不是默默地违反隐含的约束,但我不希望这会按照您想要的方式工作。