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 1lW7N2-0004US-NV for pgsql-hackers@arkaria.postgresql.org; Tue, 13 Apr 2021 00:57:37 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lW7N0-0007Jr-NU for pgsql-hackers@arkaria.postgresql.org; Tue, 13 Apr 2021 00:57:34 +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 1lW7N0-0007Ji-A1 for pgsql-hackers@lists.postgresql.org; Tue, 13 Apr 2021 00:57:34 +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 1lW7Mw-0006N1-OK for pgsql-hackers@lists.postgresql.org; Tue, 13 Apr 2021 00:57:33 +0000 Received: by mail-io1-xd33.google.com with SMTP id a11so13294525ioo.0 for ; Mon, 12 Apr 2021 17:57:30 -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=+/W7RFvAWAbhV4tXJU08h5AfoCOyfNbZzP/6yGJMWJs=; b=fAycXFIx/gZ3T+vhE+JOCJ4ePW3i87ChjmYHlGwUi4wnGktPgLZH1i3lDz+eevPLXe Zj3CK1FFnm5c3gnuuUJ8Avmm3XbYRGSMn4YUe7Zta360d0ZDv1pD3qawdXjfqbl581/p OucMm9Bd1ePc1QOEb0/wptLtzUpATP9semhO9jhnMolFl8gi8VnHDUBzwsRXILDbSCZK EAeUkl1/4S/G4iCsidvY3n5hew9P5v73d4jBH14Vz16DKbonl1p4ktxcn5kBPIwlbntf ApnzbTEF28Y0EnZmQZ6dwCTcnolvv6MxjAiBwJ91TomHsShwQTCcndzBubGQwQyIV7rV Ldew== 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=+/W7RFvAWAbhV4tXJU08h5AfoCOyfNbZzP/6yGJMWJs=; b=i9d071ojRpOB6ArFLUtabTLkJRyZixDh4rPD+RPe6Ad0Sr73m0/brMTeXjF9XUkVUv DmAe9IL3a9RAMM17CnluunP0S35zTCknM12NEkKOu1cctTGsGntPWLyiO6obh1+4DSay d4R4D82zyJCX2Mu03v9P84EvACuOasH8VIGPmDGru552hKKPh2rFNcpxjztH/XbG/CDE trxzkpf1mnbtmY7SBkPbwy6vGWvNSWZhzbZoyFb/eEnrLbga4Ui8fXxZ0eK2M30gIL1k wLAAlgtGPx6U2rRc2HQrtUenOjKRpe3daHKPjfLa6r8bwNc6YAIwfDQ9vnjT8IUwv1uJ f4pg== X-Gm-Message-State: AOAM531GiRJj2HW5c7WXiKZA4BewBJGFnCx0L1FHjxQuZ7paxUf+W2SV k06w4tWVQjTrJRwxR4UmTadVig== X-Google-Smtp-Source: ABdhPJxHfjZDJ6R8wYbXcCuhZas7i968Tu+FdNTw4BGpgOZt8ujIs3p9DAMF6RrFfE/io1aG1FC8Ag== X-Received: by 2002:a05:6638:1211:: with SMTP id n17mr10456736jas.26.1618275448454; Mon, 12 Apr 2021 17:57:28 -0700 (PDT) Received: from pryzbyj.telsasoft (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id g12sm5983864ile.71.2021.04.12.17.57.27 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 12 Apr 2021 17:57:27 -0700 (PDT) Received: by pryzbyj.telsasoft (Postfix, from userid 1000) id 78094801860; Mon, 12 Apr 2021 19:57:26 -0500 (CDT) Date: Mon, 12 Apr 2021 19:57:26 -0500 From: Justin Pryzby To: Bharath Rupireddy Cc: Fujii Masao , Kohei KaiGai , Kazutaka Onishi , Zhihong Yu , Amit Langote , Ibrar Ahmed , pgsql-hackers@lists.postgresql.org, Ashutosh Bapat Subject: Re: TRUNCATE on foreign table Message-ID: <20210413005726.GK20530@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> <20210411041658.GB14564@telsasoft.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="5p8PegU4iirBW1oA" Content-Disposition: inline In-Reply-To: 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 --5p8PegU4iirBW1oA Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Sun, Apr 11, 2021 at 03:45:36PM +0530, Bharath Rupireddy wrote: > On Sun, Apr 11, 2021 at 9:47 AM Justin Pryzby wrote: > > Also, you currently test: > > > + if (extra & TRUNCATE_REL_CONTEXT_ONLY) > > > > but TRUNCATE_REL_ aren't indepedent bits, so shouldn't be tested with "&". > > Yeah this is an issue. We could just change the #defines to values > 0x0001, 0x0002, 0x0004, 0x0008 ... 0x0020 and so on and then testing > with & would work. So, this way, more than option can be multiplexed > into the same int value. To multiplex, we need to think: will there be > a scenario where a single rel in the truncate can have multiple > options at a time and do we want to distinguish these options while > deparsing? > > #define TRUNCATE_REL_CONTEXT_NORMAL 0x0001 /* specified without > ONLY clause */ > #define TRUNCATE_REL_CONTEXT_ONLY 0x0002 /* specified with > ONLY clause */ > #define TRUNCATE_REL_CONTEXT_CASCADING 0x0004 /* not specified > but truncated > > And I'm not sure what's the agreement on retaining or removing #define > values, currently I see only TRUNCATE_REL_CONTEXT_ONLY is being used, > others are just being set but not used. As I said upthread, it will be > good to remove the unused macros/enums, retain only the ones that are > used, especially TRUNCATE_REL_CONTEXT_CASCADING this is not required I > feel, because we can add the child partitions that are foreign tables > to relids as just normal foreign tables with TRUNCATE_REL_CONTEXT_ONLY > option. Converting to "bits" would collapse TRUNCATE_REL_CONTEXT_ONLY and TRUNCATE_REL_CONTEXT_NORMAL into a single bit. TRUNCATE_REL_CONTEXT_CASCADING could optionally be removed. +1 to convert to bits instead of changing "&" to "==". -- Justin --5p8PegU4iirBW1oA Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="0001-Convert-TRUNCATE_REL_CONTEXT_-to-bits.patch" From 26e1015de344352c4cd73deda28ad59594914067 Mon Sep 17 00:00:00 2001 From: Justin Pryzby Date: Mon, 12 Apr 2021 19:07:34 -0500 Subject: [PATCH 1/2] Convert TRUNCATE_REL_CONTEXT_* to bits The values were defined as consecutive integers, but TRUNCATE_REL_CONTEXT_ONLY was tested with bitwise test. If specified as bits, NORMAL vs ONLY is redundant (there's only one bit). And CASCADING was unused. See also: 8ff1c94649f5c9184ac5f07981d8aea9dfd7ac19 --- src/backend/commands/tablecmds.c | 9 +++------ src/backend/replication/logical/worker.c | 4 ++-- src/include/commands/tablecmds.h | 6 +----- 3 files changed, 6 insertions(+), 13 deletions(-) diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 096a6f2891..e8215d8dc8 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -1654,8 +1654,7 @@ ExecuteTruncate(TruncateStmt *stmt) rels = lappend(rels, rel); relids = lappend_oid(relids, myrelid); - relids_extra = lappend_int(relids_extra, (recurse ? - TRUNCATE_REL_CONTEXT_NORMAL : + relids_extra = lappend_int(relids_extra, (recurse ? 0 : TRUNCATE_REL_CONTEXT_ONLY)); /* Log this relation only if needed for logical decoding */ if (RelationIsLogicallyLogged(rel)) @@ -1704,8 +1703,7 @@ ExecuteTruncate(TruncateStmt *stmt) rels = lappend(rels, rel); relids = lappend_oid(relids, childrelid); - relids_extra = lappend_int(relids_extra, - TRUNCATE_REL_CONTEXT_CASCADING); + relids_extra = lappend_int(relids_extra, 0); /* Log this relation only if needed for logical decoding */ if (RelationIsLogicallyLogged(rel)) relids_logged = lappend_oid(relids_logged, childrelid); @@ -1799,8 +1797,7 @@ ExecuteTruncateGuts(List *explicit_rels, truncate_check_activity(rel); rels = lappend(rels, rel); relids = lappend_oid(relids, relid); - relids_extra = lappend_int(relids_extra, - TRUNCATE_REL_CONTEXT_CASCADING); + relids_extra = lappend_int(relids_extra, 0); /* Log this relation only if needed for logical decoding */ if (RelationIsLogicallyLogged(rel)) relids_logged = lappend_oid(relids_logged, relid); diff --git a/src/backend/replication/logical/worker.c b/src/backend/replication/logical/worker.c index fb3ba5c415..986b634525 100644 --- a/src/backend/replication/logical/worker.c +++ b/src/backend/replication/logical/worker.c @@ -1825,7 +1825,7 @@ apply_handle_truncate(StringInfo s) remote_rels = lappend(remote_rels, rel); rels = lappend(rels, rel->localrel); relids = lappend_oid(relids, rel->localreloid); - relids_extra = lappend_int(relids_extra, TRUNCATE_REL_CONTEXT_NORMAL); + relids_extra = lappend_int(relids_extra, 0); if (RelationIsLogicallyLogged(rel->localrel)) relids_logged = lappend_oid(relids_logged, rel->localreloid); @@ -1864,7 +1864,7 @@ apply_handle_truncate(StringInfo s) rels = lappend(rels, childrel); part_rels = lappend(part_rels, childrel); relids = lappend_oid(relids, childrelid); - relids_extra = lappend_int(relids_extra, TRUNCATE_REL_CONTEXT_CASCADING); + relids_extra = lappend_int(relids_extra, 0); /* Log this relation only if needed for logical decoding */ if (RelationIsLogicallyLogged(childrel)) relids_logged = lappend_oid(relids_logged, childrelid); diff --git a/src/include/commands/tablecmds.h b/src/include/commands/tablecmds.h index b808a07e46..9aa9f3c6c7 100644 --- a/src/include/commands/tablecmds.h +++ b/src/include/commands/tablecmds.h @@ -25,11 +25,7 @@ * These values indicate how a relation was specified as the target to * truncate in TRUNCATE command. */ -#define TRUNCATE_REL_CONTEXT_NORMAL 1 /* specified without ONLY clause */ -#define TRUNCATE_REL_CONTEXT_ONLY 2 /* specified with ONLY clause */ -#define TRUNCATE_REL_CONTEXT_CASCADING 3 /* not specified but truncated - * due to dependency (e.g., - * partition table) */ +#define TRUNCATE_REL_CONTEXT_ONLY 0x01 /* specified with ONLY clause */ struct AlterTableUtilityContext; /* avoid including tcop/utility.h here */ -- 2.17.0 --5p8PegU4iirBW1oA Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="0002-WIP-doc-review-Allow-TRUNCATE-command-to-truncate-fo.patch" From 8f332a4c9a8690ade17421528b1d375ddd992cce Mon Sep 17 00:00:00 2001 From: Justin Pryzby Date: Thu, 8 Apr 2021 10:10:58 -0500 Subject: [PATCH 2/2] WIP doc review: Allow TRUNCATE command to truncate foreign tables. 8ff1c94649f5c9184ac5f07981d8aea9dfd7ac19 --- doc/src/sgml/fdwhandler.sgml | 55 ++++++++++++++-------------------- doc/src/sgml/postgres-fdw.sgml | 2 +- doc/src/sgml/ref/truncate.sgml | 2 +- 3 files changed, 25 insertions(+), 34 deletions(-) diff --git a/doc/src/sgml/fdwhandler.sgml b/doc/src/sgml/fdwhandler.sgml index 98882ddab8..69feefe66b 100644 --- a/doc/src/sgml/fdwhandler.sgml +++ b/doc/src/sgml/fdwhandler.sgml @@ -1075,46 +1075,37 @@ 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 - TRUNCATE_REL_CONTEXT_NORMAL, which means that - the foreign table is specified WITHOUT ONLY clause - in TRUNCATE, - TRUNCATE_REL_CONTEXT_ONLY, which means that - the foreign table is specified WITH ONLY clause, - and 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. - - - - 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 + on a foreign table. rels is a list of + Relation data structures for each + foreign tables to be truncated. + rels_extra is a list of int values + of the same length as rels. Each element of + rels_extra is a bitmask of flags and provides extra + information about the corresponding foreign table. Currently, the only + defined flag is TRUNCATE_REL_CONTEXT_ONLY, which means + that the foreign table was specified with the ONLY + clause in the original TRUNCATE command. + + + + behavior is either + DROP_RESTRICT or DROP_CASCADE. + DROP_CASCADE indicates that the + CASCADE option was specified in the original TRUNCATE command. - 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 true, + the original TRUNCATE command included the + RESTART IDENTITY option. - TRUNCATE invokes - ExecForeignTruncate once per foreign server - that foreign tables to truncate belong to. This means that all foreign + ExecForeignTruncate is invoked once per foreign server + for which foreign tables are to be truncated. This means that all foreign tables included in rels must belong to the same server. 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 --5p8PegU4iirBW1oA--