Re: TRUNCATE on foreign table

Justin Pryzby <pryzby@telsasoft.com>

From: Justin Pryzby <pryzby@telsasoft.com>
To: Fujii Masao <masao.fujii@oss.nttdata.com>
Cc: Kohei KaiGai <kaigai@heterodb.com>, Kazutaka Onishi <onishi@heterodb.com>, Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>, Zhihong Yu <zyu@yugabyte.com>, Amit Langote <amitlangote09@gmail.com>, Ibrar Ahmed <ibrar.ahmad@gmail.com>, pgsql-hackers@lists.postgresql.org, Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>
Date: 2021-04-11T04:16:58Z
Lists: pgsql-hackers

Attachments

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");
> +}

Commits

  1. doc: Review for "Allow TRUNCATE command to truncate foreign tables".

  2. Don't pass "ONLY" options specified in TRUNCATE to foreign data wrapper.

  3. Support tab-complete for TRUNCATE on foreign tables.

  4. Allow TRUNCATE command to truncate foreign tables.

  5. Add support for asynchronous execution.