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 1rdF3X-004Ze3-ET for pgsql-hackers@arkaria.postgresql.org; Thu, 22 Feb 2024 19:48:31 +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 1rdF3V-00Dcud-1A for pgsql-hackers@arkaria.postgresql.org; Thu, 22 Feb 2024 19:48:29 +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 1rdF3U-00DcuU-Jc for pgsql-hackers@lists.postgresql.org; Thu, 22 Feb 2024 19:48:29 +0000 Received: from mail-pl1-x62d.google.com ([2607:f8b0:4864:20::62d]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rdF3R-000Nxz-W9 for pgsql-hackers@lists.postgresql.org; Thu, 22 Feb 2024 19:48:28 +0000 Received: by mail-pl1-x62d.google.com with SMTP id d9443c01a7336-1d7431e702dso1710245ad.1 for ; Thu, 22 Feb 2024 11:48:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=j-davis-com.20230601.gappssmtp.com; s=20230601; t=1708631304; x=1709236104; 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=l5Su0gTgipEafPNamliGhUBDNG8Smme1tly0U1WtA80=; b=chYwXjCTr1WhtstECSzoRdIr6q2Y62OkA4fsB5tlSY2XIsD1Pq/1ZNy0WnqyR+Ef5k Y4mAcQpDBHd4ii4W7muwwuHorv0Y/saFAx80+tBj0FVK9yQ7oTjPmg7guslDwvpy/59D jfSieiXeSCmQJpXkxfSCBiAXMxUF2sR0GsMNUmYrDWg3STlTU6SjDdgV9UyqWOd6lcqj UA0mSjQGoBXfO37Rycoo18HcGvLdnyfsyrRkiRoL3U7CWobTRBcHyMq7GA7NvZyY3sG5 LKBqFPXFe3+dAqOfXZLUS89lwH281Jqy/3+H0XbM0g5oGKtHVbNu7uoRaEGKJaCIrk3G FRRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708631304; x=1709236104; 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=l5Su0gTgipEafPNamliGhUBDNG8Smme1tly0U1WtA80=; b=dX3NoYjdwgQskT4f+7eM4DzRhXob3AMbE2e2Bhl3/BjsGg0cc3uFXxauPjWDkL526R H/qAiNgcNLDGUiYQIwOLhLgz+5pt59L4BNPJZmzgLG8LGqv2JQmxOWIIvZxbh3PnQLTW 7JFBPr5o9x35eImsC7H5Cub5OAlI177hagjum9CYj7Ed1B+BGdW6wde1zaf+2pDfUIxX 35Ux37wgNXbzQopJ/fItwyK8MGIEOLjwyZdQ8iuPftDdrmoZBxWm/WqcZDW+dwFKasrr C0ezJob0AvlmjCJ4PRPEtmNz7EJa8EvwNkpLIA+Iq76P/8cVD0dAPUE5LKwptFlJzv1C foIQ== X-Forwarded-Encrypted: i=1; AJvYcCWvCIj99wNzBmmfVo6kcQiv8jzE5rzNAxvkytxkONrfJhUIJJTfFFyg4Uk+hb1keTqUEF0Ql3yxiXrPJr4VKoW7VQ/Jc5LVt2onhk9vXuwwV//T X-Gm-Message-State: AOJu0YxlAbeok8+aKVkmXf/wLDzxywApNXMTcOo1KAhn8gMuYWLto+xA pASELBoUMJwotXaiFaRVkxDbMs9e8rFW8QAjKEgq/BT3UdPDa46UbQrCY2/pwM0shDspU0j2CBk = X-Google-Smtp-Source: AGHT+IEXumArstoMkjZi9+ilQvFkrYfPg0gmkblg76LBdZh9Ffwr0xk5bPJLnBEYlCHyBWY37HJ/KA== X-Received: by 2002:a17:902:64cf:b0:1dc:4adb:9c1 with SMTP id y15-20020a17090264cf00b001dc4adb09c1mr2686041pli.0.1708631303779; Thu, 22 Feb 2024 11:48:23 -0800 (PST) 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 j6-20020a17090276c600b001da1ecb05f9sm10270100plt.240.2024.02.22.11.48.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Feb 2024 11:48:23 -0800 (PST) Message-ID: Subject: Re: LogwrtResult contended spinlock From: Jeff Davis To: Robert Haas Cc: Bharath Rupireddy , Alvaro Herrera , Tom Lane , Andres Freund , pgsql-hackers@lists.postgresql.org, Jaime Casanova Date: Thu, 22 Feb 2024 11:48:22 -0800 In-Reply-To: References: <20220923084941.g3756xqz6lwnqwso@alvherre.pgsql> <727449f3151c6b9ab76ba706fa4d30bf7b03ad4f.camel@j-davis.com> 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-02-22 at 10:17 +0530, Robert Haas wrote: > I think the problems tend to be worst when you have some bit of data > that's being frequently modified by multiple backends. Every backend > that wants to modify the value needs to steal the cache line, and > eventually you spend most of your CPU time stealing cache lines from > other sockets and not much of it doing any actual work. If you have a > value that's just being read by a lot of backends without > modification, I think the cache line can be shared in read only mode > by all the CPUs and it's not too bad. That makes sense. I guess they'd be on the same cache line as well, which means a write to either will invalidate both. Some places (XLogWrite, XLogInsertRecord, XLogSetAsyncXactLSN, GetFlushRecPtr, GetXLogWriteRecPtr) already update it from shared memory anyway, so those are non-issues. The potential problem areas (unless I missed something) are: * AdvanceXLInsertBuffer reads it as an early check to see if a buffer is already written out. * XLogFlush / XLogNeedsFlush use it for an early return * WALReadFromBuffers reads it for an error check (currently an Assert, but we might want to make that an elog). * We are discussing adding a Copy pointer, which would be advanced by WaitXLogInsertionsToFinish(), and if we do replication-before-flush, we may want to be more eager about advancing it. That could cause an issue, as well. I don't see the global non-shared variable as a huge problem, so if it serves a purpose then I'm fine keeping it. Perhaps we could make it a bit safer by using some wrapper functions. Something like: bool IsWriteRecPtrAtLeast(XLogRecPtr recptr) { XLogRecPtr writeptr; if (LogwrtResult.Write >=3D recptr) return true; writeptr =3D GetXLogWriteRecPtr(); return (writeptr >=3D recptr); } That would reduce the number of direct references to LogwrtResult, callers would never see a stale value, and would avoid the cache miss problem that you're concerned about. Not sure if they'd need to be inline or not. Regards, Jeff Davis