Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iALkz-0002k1-T4 for pgsql-hackers@arkaria.postgresql.org; Tue, 17 Sep 2019 22:15:34 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iALkx-0006Dm-0M for pgsql-hackers@arkaria.postgresql.org; Tue, 17 Sep 2019 22:15:31 +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_SHA1:256) (Exim 4.89) (envelope-from ) id 1iALkw-0006Dd-HO for pgsql-hackers@lists.postgresql.org; Tue, 17 Sep 2019 22:15:30 +0000 Received: from mail-qk1-x744.google.com ([2607:f8b0:4864:20::744]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iALks-0001Fq-Oy for pgsql-hackers@postgresql.org; Tue, 17 Sep 2019 22:15:30 +0000 Received: by mail-qk1-x744.google.com with SMTP id w2so5824029qkf.2 for ; Tue, 17 Sep 2019 15:15:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:mime-version:content-disposition :content-transfer-encoding:in-reply-to:user-agent; bh=MO+PBsDvhGGLFUJ6HIhqhr7pfVOUq9wkcJcuMYNaNuI=; b=Yt3C++Poosiaxe8gowaq9/nPurOM98shrPPY6z3wiJHbJlwgz9DKlkU/YK1nXfOenE Fr8/VmXAvfN/rufrltwbXAV8TTHCcVb6jLTwg3gWJzSBQXiFXqLh4B9pP2BmTbrUpIjQ iXzr5dKOcLCIMZrHYVhyWam0mb1wpuis45AXxDyM/RKIaXPLKYiQWRspWvE9vpSqqzUQ WBv80bG193o6nxROzRX1QTgaOvNu9DU3fLpWno4Zl7YxdmFXb0pLMrMVrXy37I4k/oot PI2bBsHArYCvqa1X7gSX+hJ+VmDW95Pbu7ix9gLHJ45IseQS2xQZNPwoPTBRFeo81uTp egPg== 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:mime-version :content-disposition:content-transfer-encoding:in-reply-to :user-agent; bh=MO+PBsDvhGGLFUJ6HIhqhr7pfVOUq9wkcJcuMYNaNuI=; b=GPMYtV/qAjH6TTcmVc2/j+nS96MaAiggGmIxHjvA21rM4dErbGrKQistJHy+kQKatd /NiWHu9CebHmQD1Nlx5euJcpclQGtJCfsPX6WeFLbN7Pa0eEkPPKrP/mkuVkj2niVNOc IIUiAkZw+oR5gfxuHWy0Y3mM0MFKv76LZnXMAE2SfuxVk2BDTuzjr4nA7A99sJt/Aguh wJ+DyrTJg0XoThS5pRXUMMIPyDamcPjZQzreNEK0oa3vOWIDkWw9c3Zq64Q6DrTZoszw KRjL4GthYpXumFKES6aSXhfJGyLSqB/uE1GgpVvHUw5pOe0fJ3DqTa1NEyFLZMUXJxNP ul4A== X-Gm-Message-State: APjAAAUdvffGHGuILIPK7D/yhO1rNs1kpIK2ul2SsBeyabfwRhOpAPaF YmNL9HuRw7sjDT5veViG8sML1Q== X-Google-Smtp-Source: APXvYqwXlWN6TWJk244/FMIbj71redK3WYSXpcvOoob3P+FhBh5ewf4DOcz1rGf217XH3mcRW0Ad/Q== X-Received: by 2002:ae9:ee10:: with SMTP id i16mr813083qkg.35.1568758524924; Tue, 17 Sep 2019 15:15:24 -0700 (PDT) Received: from nimloth.alvh.no-ip.org ([190.121.29.3]) by smtp.gmail.com with ESMTPSA id t40sm2118961qta.36.2019.09.17.15.15.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Sep 2019 15:15:24 -0700 (PDT) Received: by nimloth.alvh.no-ip.org (Postfix, from userid 1000) id 29087120FA1; Tue, 17 Sep 2019 19:15:21 -0300 (-03) Date: Tue, 17 Sep 2019 19:15:21 -0300 From: Alvaro Herrera To: Antonin Houska Cc: Thomas Munro , Robert Haas , "pgsql-hackers@postgresql.org" Subject: Re: Attempt to consolidate reading of XLOG page Message-ID: <20190917221521.GA15733@alvherre.pgsql> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="PEIAKu/WMn1b1Hv9" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <13170.1568024430@antos> User-Agent: Mutt/1.9.4 (2018-02-28) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --PEIAKu/WMn1b1Hv9 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit I was confused by the struct name XLogSegment -- the struct is used to represent a WAL segment while it's kept open, rather than just a WAL segment in abstract. Also, now that we've renamed everything to use the term WAL, it seems wrong to use the name XLog for new structs. I propose the name WALOpenSegment for the struct, which solves both problems. (Its initializer function would get the name WALOpenSegmentInit.) Now, the patch introduces a callback for XLogRead, the type of which is called XLogOpenSegment. If we rename it from XLog to WAL, both names end up the same. I propose to rename the function type to WALSegmentOpen, which in a "noun-verb" view of the world, represents the action of opening a WAL segment. I attach a patch for all this renaming, on top of your series. I wonder if each of those WALSegmentOpen callbacks should reset [at least some members of] the struct; they're already in charge of setting ->file, and apparently we're leaving the responsibility of setting the rest of the members to XLogRead. That seems weird. Maybe we should say that the CB should only open the segment and not touch the struct at all and XLogRead is in charge of everything. Perhaps the other way around -- the CB should set everything correctly ... I'm not sure which is best. But having half here and half there seems a recipe for confusion and bugs. Another thing I didn't like much is that everything seems to assume that the only error possible from XLogRead is a read error. Maybe that's okay, because it seems to be the current reality, but it seemed odd. -- Álvaro Herrera https://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services --PEIAKu/WMn1b1Hv9 Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="xlogopen-rename.patch" diff --git a/src/backend/access/transam/xlogreader.c b/src/backend/access/transam/xlogreader.c index 2a80bc5823..3e03d72a18 100644 --- a/src/backend/access/transam/xlogreader.c +++ b/src/backend/access/transam/xlogreader.c @@ -100,7 +100,7 @@ XLogReaderAllocate(int wal_segment_size, XLogPageReadCB pagereadfunc, } /* Initialize segment pointer. */ - XLogSegmentInit(&state->seg, wal_segment_size); + WALOpenSegmentInit(&state->seg, wal_segment_size); state->read_page = pagereadfunc; /* system_identifier initialized to zeroes above */ @@ -1006,7 +1006,7 @@ out: * Initialize the passed segment pointer. */ void -XLogSegmentInit(XLogSegment *seg, int size) +WALOpenSegmentInit(WALOpenSegment *seg, int size) { seg->file = -1; seg->num = 0; @@ -1032,7 +1032,7 @@ XLogSegmentInit(XLogSegment *seg, int size) */ bool XLogRead(char *buf, XLogRecPtr startptr, Size count, - TimeLineID *tli, XLogSegment *seg, XLogOpenSegment openSegment) + TimeLineID *tli, WALOpenSegment *seg, WALSegmentOpen openSegment) { char *p; XLogRecPtr recptr; @@ -1120,7 +1120,7 @@ XLogRead(char *buf, XLogRecPtr startptr, Size count, * Backend-specific code to handle errors encountered by XLogRead(). */ void -XLogReadProcessError(XLogSegment *seg) +XLogReadProcessError(WALOpenSegment *seg) { if (errno != 0) { diff --git a/src/backend/access/transam/xlogutils.c b/src/backend/access/transam/xlogutils.c index 5b0ba39f17..2b9758c3ce 100644 --- a/src/backend/access/transam/xlogutils.c +++ b/src/backend/access/transam/xlogutils.c @@ -776,13 +776,15 @@ XLogReadDetermineTimeline(XLogReaderState *state, XLogRecPtr wantPage, uint32 wa */ static void read_local_xlog_page_open_segment(XLogSegNo nextSegNo, TimeLineID *tli, - XLogSegment *seg) + WALOpenSegment *seg) { char path[MAXPGPATH]; XLogFilePath(path, *tli, nextSegNo, seg->size); seg->file = BasicOpenFile(path, O_RDONLY | PG_BINARY); + /* XXX should we clear the other fields of WALOpenSegment?? */ + if (seg->file < 0) { if (errno == ENOENT) diff --git a/src/backend/replication/walsender.c b/src/backend/replication/walsender.c index 072d46d853..a5ca834020 100644 --- a/src/backend/replication/walsender.c +++ b/src/backend/replication/walsender.c @@ -128,7 +128,7 @@ bool log_replication_commands = false; */ bool wake_wal_senders = false; -static XLogSegment *sendSeg = NULL; +static WALOpenSegment *sendSeg = NULL; /* * These variables keep track of the state of the timeline we're currently @@ -248,7 +248,7 @@ static TimeOffset LagTrackerRead(int head, XLogRecPtr lsn, TimestampTz now); static bool TransactionIdInRecentPast(TransactionId xid, uint32 epoch); static void WalSndOpenSegment(XLogSegNo nextSegNo, TimeLineID *tli, - XLogSegment *seg); + WALOpenSegment *seg); /* Initialize walsender process before entering the main command loop */ @@ -279,8 +279,9 @@ InitWalSender(void) lag_tracker = MemoryContextAllocZero(TopMemoryContext, sizeof(LagTracker)); /* Make sure we can remember the current read position in XLOG. */ - sendSeg = (XLogSegment *) MemoryContextAlloc(TopMemoryContext, sizeof(XLogSegment)); - XLogSegmentInit(sendSeg, wal_segment_size); + sendSeg = (WALOpenSegment *) + MemoryContextAlloc(TopMemoryContext, sizeof(WALOpenSegment)); + WALOpenSegmentInit(sendSeg, wal_segment_size); } /* @@ -2354,7 +2355,7 @@ WalSndKill(int code, Datum arg) * Callback for XLogRead() to open the next segment. */ void -WalSndOpenSegment(XLogSegNo nextSegNo, TimeLineID *tli, XLogSegment *seg) +WalSndOpenSegment(XLogSegNo nextSegNo, TimeLineID *tli, WALOpenSegment *seg) { char path[MAXPGPATH]; diff --git a/src/bin/pg_waldump/pg_waldump.c b/src/bin/pg_waldump/pg_waldump.c index cd44090d36..8466890748 100644 --- a/src/bin/pg_waldump/pg_waldump.c +++ b/src/bin/pg_waldump/pg_waldump.c @@ -296,7 +296,7 @@ identify_target_directory(XLogDumpPrivate *private, char *directory, } static void -XLogDumpOpenSegment(XLogSegNo nextSegNo, TimeLineID *tli, XLogSegment *seg) +WALDumpOpenSegment(XLogSegNo nextSegNo, TimeLineID *tli, WALOpenSegment *seg) { char fname[MAXPGPATH]; int tries; @@ -358,9 +358,9 @@ XLogDumpReadPage(XLogReaderState *state, XLogRecPtr targetPagePtr, int reqLen, } if (!XLogRead(readBuff, targetPagePtr, count, &private->timeline, - &state->seg, XLogDumpOpenSegment)) + &state->seg, WALDumpOpenSegment)) { - XLogSegment *seg = &state->seg; + WALOpenSegment *seg = &state->seg; int err = errno; char fname[MAXPGPATH]; int save_errno = errno; diff --git a/src/include/access/xlogreader.h b/src/include/access/xlogreader.h index 4731023ccc..c93b255d84 100644 --- a/src/include/access/xlogreader.h +++ b/src/include/access/xlogreader.h @@ -32,9 +32,9 @@ #include "access/xlogrecord.h" /* - * Position in XLOG file while reading it. + * WALOpenSegment represents a WAL segment being read. */ -typedef struct XLogSegment +typedef struct WALOpenSegment { int file; /* segment file descriptor */ XLogSegNo num; /* segment number */ @@ -44,7 +44,7 @@ typedef struct XLogSegment char *dir; /* directory (only needed by frontends) */ int size; /* segment size */ int last_req; /* the amount of data requested last time */ -} XLogSegment; +} WALOpenSegment; typedef struct XLogReaderState XLogReaderState; @@ -165,7 +165,7 @@ struct XLogReaderState uint32 readLen; /* last read XLOG position for data currently in readBuf */ - XLogSegment seg; + WALOpenSegment seg; /* * beginning of prior page read, and its TLI. Doesn't necessarily @@ -227,7 +227,7 @@ extern XLogRecPtr XLogFindNextRecord(XLogReaderState *state, XLogRecPtr RecPtr); #endif /* FRONTEND */ /* - * Callback to open the specified XLOG segment nextSegNo in timeline *tli for + * Callback to open the specified WAL segment nextSegNo in timeline *tli for * reading, and assign the descriptor to ->file. BasicOpenFile() is the * preferred way to open the segment file in backend code, whereas open(2) * should be used in frontend. @@ -235,15 +235,15 @@ extern XLogRecPtr XLogFindNextRecord(XLogReaderState *state, XLogRecPtr RecPtr); * If NULL is passed for tli, the callback must determine the timeline * itself. In any case it's supposed to eventually set ->tli. */ -typedef void (*XLogOpenSegment) (XLogSegNo nextSegNo, TimeLineID *tli, - XLogSegment *seg); +typedef void (*WALSegmentOpen) (XLogSegNo nextSegNo, TimeLineID *tli, + WALOpenSegment *seg); -extern void XLogSegmentInit(XLogSegment *seg, int size); +extern void WALOpenSegmentInit(WALOpenSegment *seg, int size); extern bool XLogRead(char *buf, XLogRecPtr startptr, Size count, - TimeLineID *tli, XLogSegment *seg, - XLogOpenSegment openSegment); + TimeLineID *tli, WALOpenSegment *seg, + WALSegmentOpen openSegment); #ifndef FRONTEND -void XLogReadProcessError(XLogSegment *seg); +void XLogReadProcessError(WALOpenSegment *seg); #endif /* Functions for decoding an XLogRecord */ --PEIAKu/WMn1b1Hv9--