From: Justin Pryzby <pryzby@telsasoft.com>
To: Fujii Masao <masao.fujii@oss.nttdata.com>
Cc: Kohei KaiGai <kaigai@heterodb.com>
Cc: Kazutaka Onishi <onishi@heterodb.com>
Cc: Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
Cc: Zhihong Yu <zyu@yugabyte.com>
Cc: Amit Langote <amitlangote09@gmail.com>
Cc: Ibrar Ahmed <ibrar.ahmad@gmail.com>
Cc: pgsql-hackers@lists.postgresql.org, Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>
Subject: Re: TRUNCATE on foreign table
Date: Sat, 10 Apr 2021 23:16:58 -0500
Message-ID: <20210411041658.GB14564@telsasoft.com> (raw)
In-Reply-To: <551ed8c1-f531-818b-664a-2cecdab99cd8@oss.nttdata.com>
References: <CAJuF6cMV8oBTR-1T=5d6g-XQM_mXu+kTwzbY+PeQx4mOr2xbRg@mail.gmail.com>
<e1d29b4c-0a3d-9e42-9575-a9373b593aa3@oss.nttdata.com>
<CAOP8fzbDMAS78vD=J183SRXzUJfJgPqudgab++FNRDAS3Eq93w@mail.gmail.com>
<66e78949-4747-941c-7966-60d9cd1aad99@oss.nttdata.com>
<CAOP8fzajfK4vUe0s0YAOehjaFxvcecdvv2JdzeR6mZ-V3fLVbw@mail.gmail.com>
<d5df2e94-f643-6409-5cd4-fa1c2ebd41fa@oss.nttdata.com>
<CAOP8fzbKh1V_n-huDmzxQRsMRZ5qNb+2wo5khKOrweGgx6pBrg@mail.gmail.com>
<43f71643-5068-8a77-4bda-005b0ff7bd4f@oss.nttdata.com>
<CAOP8fzYY=xkj_ke3h-cHO08S6WKfcja0uNPmGjNYo50rm66+6Q@mail.gmail.com>
<551ed8c1-f531-818b-664a-2cecdab99cd8@oss.nttdata.com>
On Thu, Apr 08, 2021 at 10:14:17PM +0900, Fujii Masao wrote:
> On 2021/04/08 22:02, Kohei KaiGai wrote:
> > > Anyway, attached is the updated version of the patch. This is still based on the latest Kazutaka-san's patch. That is, extra list for ONLY is still passed to FDW. What about committing this version at first? Then we can continue the discussion and change the behavior later if necessary.
>
> Pushed! Thank all involved in this development!!
> For record, I attached the final patch I committed.
Find attached language fixes.
I'm also proposing to convert an if/else to an switch(), since I don't like
"if/else if" without an "else", and since the compiler can warn about missing
enum values in switch cases. You could also write:
| Assert(behavior == DROP_RESTRICT || behavior == DROP_CASCADE)
Also, you currently test:
> + if (extra & TRUNCATE_REL_CONTEXT_ONLY)
but TRUNCATE_REL_ aren't indepedent bits, so shouldn't be tested with "&".
src/include/commands/tablecmds.h-#define TRUNCATE_REL_CONTEXT_NORMAL 1 /* specified without ONLY clause */
src/include/commands/tablecmds.h-#define TRUNCATE_REL_CONTEXT_ONLY 2 /* specified with ONLY clause */
src/include/commands/tablecmds.h:#define TRUNCATE_REL_CONTEXT_CASCADING 3 /* not specified but truncated
src/include/commands/tablecmds.h- * due to dependency (e.g.,
src/include/commands/tablecmds.h- * partition table) */
> +++ b/contrib/postgres_fdw/deparse.c
> @@ -2172,6 +2173,43 @@ deparseAnalyzeSql(StringInfo buf, Relation rel, List **retrieved_attrs)
> deparseRelation(buf, rel);
> }
>
> +/*
> + * Construct a simple "TRUNCATE rel" statement
> + */
> +void
> +deparseTruncateSql(StringInfo buf,
> + List *rels,
> + List *rels_extra,
> + DropBehavior behavior,
> + bool restart_seqs)
> +{
> + ListCell *lc1,
> + *lc2;
> +
> + appendStringInfoString(buf, "TRUNCATE ");
> +
> + forboth(lc1, rels, lc2, rels_extra)
> + {
> + Relation rel = lfirst(lc1);
> + int extra = lfirst_int(lc2);
> +
> + if (lc1 != list_head(rels))
> + appendStringInfoString(buf, ", ");
> + if (extra & TRUNCATE_REL_CONTEXT_ONLY)
> + appendStringInfoString(buf, "ONLY ");
> +
> + deparseRelation(buf, rel);
> + }
> +
> + appendStringInfo(buf, " %s IDENTITY",
> + restart_seqs ? "RESTART" : "CONTINUE");
> +
> + if (behavior == DROP_RESTRICT)
> + appendStringInfoString(buf, " RESTRICT");
> + else if (behavior == DROP_CASCADE)
> + appendStringInfoString(buf, " CASCADE");
> +}
Attachments:download all (3 files, .tar.gz)0001-WIP-doc-review-Allow-TRUNCATE-command-to-truncate-fo.patch (text/x-diff, 5.3K) download
From 7bbd9312464899dfc2c70fdc64c95a298ac01594 Mon Sep 17 00:00:00 2001
From: Justin Pryzby <pryzbyj@telsasoft.com>
Date: Thu, 8 Apr 2021 10:10:58 -0500
Subject: [PATCH 1/3] WIP doc review: Allow TRUNCATE command to truncate
foreign tables.
8ff1c94649f5c9184ac5f07981d8aea9dfd7ac19
---
doc/src/sgml/fdwhandler.sgml | 43 +++++++++++++++++-----------------
doc/src/sgml/postgres-fdw.sgml | 2 +-
doc/src/sgml/ref/truncate.sgml | 2 +-
3 files changed, 23 insertions(+), 24 deletions(-)
diff --git a/doc/src/sgml/fdwhandler.sgml b/doc/src/sgml/fdwhandler.sgmlindex 98882ddab8..5a76dede24 100644--- a/doc/src/sgml/fdwhandler.sgml+++ b/doc/src/sgml/fdwhandler.sgml@@ -1075,40 +1075,39 @@ ExecForeignTruncate(List *rels, List *rels_extra,
DropBehavior behavior, bool restart_seqs);
</programlisting>
- Truncate a set of foreign tables specified in <literal>rels</literal>.+ Truncate foreign tables.
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+ on a foreign table. <literal>rels</literal> is a list of+ <structname>Relation</structname> data structures for the+ foreign tables to be truncated. <literal>rels_extra</literal> is a list of+ corresponding <literal>int</literal> values which provide extra information about+ the foreign tables. Each element of rels_extra may have the value
<literal>TRUNCATE_REL_CONTEXT_NORMAL</literal>, which means that
- the foreign table is specified WITHOUT <literal>ONLY</literal> clause+ the foreign table is specified <emphasis>WITHOUT</emphasis> the <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>,+ the foreign table is specified <emphasis>WITH</emphasis> the <literal>ONLY</literal> clause,+ or <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.+ but will be truncated due to a dependency (for example, a table partition).
</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+ <literal>behavior</literal> must be specified as+ <literal>DROP_RESTRICT</literal> or <literal>DROP_CASCADE</literal>.+ If specified as <literal>DROP_RESTRICT</literal>, the+ <literal>RESTRICT</literal> option will be included in the
<command>TRUNCATE</command> command.
+ If specified as <literal>DROP_CASCADE</literal>, the+ <literal>CASCADE</literal> option will be included.
</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.+ If <literal>restart_seqs</literal> is specified as <literal>true</literal>,+ the <command>TRUNCATE</command> command will include the+ <literal>RESTART IDENTITY</literal> option.+ Otherwise, the <command>TRUNCATE</command> command will include the+ <literal>CONTINUE IDENTITY</literal> option.
</para>
<para>
diff --git a/doc/src/sgml/postgres-fdw.sgml b/doc/src/sgml/postgres-fdw.sgmlindex 5320accf6f..116434f658 100644--- a/doc/src/sgml/postgres-fdw.sgml+++ b/doc/src/sgml/postgres-fdw.sgml@@ -452,7 +452,7 @@ OPTIONS (ADD password_required 'false');
<listitem>
<para>
This option controls whether <filename>postgres_fdw</filename> allows
- foreign tables to be truncated using <command>TRUNCATE</command>+ foreign tables to be truncated using the <command>TRUNCATE</command>
command. It can be specified for a foreign table or a foreign server.
A table-level option overrides a server-level option.
The default is <literal>true</literal>.
diff --git a/doc/src/sgml/ref/truncate.sgml b/doc/src/sgml/ref/truncate.sgmlindex acf3633be4..9d846f88c9 100644--- a/doc/src/sgml/ref/truncate.sgml+++ b/doc/src/sgml/ref/truncate.sgml@@ -173,7 +173,7 @@ TRUNCATE [ TABLE ] [ ONLY ] <replaceable class="parameter">name</replaceable> [
<para>
<command>TRUNCATE</command> can be used for foreign tables if
- the foreign data wrapper supports, for instance,+ supported by the foreign data wrapper, for instance,
see <xref linkend="postgres-fdw"/>.
</para>
</refsect1>
--
2.17.0
0002-Convert-an-if-else-if-without-an-else-to-a-switch.patch (text/x-diff, 929B) download
From 513bb389e3e3c3cfe47518164c6ce49736854756 Mon Sep 17 00:00:00 2001
From: Justin Pryzby <pryzbyj@telsasoft.com>
Date: Sat, 10 Apr 2021 22:53:21 -0500
Subject: [PATCH 2/3] Convert an if/else if without an else to a switch
---
contrib/postgres_fdw/deparse.c | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)
diff --git a/contrib/postgres_fdw/deparse.c b/contrib/postgres_fdw/deparse.cindex bdc4c3620d..2c702c0e37 100644--- a/contrib/postgres_fdw/deparse.c+++ b/contrib/postgres_fdw/deparse.c@@ -2204,10 +2204,15 @@ deparseTruncateSql(StringInfo buf,
appendStringInfo(buf, " %s IDENTITY",
restart_seqs ? "RESTART" : "CONTINUE");
- if (behavior == DROP_RESTRICT)+ switch (behavior)+ {+ case DROP_RESTRICT:
appendStringInfoString(buf, " RESTRICT");
- else if (behavior == DROP_CASCADE)+ break;+ case DROP_CASCADE:
appendStringInfoString(buf, " CASCADE");
+ break;+ }
}
/*
--
2.17.0
0003-Test-integer-using-and-not.patch (text/x-diff, 783B) download
From 76d783f4e472ea466cca0d126798505f851e54d0 Mon Sep 17 00:00:00 2001
From: Justin Pryzby <pryzbyj@telsasoft.com>
Date: Sat, 10 Apr 2021 23:08:58 -0500
Subject: [PATCH 3/3] Test integer using == and not &
---
contrib/postgres_fdw/deparse.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/contrib/postgres_fdw/deparse.c b/contrib/postgres_fdw/deparse.cindex 2c702c0e37..c2b1269f73 100644--- a/contrib/postgres_fdw/deparse.c+++ b/contrib/postgres_fdw/deparse.c@@ -2195,7 +2195,7 @@ deparseTruncateSql(StringInfo buf,
if (lc1 != list_head(rels))
appendStringInfoString(buf, ", ");
- if (extra & TRUNCATE_REL_CONTEXT_ONLY)+ if (extra == TRUNCATE_REL_CONTEXT_ONLY)
appendStringInfoString(buf, "ONLY ");
deparseRelation(buf, rel);
--
2.17.0
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-hackers@postgresql.org
Cc: pryzby@telsasoft.com, masao.fujii@oss.nttdata.com, kaigai@heterodb.com, onishi@heterodb.com, bharath.rupireddyforpostgres@gmail.com, zyu@yugabyte.com, amitlangote09@gmail.com, ibrar.ahmad@gmail.com, ashutosh.bapat.oss@gmail.com
Subject: Re: TRUNCATE on foreign table
In-Reply-To: <20210411041658.GB14564@telsasoft.com>
References: <CAJuF6cMV8oBTR-1T=5d6g-XQM_mXu+kTwzbY+PeQx4mOr2xbRg@mail.gmail.com> <e1d29b4c-0a3d-9e42-9575-a9373b593aa3@oss.nttdata.com> <CAOP8fzbDMAS78vD=J183SRXzUJfJgPqudgab++FNRDAS3Eq93w@mail.gmail.com> <66e78949-4747-941c-7966-60d9cd1aad99@oss.nttdata.com> <CAOP8fzajfK4vUe0s0YAOehjaFxvcecdvv2JdzeR6mZ-V3fLVbw@mail.gmail.com> <d5df2e94-f643-6409-5cd4-fa1c2ebd41fa@oss.nttdata.com> <CAOP8fzbKh1V_n-huDmzxQRsMRZ5qNb+2wo5khKOrweGgx6pBrg@mail.gmail.com> <43f71643-5068-8a77-4bda-005b0ff7bd4f@oss.nttdata.com> <CAOP8fzYY=xkj_ke3h-cHO08S6WKfcja0uNPmGjNYo50rm66+6Q@mail.gmail.com> <551ed8c1-f531-818b-664a-2cecdab99cd8@oss.nttdata.com> <20210411041658.GB14564@telsasoft.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox