Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lVRXZ-0007ps-Py for pgsql-hackers@arkaria.postgresql.org; Sun, 11 Apr 2021 04:17:42 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lVRX1-0002nR-Hu for pgsql-hackers@arkaria.postgresql.org; Sun, 11 Apr 2021 04:17:07 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lVRX1-0002lU-6L for pgsql-hackers@lists.postgresql.org; Sun, 11 Apr 2021 04:17:07 +0000 Received: from mail-io1-xd33.google.com ([2607:f8b0:4864:20::d33]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lVRWx-0004sE-MN for pgsql-hackers@lists.postgresql.org; Sun, 11 Apr 2021 04:17:06 +0000 Received: by mail-io1-xd33.google.com with SMTP id d5so568040iof.3 for ; Sat, 10 Apr 2021 21:17:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telsasoft-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=4IgktPS97/0n6Gfc+Ipke1OcC7Yjy+NuSMNyt/7pOc0=; b=p8h90mJrz9a+vvlz92U/VYFOE4RuVqBaOioSmXQwkjU0WXDRcC5NwhmTeGkd22PnXN yN91Sf2ZNJ9FXdC81m8j/Jzr4XJAEfyCrCw7u89Yu1C5CmnjRR1vnb5cvAU8RD5s6Mh6 ZqK3lNi81siLF/sYQ1SmeZEa8yQvVpZEispoA3JPmXwiznja0/lX+uYtqNCpzT77jGJn AlxUrs6R32AyxHJWHmaJAhQXGd4abtBHsqifN4gJl/PAIucoNFFUI5PaD7wHKCaURMxt QPCAe5W7BCmLIqg1kkeSuYNwhWeT/IRS6XIP5EWjI24+oX5Z4/hZDEId674Gy+cfhh0j 9bhw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=4IgktPS97/0n6Gfc+Ipke1OcC7Yjy+NuSMNyt/7pOc0=; b=mbS4u3FW7GjWfa8ct5vdbr8HmNTUwKOtU3z5udGaQg9klA0WuS2rYa6q0pBhfstlIH 2J0OPrfPFc2I3fz349XEP+EiqO4K4/Lq+S09uMR+M9DNJ734VOUs3s3Xg5O5qYnKvu+q Nyq+BfA8Kdf23Gs72OE7YiwolZANh7EvyLl74oPjhWsS3T7gzD2DFZA2Fi/IOJN438ya pd8/MNyOWZ7byNjZS05OvBbDMzbHtVXt2WhQkl1R2HM69SOn9HN7BsbgDjgOtDSLAYwo 3Qrz8bT77EmJ4v0SvDT9ApFB00OJ7pIaygQoTdOTTYSYyUWzczxwCStK/1fmfV66H6qc 6Ebw== X-Gm-Message-State: AOAM53140N9jRVrufBDYFmGZABGK7XGzCn8idTErdr4XP8dKILpLTqWG 0Rv+bxOSIU8nbnafWRnhPq0YiQ== X-Google-Smtp-Source: ABdhPJwVSbCxsP/QZJK4A9J4TbS/UmhTC5PA99mms28Uzn+mzx756f+WDT2csgXUpzaYF38ZhC6Lgg== X-Received: by 2002:a5d:878e:: with SMTP id f14mr18160263ion.176.1618114620831; Sat, 10 Apr 2021 21:17:00 -0700 (PDT) Received: from pryzbyj.telsasoft (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id s14sm3554885ilp.35.2021.04.10.21.16.59 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 10 Apr 2021 21:17:00 -0700 (PDT) Received: by pryzbyj.telsasoft (Postfix, from userid 1000) id D4FB1800A3B; Sat, 10 Apr 2021 23:16:58 -0500 (CDT) Date: Sat, 10 Apr 2021 23:16:58 -0500 From: Justin Pryzby To: Fujii Masao Cc: Kohei KaiGai , Kazutaka Onishi , Bharath Rupireddy , Zhihong Yu , Amit Langote , Ibrar Ahmed , pgsql-hackers@lists.postgresql.org, Ashutosh Bapat Subject: Re: TRUNCATE on foreign table Message-ID: <20210411041658.GB14564@telsasoft.com> References: <66e78949-4747-941c-7966-60d9cd1aad99@oss.nttdata.com> <43f71643-5068-8a77-4bda-005b0ff7bd4f@oss.nttdata.com> <551ed8c1-f531-818b-664a-2cecdab99cd8@oss.nttdata.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="2oS5YaxWCcQjTEyO" Content-Disposition: inline In-Reply-To: <551ed8c1-f531-818b-664a-2cecdab99cd8@oss.nttdata.com> User-Agent: Mutt/1.9.4 (2018-02-28) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --2oS5YaxWCcQjTEyO Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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"); > +} --2oS5YaxWCcQjTEyO Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="0001-WIP-doc-review-Allow-TRUNCATE-command-to-truncate-fo.patch" From 7bbd9312464899dfc2c70fdc64c95a298ac01594 Mon Sep 17 00:00:00 2001 From: Justin Pryzby 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.sgml index 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); - Truncate a set of foreign tables specified in rels. + Truncate foreign tables. This function is called when is executed - on foreign tables. rels is the list of - Relation data structure that indicates - a foreign table to truncate. rels_extra the list of - int value, which delivers extra information about - a foreign table to truncate. Possible values are + on a foreign table. rels is a list of + Relation data structures for the + foreign tables to be truncated. rels_extra is a list of + corresponding int values which provide extra information about + the foreign tables. Each element of rels_extra may have the value TRUNCATE_REL_CONTEXT_NORMAL, which means that - the foreign table is specified WITHOUT ONLY clause + the foreign table is specified WITHOUT the ONLY clause in TRUNCATE, TRUNCATE_REL_CONTEXT_ONLY, which means that - the foreign table is specified WITH ONLY clause, - and TRUNCATE_REL_CONTEXT_CASCADING, + the foreign table is specified WITH the ONLY clause, + or TRUNCATE_REL_CONTEXT_CASCADING, 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 rels and - rels_extra. The number of entries is the same - between the two lists. + but will be truncated due to a dependency (for example, a table partition). - behavior defines how foreign tables should - be truncated, using as possible values DROP_RESTRICT, - which means that RESTRICT option is specified, - and DROP_CASCADE, which means that - CASCADE option is specified, in + behavior must be specified as + DROP_RESTRICT or DROP_CASCADE. + If specified as DROP_RESTRICT, the + RESTRICT option will be included in the TRUNCATE command. + If specified as DROP_CASCADE, the + CASCADE option will be included. - restart_seqs is set to true - if RESTART IDENTITY option is specified in - TRUNCATE command. It is false - if CONTINUE IDENTITY option is specified. + If restart_seqs is specified as true, + the TRUNCATE command will include the + RESTART IDENTITY option. + Otherwise, the TRUNCATE command will include the + CONTINUE IDENTITY option. diff --git a/doc/src/sgml/postgres-fdw.sgml b/doc/src/sgml/postgres-fdw.sgml index 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'); This option controls whether postgres_fdw allows - foreign tables to be truncated using TRUNCATE + foreign tables to be truncated using the TRUNCATE 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 true. diff --git a/doc/src/sgml/ref/truncate.sgml b/doc/src/sgml/ref/truncate.sgml index acf3633be4..9d846f88c9 100644 --- a/doc/src/sgml/ref/truncate.sgml +++ b/doc/src/sgml/ref/truncate.sgml @@ -173,7 +173,7 @@ TRUNCATE [ TABLE ] [ ONLY ] name [ TRUNCATE can be used for foreign tables if - the foreign data wrapper supports, for instance, + supported by the foreign data wrapper, for instance, see . -- 2.17.0 --2oS5YaxWCcQjTEyO Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="0002-Convert-an-if-else-if-without-an-else-to-a-switch.patch" From 513bb389e3e3c3cfe47518164c6ce49736854756 Mon Sep 17 00:00:00 2001 From: Justin Pryzby 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.c index 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 --2oS5YaxWCcQjTEyO Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="0003-Test-integer-using-and-not.patch" From 76d783f4e472ea466cca0d126798505f851e54d0 Mon Sep 17 00:00:00 2001 From: Justin Pryzby 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.c index 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 --2oS5YaxWCcQjTEyO--