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 1f5Vwk-0001Pf-7S for pgsql-hackers@arkaria.postgresql.org; Mon, 09 Apr 2018 12:30:54 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1f5Vwj-0001VD-4l for pgsql-hackers@arkaria.postgresql.org; Mon, 09 Apr 2018 12:30:53 +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 1f5Vwi-0001V3-QJ for pgsql-hackers@lists.postgresql.org; Mon, 09 Apr 2018 12:30:52 +0000 Received: from mail-lf0-x234.google.com ([2a00:1450:4010:c07::234]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1f5Vwa-0006sf-Ur for pgsql-hackers@postgresql.org; Mon, 09 Apr 2018 12:30:51 +0000 Received: by mail-lf0-x234.google.com with SMTP id q9-v6so6281561lfk.9 for ; Mon, 09 Apr 2018 05:30:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:subject:to:references:cc:from:message-id:date:user-agent :mime-version:in-reply-to:content-language; bh=hl7He6BCz/9lYvibmoodNC6g1ib+r7Z6eh2rz5xBrRE=; b=bl8Dh9g/z3d8HxLeMmR/9k/qmInlR0WbNQ9lEnaRUBb66TBmCwqJoxfsonF37MOIiL X4YFt/qZlI4bziEcGnckzq+9UAq2sgkWmdESQZHvWLIxaYABWd1YWzJy2IflhQpOGvTK +lY7K4bHlVYZf9XYQxmiG2xG7NcUhs4V9lg05+9y1bS/PptmMMEZyDukG0YL6jqLmVgN RGcfRkgmIyy8FoXRZJg4M21Hr7EcWZOkkM6AlR30TMSVc9aVDAolWIwQyEM76+MwueK3 qdhaVfpt7DOpxkyM2I05QLnqXFOn/Xpkgm/nCMs8/crHBD2Cz6BXMdeZhejRCm6j8v5f t3ig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:subject:to:references:cc:from:message-id :date:user-agent:mime-version:in-reply-to:content-language; bh=hl7He6BCz/9lYvibmoodNC6g1ib+r7Z6eh2rz5xBrRE=; b=Uv4kQ+mmGGrpy/pihpuLFdrVrUneZY1M9I1rv3eTDscaB+IFcFRjHXnUTD9YE48hBQ vOJKomuQJBpCJLVpIZIX+qZE/PHunczshbX1Vim6lbuGmsde50KH/o1sKEx1d03OyNIV TTGwZeGd3Nf0zXmzjClxgOsoptkDKoLLxZNDmMHHcG3FlUPF++WRYhe8I6JdcIZHVq98 7aUVgY2tkWNAYPEialA8Yc0T+VFzj5Y78gnuQluVYszf5c5J5vhhbvitmVZyUDng2AxD pv1qGrP3udQ7JmM3X6fLbfnfYRXpNpIpKEvs3XY6kiALTPaMuvOpz+BIFU2MaY6SLgiU ivkw== X-Gm-Message-State: ALQs6tB5wk2Ve3ASru1ZrPrInqSnt9ws0D4ehXiM9KtRu8bo9XACfVjM S40cgaMRrJzgS2QWsPf2sIXodw== X-Google-Smtp-Source: AIpwx494HHbdXmHY/usYiPBUEOmr1TdzOhiqtX3MD24ex+WhhRfxlu2fkMySP60glYMBTowEv2FEbg== X-Received: by 10.46.16.130 with SMTP id 2mr22166232ljq.119.1523277041851; Mon, 09 Apr 2018 05:30:41 -0700 (PDT) Received: from [192.168.1.111] (dsl-hkibng32-54fbf8-78.dhcp.inet.fi. [84.251.248.78]) by smtp.gmail.com with ESMTPSA id o8sm47777ljg.52.2018.04.09.05.30.40 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 09 Apr 2018 05:30:40 -0700 (PDT) Sender: Heikki Linnakangas Subject: Re: pgsql: Store 2PC GID in commit/abort WAL recs for logical decoding To: Simon Riggs References: Cc: pgsql-hackers From: Heikki Linnakangas Message-ID: <33b787bf-dc20-1161-54e9-3f3b607bf59d@iki.fi> Date: Mon, 9 Apr 2018 15:30:39 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.7.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/mixed; boundary="------------D765DD69D112128B9690C749" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------D765DD69D112128B9690C749 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit On 28/03/18 19:47, Simon Riggs wrote: > Store 2PC GID in commit/abort WAL recs for logical decoding This forgot to update the comments in xl_xact_commit and xl_xact_abort, for the new fields. > + > + if (parsed->xinfo & XACT_XINFO_HAS_GID) > + { > + int gidlen; > + strcpy(parsed->twophase_gid, data); > + gidlen = strlen(parsed->twophase_gid) + 1; > + data += MAXALIGN(gidlen); > + } > + } > + > + if (parsed->xinfo & XACT_XINFO_HAS_ORIGIN) > + { > + xl_xact_origin xl_origin; > + > + /* we're only guaranteed 4 byte alignment, so copy onto stack */ > + memcpy(&xl_origin, data, sizeof(xl_origin)); > + > + parsed->origin_lsn = xl_origin.origin_lsn; > + parsed->origin_timestamp = xl_origin.origin_timestamp; > + > + data += sizeof(xl_xact_origin); > } There seems to be some confusion on the padding here. Firstly, what's the point of zero-padding the GID length to the next MAXALIGN boundary, which would be 8 bytes on 64-bit systems, if the start is only guaranteed 4-byte alignment, per the comment at the memcpy() above. Secondly, if we're memcpying the fields that follow anyway, why bother with any alignment padding at all? I propose the attached. - Heikki --------------D765DD69D112128B9690C749 Content-Type: text/x-patch; name="gid-in-wal-records-cleanup.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="gid-in-wal-records-cleanup.patch" diff --git a/src/backend/access/rmgrdesc/xactdesc.c b/src/backend/access/rmgrdesc/xactdesc.c index 3b3c95f810..2e4ea62e62 100644 --- a/src/backend/access/rmgrdesc/xactdesc.c +++ b/src/backend/access/rmgrdesc/xactdesc.c @@ -105,10 +105,8 @@ ParseCommitRecord(uint8 info, xl_xact_commit *xlrec, xl_xact_parsed_commit *pars if (parsed->xinfo & XACT_XINFO_HAS_GID) { - int gidlen; strlcpy(parsed->twophase_gid, data, sizeof(parsed->twophase_gid)); - gidlen = strlen(data) + 1; - data += MAXALIGN(gidlen); + data += strlen(data) + 1; } } @@ -116,7 +114,7 @@ ParseCommitRecord(uint8 info, xl_xact_commit *xlrec, xl_xact_parsed_commit *pars { xl_xact_origin xl_origin; - /* we're only guaranteed 4 byte alignment, so copy onto stack */ + /* we're not guaranteed any alignment, so copy onto stack */ memcpy(&xl_origin, data, sizeof(xl_origin)); parsed->origin_lsn = xl_origin.origin_lsn; @@ -189,10 +187,8 @@ ParseAbortRecord(uint8 info, xl_xact_abort *xlrec, xl_xact_parsed_abort *parsed) if (parsed->xinfo & XACT_XINFO_HAS_GID) { - int gidlen; strlcpy(parsed->twophase_gid, data, sizeof(parsed->twophase_gid)); - gidlen = strlen(data) + 1; - data += MAXALIGN(gidlen); + data += strlen(data) + 1; } } @@ -200,7 +196,7 @@ ParseAbortRecord(uint8 info, xl_xact_abort *xlrec, xl_xact_parsed_abort *parsed) { xl_xact_origin xl_origin; - /* we're only guaranteed 4 byte alignment, so copy onto stack */ + /* we're not guaranteed any alignment, so copy onto stack */ memcpy(&xl_origin, data, sizeof(xl_origin)); parsed->origin_lsn = xl_origin.origin_lsn; diff --git a/src/backend/access/transam/twophase.c b/src/backend/access/transam/twophase.c index d6e4b7980f..5c05d545c4 100644 --- a/src/backend/access/transam/twophase.c +++ b/src/backend/access/transam/twophase.c @@ -1129,9 +1129,11 @@ EndPrepare(GlobalTransaction gxact) gxact->prepare_end_lsn = XLogInsert(RM_XACT_ID, XLOG_XACT_PREPARE); if (replorigin) + { /* Move LSNs forward for this replication origin */ replorigin_session_advance(replorigin_session_origin_lsn, gxact->prepare_end_lsn); + } XLogFlush(gxact->prepare_end_lsn); diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c index b88d4ccf74..1dcf825625 100644 --- a/src/backend/access/transam/xact.c +++ b/src/backend/access/transam/xact.c @@ -5248,7 +5248,6 @@ XactLogCommitRecord(TimestampTz commit_time, xl_xact_origin xl_origin; uint8 info; - int gidlen = 0; Assert(CritSectionCount > 0); @@ -5314,10 +5313,7 @@ XactLogCommitRecord(TimestampTz commit_time, Assert(twophase_gid != NULL); if (XLogLogicalInfoActive()) - { xl_xinfo.xinfo |= XACT_XINFO_HAS_GID; - gidlen = strlen(twophase_gid) + 1; /* include '\0' */ - } } /* dump transaction origin information */ @@ -5371,12 +5367,7 @@ XactLogCommitRecord(TimestampTz commit_time, { XLogRegisterData((char *) (&xl_twophase), sizeof(xl_xact_twophase)); if (xl_xinfo.xinfo & XACT_XINFO_HAS_GID) - { - static const char zeroes[MAXIMUM_ALIGNOF] = { 0 }; - XLogRegisterData((char*) twophase_gid, gidlen); - if (MAXALIGN(gidlen) != gidlen) - XLogRegisterData((char*) zeroes, MAXALIGN(gidlen) - gidlen); - } + XLogRegisterData((char *) twophase_gid, strlen(twophase_gid)); } if (xl_xinfo.xinfo & XACT_XINFO_HAS_ORIGIN) @@ -5410,7 +5401,6 @@ XactLogAbortRecord(TimestampTz abort_time, xl_xact_origin xl_origin; uint8 info; - int gidlen = 0; Assert(CritSectionCount > 0); @@ -5449,10 +5439,7 @@ XactLogAbortRecord(TimestampTz abort_time, Assert(twophase_gid != NULL); if (XLogLogicalInfoActive()) - { xl_xinfo.xinfo |= XACT_XINFO_HAS_GID; - gidlen = strlen(twophase_gid) + 1; /* include '\0' */ - } } if (TransactionIdIsValid(twophase_xid) && XLogLogicalInfoActive()) @@ -5488,7 +5475,6 @@ XactLogAbortRecord(TimestampTz abort_time, if (xl_xinfo.xinfo & XACT_XINFO_HAS_DBINFO) XLogRegisterData((char *) (&xl_dbinfo), sizeof(xl_dbinfo)); - if (xl_xinfo.xinfo & XACT_XINFO_HAS_SUBXACTS) { XLogRegisterData((char *) (&xl_subxacts), @@ -5509,14 +5495,14 @@ XactLogAbortRecord(TimestampTz abort_time, { XLogRegisterData((char *) (&xl_twophase), sizeof(xl_xact_twophase)); if (xl_xinfo.xinfo & XACT_XINFO_HAS_GID) - { - static const char zeroes[MAXIMUM_ALIGNOF] = { 0 }; - XLogRegisterData((char*) twophase_gid, gidlen); - if (MAXALIGN(gidlen) != gidlen) - XLogRegisterData((char*) zeroes, MAXALIGN(gidlen) - gidlen); - } + XLogRegisterData((char *) twophase_gid, strlen(twophase_gid) + 1); } + /* + * NOTE: twophase_gid is variable-length, so no alignment is guaranteed + * after this point. + */ + if (xl_xinfo.xinfo & XACT_XINFO_HAS_ORIGIN) XLogRegisterData((char *) (&xl_origin), sizeof(xl_xact_origin)); diff --git a/src/include/access/xact.h b/src/include/access/xact.h index a46396f2d9..3661b8d090 100644 --- a/src/include/access/xact.h +++ b/src/include/access/xact.h @@ -269,6 +269,7 @@ typedef struct xl_xact_commit /* xl_xact_relfilenodes follows if XINFO_HAS_RELFILENODES */ /* xl_xact_invals follows if XINFO_HAS_INVALS */ /* xl_xact_twophase follows if XINFO_HAS_TWOPHASE */ + /* twophase_gid follows if XINFO_HAS_GID. As a null-terminated string. */ /* xl_xact_origin follows if XINFO_HAS_ORIGIN, stored unaligned! */ } xl_xact_commit; #define MinSizeOfXactCommit (offsetof(xl_xact_commit, xact_time) + sizeof(TimestampTz)) @@ -278,11 +279,13 @@ typedef struct xl_xact_abort TimestampTz xact_time; /* time of abort */ /* xl_xact_xinfo follows if XLOG_XACT_HAS_INFO */ - /* No db_info required */ + /* xl_xact_dbinfo follows if XINFO_HAS_DBINFO */ /* xl_xact_subxacts follows if HAS_SUBXACT */ /* xl_xact_relfilenodes follows if HAS_RELFILENODES */ /* No invalidation messages needed. */ /* xl_xact_twophase follows if XINFO_HAS_TWOPHASE */ + /* twophase_gid follows if XINFO_HAS_GID. As a null-terminated string. */ + /* xl_xact_origin follows if XINFO_HAS_ORIGIN, stored unaligned! */ } xl_xact_abort; #define MinSizeOfXactAbort sizeof(xl_xact_abort) --------------D765DD69D112128B9690C749--