Skip to content

Latest commit

 

History

History
142 lines (111 loc) · 6.69 KB

20210409_02.md

File metadata and controls

142 lines (111 loc) · 6.69 KB

PostgreSQL 14 preview - FDW 外部表接口支持 truncate [only|cascade] , postgres_fdw已实现, 基于FDW的sharding 功能更进一步

作者

digoal

日期

2021-04-09

标签

PostgreSQL , sharding , fdw , postgres_fdw , truncate , only , cascade


背景

PostgreSQL 14 FDW 功能增强, 支持truncate 语法, truncate 外部表, 同时如果使用了truncate only, cascade也会透出到外部表远端.

《怎么看待PostgreSQL sharding插件: citus - 对比PG 社区基于 postgres_fdw 的sharding》

https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=8ff1c94649f5c9184ac5f07981d8aea9dfd7ac19

Allow TRUNCATE command to truncate foreign tables.  
author	Fujii Masao <fujii@postgresql.org>	  
Thu, 8 Apr 2021 11:56:08 +0000 (20:56 +0900)  
committer	Fujii Masao <fujii@postgresql.org>	  
Thu, 8 Apr 2021 11:56:08 +0000 (20:56 +0900)  
commit	8ff1c94649f5c9184ac5f07981d8aea9dfd7ac19  
tree	351ccf9b3a7e2b3256684ba7f73a80b3eac220b1	tree  
parent	50e17ad281b8d1c1b410c9833955bc80fbad4078	commit | diff  
Allow TRUNCATE command to truncate foreign tables.  
  
This commit introduces new foreign data wrapper API for TRUNCATE.  
It extends TRUNCATE command so that it accepts foreign tables as  
the targets to truncate and invokes that API. Also it extends postgres_fdw  
so that it can issue TRUNCATE command to foreign servers, by adding  
new routine for that TRUNCATE API.  
  
The information about options specified in TRUNCATE command, e.g.,  
ONLY, CACADE, etc is passed to FDW via API. The list of foreign tables to  
truncate is also passed to FDW. FDW truncates the foreign data sources  
that the passed foreign tables specify, based on those information.  
For example, postgres_fdw constructs TRUNCATE command using them  
and issues it to the foreign server.  
  
For performance, TRUNCATE command invokes the FDW routine for  
TRUNCATE once per foreign server that foreign tables to truncate belong to.  
  
Author: Kazutaka Onishi, Kohei KaiGai, slightly modified by Fujii Masao  
Reviewed-by: Bharath Rupireddy, Michael Paquier, Zhihong Yu, Alvaro Herrera, Stephen Frost, Ashutosh Bapat, Amit Langote, Daniel Gustafsson, Ibrar Ahmed, Fujii Masao  
Discussion: https://postgr.es/m/CAOP8fzb_gkReLput7OvOK+8NHgw-RKqNv59vem7=524krQTcWA@mail.gmail.com  
Discussion: https://postgr.es/m/CAJuF6cMWDDqU-vn_knZgma+2GMaout68YUgn1uyDnexRhqqM5Q@mail.gmail.com  
+   <sect2 id="fdw-callbacks-truncate">
+    <title>FDW Routines for <command>TRUNCATE</command></title>
+
+    <para>
+<programlisting>
+void
+ExecForeignTruncate(List *rels, List *rels_extra,
+                    DropBehavior behavior, bool restart_seqs);
+</programlisting>
+
+     Truncate a set of foreign tables specified in <literal>rels</literal>.
+     This function is called when <xref linkend="sql-truncate"/> is executed
+     on foreign tables.  <literal>rels</literal> is the list of
+     <structname>Relation</structname> data structure that indicates
+     a foreign table to truncate.  <literal>rels_extra</literal> the list of
+     <literal>int</literal> value, which delivers extra information about
+     a foreign table to truncate.  Possible values are
+     <literal>TRUNCATE_REL_CONTEXT_NORMAL</literal>, which means that
+     the foreign table is specified WITHOUT <literal>ONLY</literal> clause
+     in <command>TRUNCATE</command>,
+     <literal>TRUNCATE_REL_CONTEXT_ONLY</literal>, which means that
+     the foreign table is specified WITH <literal>ONLY</literal> clause,
+     and <literal>TRUNCATE_REL_CONTEXT_CASCADING</literal>,
+     which means that the foreign table is not specified explicitly,
+     but will be truncated due to dependency (for example, partition table).
+     There is one-to-one mapping between <literal>rels</literal> and
+     <literal>rels_extra</literal>.  The number of entries is the same
+     between the two lists.
+    </para>
+
+    <para>
+     <literal>behavior</literal> defines how foreign tables should
+     be truncated, using as possible values <literal>DROP_RESTRICT</literal>,
+     which means that <literal>RESTRICT</literal> option is specified,
+     and <literal>DROP_CASCADE</literal>, which means that
+     <literal>CASCADE</literal> option is specified, in
+     <command>TRUNCATE</command> command.
+    </para>
+    
+    <para>
+     <literal>restart_seqs</literal> is set to <literal>true</literal>
+     if <literal>RESTART IDENTITY</literal> option is specified in
+     <command>TRUNCATE</command> command.  It is <literal>false</literal>
+     if <literal>CONTINUE IDENTITY</literal> option is specified.
+    </para>
+
+    <para>
+     <command>TRUNCATE</command> invokes
+     <function>ExecForeignTruncate</function> once per foreign server
+     that foreign tables to truncate belong to.  This means that all foreign
+     tables included in <literal>rels</literal> must belong to the same
+     server.
+    </para>
+
+    <para>
+     If the <function>ExecForeignTruncate</function> pointer is set to
+     <literal>NULL</literal>, attempts to truncate foreign tables will
+     fail with an error message.
+    </para>
+   </sect2>

您的愿望将传达给PG kernel hacker、数据库厂商等, 帮助提高数据库产品质量和功能, 说不定下一个PG版本就有您提出的功能点. 针对非常好的提议,奖励限量版PG文化衫、纪念品、贴纸、PG热门书籍等,奖品丰富,快来许愿。开不开森.

digoal's wechat