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 1rsVvI-007ZBv-3s for pgsql-hackers@arkaria.postgresql.org; Thu, 04 Apr 2024 22:51:08 +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 1rsVvH-00Dy3J-5M for pgsql-hackers@arkaria.postgresql.org; Thu, 04 Apr 2024 22:51:07 +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 1rsVvG-00Dy39-NF for pgsql-hackers@lists.postgresql.org; Thu, 04 Apr 2024 22:51:06 +0000 Received: from mail-pl1-x62e.google.com ([2607:f8b0:4864:20::62e]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rsVvB-000gJh-VX for pgsql-hackers@lists.postgresql.org; Thu, 04 Apr 2024 22:51:06 +0000 Received: by mail-pl1-x62e.google.com with SMTP id d9443c01a7336-1e2232e30f4so14191625ad.2 for ; Thu, 04 Apr 2024 15:51:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=j-davis-com.20230601.gappssmtp.com; s=20230601; t=1712271060; x=1712875860; darn=lists.postgresql.org; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:cc:to:from:subject:message-id:from:to:cc:subject :date:message-id:reply-to; bh=U6gMP6F9u6Q34J2I67qdpt28yJqXSs9urE4H5Yj2cFs=; b=dN0dyu8oHJvnuE4AyaIw6v57cm0WzoFE6RzvvdpJwV+fCps9vn+Wo/j3iS5uQe6tIc VFrl5JPWF3zMHVoTN02x7hmV7oW5IJi47joVgPtG07gZvtIDqxxG6rGA03aR7xrgK9j7 oVs3bjFvhZWke/xgOAuYS/XDy+VUAP8LeHimifKauZwTtSdLpeZ3BLDqu2XnsmWtfKWE yfEL25qRsMdC3JmZceKPYiggx2RoSt1BD+O5A8bkKFswxqpDZUV/JTGZxUMZ34IfMkeI 0I6QTTvfjcm3Rh7vlljS2HulA7EUFm2z5EDXiuomT0+FnY95yCIHAjbZg3WZ/NHBCyCc YsKg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712271060; x=1712875860; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:cc:to:from:subject:message-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=U6gMP6F9u6Q34J2I67qdpt28yJqXSs9urE4H5Yj2cFs=; b=epDkGz3VdsdkUK1S2MoNTj7nMxRnZUiAmpE8avv9JVKcjYNcv35ByCqwHNSpM6JNHH Qp/J59UIPPJz3cgg+QswwDIFrQCot0Qbuu2IseTLsLLoD8IVZCI+6SoTeefYehmrJ906 eGNenDEukbQyb6E1J3Aamw91XbolHPLibJM5+kscvmFUlXE7gnhamGcjTdgYKDAOaMPQ D1hEa3LhRVuJOOyVy/k0DUOXsuz5iLjYfB4+/4wTSO78g1K/+rjjKbQxOAqJFu6l40u3 F7euHJIJvn8y9F+GC1hai4MEfZoZYx5PqZoqRgmI9PoHRoDd0ohAl1Exo2U6zsOt+Bf2 9KiA== X-Forwarded-Encrypted: i=1; AJvYcCVPUVdMZlatQ4sw6q6zEmXQCO4TPNZ2dYn59OqPGuf1X+1IrPy71wIvbc+/xuSuNdYivaxUJc9almQKLHTr3CjitX8yMavl1FfDYhgYfSonsW1l X-Gm-Message-State: AOJu0Yw3BGuo8Hz1gBmOWPjKJNLtZ7O9ayhGSr6JQ6FexlTzRejGcDBd EDs0rKmLlWENiC602NMmh78p/E54fc4py7SNvM/YKPuB69eAskfcEE8k6uVenw== X-Google-Smtp-Source: AGHT+IEFFZHlbcaH3hFt5tszBzOs0fA4tXdhdXuBga41mnqRvsEgBCz9UEceNJtl8v4drHUpRnrXUw== X-Received: by 2002:a17:903:40c3:b0:1e1:3300:702b with SMTP id t3-20020a17090340c300b001e13300702bmr999353pld.15.1712271060381; Thu, 04 Apr 2024 15:51:00 -0700 (PDT) Received: from jeff-laptop.lan (c-76-102-242-158.hsd1.ca.comcast.net. [76.102.242.158]) by smtp.gmail.com with ESMTPSA id e2-20020a170902f1c200b001e2a4497956sm161163plc.90.2024.04.04.15.50.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Apr 2024 15:50:59 -0700 (PDT) Message-ID: <5e3a2a809388bc01b66bf7fa692eaac5622acc0a.camel@j-davis.com> Subject: Re: LogwrtResult contended spinlock From: Jeff Davis To: Alvaro Herrera Cc: Bharath Rupireddy , Tom Lane , Andres Freund , pgsql-hackers@lists.postgresql.org, Jaime Casanova Date: Thu, 04 Apr 2024 15:50:58 -0700 In-Reply-To: <202404041745.jzrrw3ffyac7@alvherre.pgsql> References: <202404041745.jzrrw3ffyac7@alvherre.pgsql> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.44.4-0ubuntu2 MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Thu, 2024-04-04 at 19:45 +0200, Alvaro Herrera wrote: > 1. Using pg_atomic_write_membarrier_u64 is useless and it imposes > mora > barriers than we actually need.=C2=A0 So I switched back to > pg_atomic_write_u64 and add one barrier between the two writes.=C2=A0 Sam= e > for reads. +1. This looks correct to me. Just before the writes there's a spinlock, which acts as a full barrier; and just afterwards, the function returns and the WALWriteLock is released, again acting as a full barrier. The write barrier in between enforces the Write >=3D Flush invariant. > 2. Using monotonic_advance for Write and Flush is useless. +1. > 3. Testing the invariant that the Copy pointer cannot be 0 is > useless, > because we initialize that pointer to EndOfLog during StartupXLOG. > So, removed. +1. > 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.=C2=A0 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. > =C2=A0 (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. > =C2=A0 Please argue why we need this patch. I'm not sure what you mean by "this patch"? > 5. The existing weird useless-looking block at the end of XLogWrite > is > there because we used to have it to declare a volatile pointer to > XLogCtl (cf.=C2=A0 commit 6ba4ecbf477e).=C2=A0 That's no longer needed, s= o we > could remove it.=C2=A0 Or we could leave it alone (just because it's > ancient > and it doesn't hurt anything), but there's no reason to have the new > invariant-testing block inside the same block.=C2=A0 So I added another > weird > useless-looking block, except that this one does have two variable > declaration at its top. That didn't bother me, but it could be cleaned up a bit in a later patch. > 6. In a few places, we read both Write and Flush to only use one of > them.=C2=A0 This is wasteful and we could dial this back to reading only > the > one we need.=C2=A0 Andres suggested as much in [1].=C2=A0 I didn't touch = this > in > the current patch, and I don't necessarily think we need to address > it > right now.=C2=A0 Addressing this should probably done similar to what I > posted in [2]'s 0002. I agree that it should be a separate patch. I haven't thought about the consequences of making them fully independent -- I think that means we give up the invariant that Copy >=3D Write >=3D Flush? Regarding the patches themselves, 0001 looks good to me. For 0002, did you consider having pg_atomic_monotonic_advance_u64() return the currval? =09 Regards, Jeff Davis [1] https://www.postgresql.org/message-id/CALj2ACV6rS%2B7iZx5%2BoAvyXJaN4AG-djA= QeM1mrM%3DYSDkVrUs7g%40mail.gmail.com [2] https://www.postgresql.org/message-id/20230125211540.zylu74dj2uuh3k7w%40awo= rk3.anarazel.de [3] https://www.postgresql.org/message-id/CALj2ACW65mqn6Ukv57SqDTMzAJgd1N_AdQtD= gy%2BgMDqu6v618Q%40mail.gmail.com