Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f34EM-0001ZL-KK for pgsql-hackers@arkaria.postgresql.org; Mon, 02 Apr 2018 18:30:58 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1f34EL-0002Iy-Bt for pgsql-hackers@arkaria.postgresql.org; Mon, 02 Apr 2018 18:30:57 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f34EL-0002HO-4j for pgsql-hackers@lists.postgresql.org; Mon, 02 Apr 2018 18:30:57 +0000 Received: from out5-smtp.messagingengine.com ([66.111.4.29]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f34EH-0006vZ-8E for pgsql-hackers@postgresql.org; Mon, 02 Apr 2018 18:30:56 +0000 Received: from compute7.internal (compute7.nyi.internal [10.202.2.47]) by mailout.nyi.internal (Postfix) with ESMTP id 8D75120B58; Mon, 2 Apr 2018 14:30:51 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute7.internal (MEProxy); Mon, 02 Apr 2018 14:30:51 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; bh=RMJMNx bWft2Zz6PJuTygk/DUeiePFddnDwNHm/ZYbv8=; b=FgwMWU4UmzzyrB8rl0cCCS lQ3aYk6NBS8E0o+jySzHNTOSNxDmkEvnQSP5j+bZGIjxFcj4qVrIFTdHJCTks8j2 I+NBxwQniHMUbfBeK9TVTXzoUslxM3ZEQu5FbSTDWhAvWy9uTHIUE39f4iWdq/qs MflSL6ldmpcCBWGMG3u2poUsv/LLcPm5m8P6KquvtBC+up3UsgNyduiNtkeBD1F3 UwKJhe4WOPecuL6Rwm4zU7tHRqQl8HmxIcKu4l6xliO5TH8dYbASX/eQ2hW1YhBj 3Wz1kD0Z11FeGNIhw6j845dHfO3gRWqZzYEwp2efaCmgxDaN4z6UZzIn1vdWggWA == X-ME-Sender: Received: from april.local (c-73-13-66-39.hsd1.pa.comcast.net [73.13.66.39]) by mail.messagingengine.com (Postfix) with ESMTPA id 46000E50B3; Mon, 2 Apr 2018 14:30:51 -0400 (EDT) Subject: Re: [PATCH] Logical decoding of TRUNCATE To: Andres Freund , Marco Nenciarini References: <1cb9bd52-9e3a-21bf-0c0c-afa90f463629@2ndquadrant.com> <0361327b-5d59-32b0-cb13-e844acf0f0d0@2ndquadrant.it> <31773cc5-3b29-1740-e7d2-dc48e80a06ce@2ndquadrant.com> <8ef5c78a-1e98-dce5-82f7-bd0f6eba92e5@2ndquadrant.com> <372b6f96-8415-912a-de72-ec5f54ad677f@2ndquadrant.it> <20180401200107.gjpehuqy63efechn@alap3.anarazel.de> Cc: Petr Jelinek , Thomas Munro , Simon Riggs , PostgreSQL-development From: Peter Eisentraut Organization: 2ndQuadrant Message-ID: <2c12cc3e-6442-eac3-c6db-ee95e5c7f46c@2ndquadrant.com> Date: Mon, 2 Apr 2018 14:30:50 -0400 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 MIME-Version: 1.0 In-Reply-To: <20180401200107.gjpehuqy63efechn@alap3.anarazel.de> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 4/1/18 16:01, Andres Freund wrote: > On 2018-01-25 14:21:15 +0100, Marco Nenciarini wrote: >> + if (SessionReplicationRole != SESSION_REPLICATION_ROLE_REPLICA) >> + { >> + >> + /* >> + * Check foreign key references. In CASCADE mode, this should be >> + * unnecessary since we just pulled in all the references; but as a >> + * cross-check, do it anyway if in an Assert-enabled build. >> + */ >> #ifdef USE_ASSERT_CHECKING >> heap_truncate_check_FKs(rels, false); >> + #else >> + if (stmt->behavior == DROP_RESTRICT) >> + heap_truncate_check_FKs(rels, false); >> #endif >> + } > > That *can't* be right. You mean the part that ignores this in session_replication_role = replica? I don't like that either. And it's also not clear why it's needed for this feature. >> + case REORDER_BUFFER_CHANGE_TRUNCATE: >> + appendStringInfoString(ctx->out, " TRUNCATE:"); >> + >> + if (change->data.truncate_msg.restart_seqs >> + || change->data.truncate_msg.cascade) >> + { >> + if (change->data.truncate_msg.restart_seqs) >> + appendStringInfo(ctx->out, " restart_seqs"); >> + if (change->data.truncate_msg.cascade) >> + appendStringInfo(ctx->out, " cascade"); >> + } >> + else >> + appendStringInfoString(ctx->out, " (no-flags)"); >> + break; >> default: >> Assert(false); >> } > > I know this has been discussed in the thread already, but it really > strikes me as wrong to basically do some mini DDL replication feature > via per-command WAL records. The problem is that the interaction of TRUNCATE and foreign keys is a mess. Let's say I have a provider database with table1, table2, and table3, all connected by foreign keys, and a replica database with table1, table2, and table4, all connected by foreign keys. I run TRUNCATE table1 CASCADE on the provider. What should replication do? The proposed patch applies the TRUNCATE table1 CASCADE on the replica, which ends up truncating table1, table2, and table4, which is different from what happened on the origin. An alternative might be to make the replication protocol message say "I truncated table1, table2" (let's say table3 is not replicated). (This sounds more like logical replication rather than DDL replication.) That will then fail to apply on the replica, because it requires that you include all tables connected by foreign keys. We could then do what we do in the DELETE case and just ignore foreign keys altogether, which is obviously bad and not a forward-looking behavior. Or we could do what we *should* be doing in the DELETE case and apply cascading actions on the replica to table4, but that kind of row-by-row processing is what we want to avoid by using TRUNCATE in the first place. None of these solutions are good. I don't have any other ideas, though. >> *************** >> *** 111,116 **** CREATE PUBLICATION name >> --- 111,121 ---- >> and so the default value for this option is >> 'insert, update, delete'. >> >> + >> + TRUNCATE is treated as a form of >> + DELETE for the purpose of deciding whether >> + to publish, or not. >> + >> >> >> > > Why is this a good idea? I think it seemed like a good idea at the time, so to speak, but several people have argued against it, so we should probably change this in the final version. -- Peter Eisentraut http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services