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 1f34Ww-0002hW-SY for pgsql-hackers@arkaria.postgresql.org; Mon, 02 Apr 2018 18:50:10 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1f34Wu-0007BI-IG for pgsql-hackers@arkaria.postgresql.org; Mon, 02 Apr 2018 18:50:08 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f34Wu-0007B4-7Z for pgsql-hackers@lists.postgresql.org; Mon, 02 Apr 2018 18:50:08 +0000 Received: from out5-smtp.messagingengine.com ([66.111.4.29]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f34Wr-0008Ic-K4 for pgsql-hackers@postgresql.org; Mon, 02 Apr 2018 18:50:06 +0000 Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.nyi.internal (Postfix) with ESMTP id CAE2820EC2; Mon, 2 Apr 2018 14:50:04 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute5.internal (MEProxy); Mon, 02 Apr 2018 14:50:04 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=anarazel.de; h= cc:content-type:date:from:in-reply-to:message-id:mime-version :references:subject:to:x-me-sender:x-me-sender:x-sasl-enc; s= fm1; bh=8X9NK75Avkeq8NjDjp89N9tXnfbD3wqrYuB6lf0K+Hc=; b=jrnRF0l6 +SFkeDmlkm8YDdZCXXhAlo4XWRscng6v8fMwNmkXih4jEr0P2DDa5bK5PxZ4h4dl OhD7p8xLBl8254P7eltAmUUmInCVMm0098VExqoAHyy+KZisfkxiVK8fzCE0fMci ZCFCDjdGh11JGjuOMyCM91JTOja/eEhHb4cHCtpKdoSbAZfRHO3YbhRQBwpU62oR xxowQ53PNQ3atAtNPlxB80egOAP9j+//sx2gE2qzb9GvYurczk+zd6BkzM4Jv2HV 37tFSUSChEWgGiBVIJ5JCnq+AfJ10r56biWZyp470DRduZSNtfJ8YKvw6fvnQoZZ lIO69a71DoJ4OQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc: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=8X9NK75Avkeq8NjDjp89N9tXnfbD3 wqrYuB6lf0K+Hc=; b=LfkFPh6aFMsQ9QITLkNLwaAbWMPp01F56leC13WHVVGkx hVZDPiiLBNcvqYhKPyPpQ3e4oWJFQn2Hmc1Rly1SQeJVs9ucmmKxm2NPop3mmceu TT1teLhh+HfL21TR5DJPIMjVMpbjzEfVqY4savGcYDXZOaeGewReaXqGxN1RIt5z N4vya4lHWnR/laWva9PSSpJsl1GBeo+KiKoyZJi9fLBIISZBURgrqWZTn+WI6Zve Tw0ULhNbV+pNfndzRgh5TPMjpkOVlSWYc+ubyIkLGol178smYNBrOyl2k8fgZ3Lg Glt3K89qb48d4NcQIEoBy2LtpDPo+11zF7KA4b/3Q== X-ME-Sender: Received: from intern.anarazel.de (unknown [136.24.76.189]) by mail.messagingengine.com (Postfix) with ESMTPA id 76DA410264; Mon, 2 Apr 2018 14:50:04 -0400 (EDT) Date: Mon, 2 Apr 2018 11:50:03 -0700 From: Andres Freund To: Simon Riggs Cc: Marco Nenciarini , Petr Jelinek , Thomas Munro , PostgreSQL-development Subject: Re: [PATCH] Logical decoding of TRUNCATE Message-ID: <20180402185003.vlolmqle2mikhl37@alap3.anarazel.de> References: <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> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hi, On 2018-04-02 07:39:57 +0100, Simon Riggs wrote: > On 1 April 2018 at 21:01, Andres Freund wrote: > > >> *************** > >> *** 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? > > TRUNCATE removes rows, just as DELETE does, so anybody that wants to > publish the removal of rows will be interested in this. I'm not convinced. I think it's perfectly reasonable to want to truncate away data on the live node, but maintain it on an archival node. In that case one commonly wants to continue to maintain OLTP changes (hence DELETE), but not the bulk truncation. I think there's a reasonable counter-argument in that this isn't fine grained enough. > >> + * Write a WAL record to allow this set of actions to be logically decoded. > >> + * We could optimize this away when !RelationIsLogicallyLogged(rel) > >> + * but that doesn't save much space or time. > > > > What you're saying isn't that you're not logging anything, but that > > you're allocating the header regardless? Because this certainly sounds > > like you unconditionally log a WAL record. > > It says that, yes, my idea - as explained. My complaint is that the comment and the actual implementation differ. The above sounds like it's unconditionally WAL logging, but: + /* + * Write a WAL record to allow this set of actions to be logically decoded. + * We could optimize this away when !RelationIsLogicallyLogged(rel) + * but that doesn't save much space or time. + * + * Assemble an array of relids, then an array of seqrelids so we can write + * a single WAL record for the whole action. + */ + logrelids = palloc(maxrelids * sizeof(Oid)); + foreach (cell, relids_logged) + { + nrelids++; + if (nrelids > maxrelids) + { + maxrelids *= 2; + logrelids = repalloc(logrelids, maxrelids * sizeof(Oid)); + } + logrelids[nrelids - 1] = lfirst_oid(cell); + } + + foreach (cell, seq_relids_logged) + { + nseqrelids++; + if ((nrelids + nseqrelids) > maxrelids) + { + maxrelids *= 2; + logrelids = repalloc(logrelids, maxrelids * sizeof(Oid)); + } + logrelids[nrelids + nseqrelids - 1] = lfirst_oid(cell); + } + + if ((nrelids + nseqrelids) > 0) + { + xl_heap_truncate xlrec; + + xlrec.dbId = MyDatabaseId; + xlrec.nrelids = nrelids; + xlrec.nseqrelids = nseqrelids; + xlrec.flags = 0; + if (behavior == DROP_CASCADE) + xlrec.flags |= XLH_TRUNCATE_CASCADE; + if (restart_seqs) + xlrec.flags |= XLH_TRUNCATE_RESTART_SEQS; + + XLogBeginInsert(); + XLogRegisterData((char *) &xlrec, SizeOfHeapTruncate); + XLogRegisterData((char *) logrelids, + (nrelids + nseqrelids) * sizeof(Oid)); + + XLogSetRecordFlags(XLOG_INCLUDE_ORIGIN); + + (void) XLogInsert(RM_HEAP_ID, XLOG_HEAP_TRUNCATE); + } Note that the XLogInsert() is in an if branch that's only executed if there's either logged relids or sequence relids. I think logrelids should be allocated at the max size immediately, and the comment rewritten to explicitly only talk about the allocation, rather than sounding like it's about WAL logging as well. Greetings, Andres Freund