From: Andres Freund <andres@anarazel.de>
To: Simon Riggs <simon@2ndquadrant.com>
Cc: Marco Nenciarini <marco.nenciarini@2ndquadrant.it>
Cc: Petr Jelinek <petr.jelinek@2ndquadrant.com>
Cc: Thomas Munro <thomas.munro@enterprisedb.com>
Cc: PostgreSQL-development <pgsql-hackers@postgresql.org>
Subject: Re: [PATCH] Logical decoding of TRUNCATE
Date: Mon, 2 Apr 2018 11:50:03 -0700
Message-ID: <20180402185003.vlolmqle2mikhl37@alap3.anarazel.de> (raw)
In-Reply-To: <CANP8+jLMet1MHkYP2no9Wrn5RCiCJvFsF+ie-YUDk2nfXCnjRw@mail.gmail.com>
References: <de2a9d73-1e25-797c-03f8-9faa57f75e85@2ndquadrant.it>
<ffb116ef-a072-2b9b-dd92-e288dbfa0f6c@2ndquadrant.com>
<0361327b-5d59-32b0-cb13-e844acf0f0d0@2ndquadrant.it>
<31773cc5-3b29-1740-e7d2-dc48e80a06ce@2ndquadrant.com>
<fde57e23-9828-8741-bb60-2b2d9e4ad55a@2ndquadrant.it>
<CAEepm=0AP2-z05GCxz0yTvtG3uW+gdZkZqW1EY97Sdy_G6tA5w@mail.gmail.com>
<8ef5c78a-1e98-dce5-82f7-bd0f6eba92e5@2ndquadrant.com>
<372b6f96-8415-912a-de72-ec5f54ad677f@2ndquadrant.it>
<20180401200107.gjpehuqy63efechn@alap3.anarazel.de>
<CANP8+jLMet1MHkYP2no9Wrn5RCiCJvFsF+ie-YUDk2nfXCnjRw@mail.gmail.com>
Hi,
On 2018-04-02 07:39:57 +0100, Simon Riggs wrote:
> On 1 April 2018 at 21:01, Andres Freund <andres@anarazel.de> wrote:
>
> >> ***************
> >> *** 111,116 **** CREATE PUBLICATION <replaceable class="parameter">name</replaceable>
> >> --- 111,121 ----
> >> and so the default value for this option is
> >> <literal>'insert, update, delete'</literal>.
> >> </para>
> >> + <para>
> >> + <command>TRUNCATE</command> is treated as a form of
> >> + <command>DELETE</command> for the purpose of deciding whether
> >> + to publish, or not.
> >> + </para>
> >> </listitem>
> >> </varlistentry>
> >> </variablelist>
> >
> > 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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: andres@anarazel.de, simon@2ndquadrant.com, marco.nenciarini@2ndquadrant.it, petr.jelinek@2ndquadrant.com, thomas.munro@enterprisedb.com, pgsql-hackers@postgresql.org
Subject: Re: [PATCH] Logical decoding of TRUNCATE
In-Reply-To: <20180402185003.vlolmqle2mikhl37@alap3.anarazel.de>
References: <de2a9d73-1e25-797c-03f8-9faa57f75e85@2ndquadrant.it> <ffb116ef-a072-2b9b-dd92-e288dbfa0f6c@2ndquadrant.com> <0361327b-5d59-32b0-cb13-e844acf0f0d0@2ndquadrant.it> <31773cc5-3b29-1740-e7d2-dc48e80a06ce@2ndquadrant.com> <fde57e23-9828-8741-bb60-2b2d9e4ad55a@2ndquadrant.it> <CAEepm=0AP2-z05GCxz0yTvtG3uW+gdZkZqW1EY97Sdy_G6tA5w@mail.gmail.com> <8ef5c78a-1e98-dce5-82f7-bd0f6eba92e5@2ndquadrant.com> <372b6f96-8415-912a-de72-ec5f54ad677f@2ndquadrant.it> <20180401200107.gjpehuqy63efechn@alap3.anarazel.de> <CANP8+jLMet1MHkYP2no9Wrn5RCiCJvFsF+ie-YUDk2nfXCnjRw@mail.gmail.com> <20180402185003.vlolmqle2mikhl37@alap3.anarazel.de>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox