Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rsbCi-0084z9-MS for pgsql-hackers@arkaria.postgresql.org; Fri, 05 Apr 2024 04:29:29 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1rsbCh-000SO0-TT for pgsql-hackers@arkaria.postgresql.org; Fri, 05 Apr 2024 04:29:27 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rsbCh-000SNn-Jn for pgsql-hackers@lists.postgresql.org; Fri, 05 Apr 2024 04:29:27 +0000 Received: from mail-lj1-x233.google.com ([2a00:1450:4864:20::233]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rsbCe-000ix6-Fb for pgsql-hackers@lists.postgresql.org; Fri, 05 Apr 2024 04:29:26 +0000 Received: by mail-lj1-x233.google.com with SMTP id 38308e7fff4ca-2d68c6a4630so20421161fa.3 for ; Thu, 04 Apr 2024 21:29:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712291362; x=1712896162; darn=lists.postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=UizS8dlVato0M6moSmUozUW6o3DQtLQT2XyKKFuR0ZI=; b=B3QDz9FEdDqLxHQ0JFq8laDBgMRob9Qh9IFY/0UIfEWUHXxfL/WlA3mFLxIktF1UMQ u8GZ6uitU+MvkbiMP1mvErWSGuHUMpenjKzcFdujl8RRQQiX+E3TXPL7w1Lf8tOub3qq 0qThVp2r1JlWqQg+yLsCkQJsDnrr37FH2L6lqaskw0KB7GjEiII+G9g0Wv5IgToIlzcm RNCYDv0Nd30mFonXpSBZagEf65uijSFnWo0wmPl2I+6QwaVDbn4HogeB549reCkALxlu B22WdSKcO6x0quhXXSfgcrWHyOIGRv4gfy3GiRyuGCS/QK0/AptIrKedGqZoMrIgpq5U BYnw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712291362; x=1712896162; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=UizS8dlVato0M6moSmUozUW6o3DQtLQT2XyKKFuR0ZI=; b=mpF6ZqkhR3hJ8VAmzApTH9fmha6CTJypIhfL77G9IPog3Q0t9aZyO2g7ag+kjgNhkd CP/ZPa2G73iAflB+dwRKjWUr7lo3AR6UXVJnakJk3UfvGGXuEYhjsDYDNAgwE3thz2Zs nSYUS7zAu0eZ7VO39VVFZJKrWDqFmJFIJW3jK0QET7SR0n/e4TJwyWY8tbL3PwGlxY+p Om+lovcxi9okN1hDQHNVREWCTOoiY2uF+1rhgLMx3LAr96HnF5RogWxV3xt5A82HfQXd pRa48iY3bc8zqQv1lQoyuJ96WmEcU9+kG22NXsOSk9Chpv8QwnV4cGybeof08IR1w8SR /8aQ== X-Forwarded-Encrypted: i=1; AJvYcCVQ273RqjnMlhYPBTM3+FIfUnRdl/99xN3WmWtgs9SAjE5qyP24BDkhbmxtjxB8oWCmOYd+xPV2a9wXi8oVrcwdjeLN/FIgREaLpsUwKnduCPDx X-Gm-Message-State: AOJu0YxtxRmus4r9TBfNEwWTVDHMq2bDyv9m+J8vVxwCK8EsHMV46TQy KH3acZRLQQctJq+D/5ICDJOavJgdFmy7IRogTLfoj0ZsESiSP3R03M2bc3KMK2e6KpGlaJ6O/jq Y9RHXMzuVhN1g/waFRD3O2X9JsNs= X-Google-Smtp-Source: AGHT+IGhJVfHUyN/8/OH+ogjwFaabC040MuRM/uHT9Hm1V1yZdg2UMoK9UQM9dfVtAFFszD39eX5cvxAUT9zfRrLkbo= X-Received: by 2002:a2e:9183:0:b0:2d4:a8cf:e798 with SMTP id f3-20020a2e9183000000b002d4a8cfe798mr381534ljg.14.1712291362325; Thu, 04 Apr 2024 21:29:22 -0700 (PDT) MIME-Version: 1.0 References: <202404041745.jzrrw3ffyac7@alvherre.pgsql> <5e3a2a809388bc01b66bf7fa692eaac5622acc0a.camel@j-davis.com> In-Reply-To: <5e3a2a809388bc01b66bf7fa692eaac5622acc0a.camel@j-davis.com> From: Bharath Rupireddy Date: Fri, 5 Apr 2024 09:59:09 +0530 Message-ID: Subject: Re: LogwrtResult contended spinlock To: Jeff Davis Cc: Alvaro Herrera , Tom Lane , Andres Freund , pgsql-hackers@lists.postgresql.org, Jaime Casanova Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, Apr 5, 2024 at 4:21=E2=80=AFAM Jeff Davis wrote= : > > > 4. If we're not modifying any callers of WALReadFromBuffers(), then > > AFAICS the added check that the request is not past the Copy pointer > > is > > useless. In a quick look at that code, I think we only try to read > > data > > that's been flushed, not written, so the stricter check that we don't > > read data that hasn't been Copied does nothing. > > Bharath has indicated that he may call WALReadFromBuffers() in an > extension, so I believe some error checking is appropriate there. > > > (Honestly I'm not sure > > that we want XLogSendPhysical to be reading data that has not been > > written, or do we?) > > Not yet, but there has been some discussion[1][2] about future work to > allow replicating data before it's been flushed locally. Right. Although callers of WALReadFromBuffers() in core postgres doesn't need it now (in future, they will), but it allows one to write something up externally - for example, see 0004 patch in https://www.postgresql.org/message-id/CALj2ACWmW+1ZdYE0BD5-4KVLzYGn3=3Dpudo= mwPHuHmi4YQuOFSg@mail.gmail.com where I implemented a xlogreader page_read callback that just keeps reading the WAL that's fully copied to WAL buffers. > Regarding the patches themselves, 0001 looks good to me. A few comments on 0001: 1. /* * Update local copy of shared XLogCtl->log{Write,Flush}Result + * + * It's critical that Flush always trails Write, so the order of the reads= is + * important, as is the barrier. */ #define RefreshXLogWriteResult(_target) \ do { \ - _target.Write =3D XLogCtl->logWriteResult; \ - _target.Flush =3D XLogCtl->logFlushResult; \ + _target.Flush =3D pg_atomic_read_u64(&XLogCtl->logFlushResult); \ + pg_read_barrier(); \ + _target.Write =3D pg_atomic_read_u64(&XLogCtl->logWriteResult); \ } while (0) Is it "Flush always trails Write" or "Flush always leades Write"? I guess the latter, no? 2. + + pg_atomic_write_u64(&XLogCtl->logWriteResult, LogwrtResult.Write); + pg_write_barrier(); + pg_atomic_write_u64(&XLogCtl->logFlushResult, LogwrtResult.Flush); } Maybe add the reason as to why we had to write logWriteResult first and then logFlushResult, similar to the comment atop RefreshXLogWriteResult? > For 0002, did you consider having pg_atomic_monotonic_advance_u64() > return the currval? +1 for returning currval here. Except for the above comments, the patches look good to me. I've also run the test loop for any assertion failure - for i in {1..100}; do make check PROVE_TESTS=3D"t/027_stream_regress.pl"; if [ $? -ne 0 ]; then echo "The command failed on iteration $i"; break; fi; done. -- Bharath Rupireddy PostgreSQL Contributors Team RDS Open Source Databases Amazon Web Services: https://aws.amazon.com