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 1qVZcb-000bmb-As for pgsql-hackers@arkaria.postgresql.org; Mon, 14 Aug 2023 15:36:45 +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 1qVZcZ-005jT9-EN for pgsql-hackers@arkaria.postgresql.org; Mon, 14 Aug 2023 15:36:43 +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 1qVZcZ-005jSt-4H for pgsql-hackers@lists.postgresql.org; Mon, 14 Aug 2023 15:36:43 +0000 Received: from mail-oa1-x32.google.com ([2001:4860:4864:20::32]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qVZcV-0007Ia-8q for pgsql-hackers@postgresql.org; Mon, 14 Aug 2023 15:36:42 +0000 Received: by mail-oa1-x32.google.com with SMTP id 586e51a60fabf-1c4f47810c6so472079fac.0 for ; Mon, 14 Aug 2023 08:36:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1692027397; x=1692632197; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=lRUKpuAogZR/LAEoPv+3npFZVIVbe34LcHy1FB+OLho=; b=WGH0r5lVF7OmTgMa2ejCKvc017NtUYQjLVjCustD0nyBqSQb12PKfRwxyxPqdn2mdm f5LK5Wa9ZuuQimK+u+nRgwrE/GbD8EAVFvOqAUUeeCM5WTMq1IJ/IBAcgY+DTJb14SQ8 kjFnWp2VDuf7iW7QaV7sRS+R9aMgQF1zCCH/ba85a84/5ZTLK2Lki9z2tmrUUyXjlnig Re6kANDALowVKikCTIe15ORHXd71mV1MNe69azjkCaFW6+LgLMZE6RLuOvWOU33Rnug6 hOWRjfI9YxbU/7CY8mfqUckS8DjlP8/O6Rv6vub7vt4b6C+884FfIPki/C38cRQV1Lz3 YUfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692027397; x=1692632197; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=lRUKpuAogZR/LAEoPv+3npFZVIVbe34LcHy1FB+OLho=; b=ARV9YKcxLUFWtl6a393KlClDe9aI7kFCJcWbXzkVIH9ivtk45pQ3cge2a5m0l6lET7 q9FMgfOPz0/tH5F+iCzBNeqyCtbSDD7OL+kSQM7qYQaBOf3cpqvA+yHbDQtce2tC5Drb fUBfuqg/RQdh7fsohlxEbLfAE8yYQgKofGumqjpHHtKB2ZRMkehpzF8DWxbJH9cBtvrY b4L9uQB+2omZ2RCtECFL4aR54bEyn0/0UnZUXMrElk+XQ66YnZ4JR5/FdG6kgCTI8jFa uEVmNWoPgk5+dE6t14rFMbmUX1xEDH+pDxFLsjSGDzJkvxBzniaNSxGwwBnlWC7/p84o 82rA== X-Gm-Message-State: AOJu0Yznxv+pWmQCITXCALqbNSwa9bcxBGUrfg403OyHjPasNbToPIjj k5/Zwq/LrQuDrG4fNRaTFSjJM+1QR1E= X-Google-Smtp-Source: AGHT+IHj70ihwJozpM9X7iQRKhtxfSqsDSgzK6JkI+ccXoJxDVdpQl+g7JSvZzZPcf+ZAAhyZUXCsQ== X-Received: by 2002:a05:6870:9a1e:b0:1bb:c946:b80e with SMTP id fo30-20020a0568709a1e00b001bbc946b80emr8207079oab.28.1692027397332; Mon, 14 Aug 2023 08:36:37 -0700 (PDT) Received: from nathanxps13 ([2600:1700:48f8:340f:975:5f03:fc24:ff6c]) by smtp.gmail.com with ESMTPSA id y36-20020a4a9827000000b0056cf08062adsm4829921ooi.7.2023.08.14.08.36.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Aug 2023 08:36:36 -0700 (PDT) Date: Mon, 14 Aug 2023 08:36:34 -0700 From: Nathan Bossart To: Michail Nikolaev Cc: PostgreSQL Hackers , Tom Lane Subject: Re: Replace known_assigned_xids_lck by memory barrier Message-ID: <20230814153634.GB1395427@nathanxps13> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Sun, Mar 19, 2023 at 12:43:43PM +0300, Michail Nikolaev wrote: > In a nutshell: KnownAssignedXids as well as the head/tail pointers are > modified only by the startup process, so spinlock is used to ensure > that updates of the array and head/tail pointers are seen in a correct > order. It is enough to pass the barrier after writing to the array > (but before updating the pointers) to achieve the same result. What sort of benefits do you see from this patch? It might be worthwhile in itself to remove spinlocks when possible, but IME it's much easier to justify such changes when there is a tangible benefit we can point to. /* - * Now update the head pointer. We use a spinlock to protect this + * Now update the head pointer. We use a memory barrier to protect this * pointer, not because the update is likely to be non-atomic, but to * ensure that other processors see the above array updates before they * see the head pointer change. * * If we're holding ProcArrayLock exclusively, there's no need to take the - * spinlock. + * barrier. */ Are the assignments in question guaranteed to be atomic? IIUC we assume that aligned 4-byte loads/stores are atomic, so we should be okay as long as we aren't handling anything larger. - SpinLockAcquire(&pArray->known_assigned_xids_lck); + pg_write_barrier(); pArray->headKnownAssignedXids = head; - SpinLockRelease(&pArray->known_assigned_xids_lck); This use of pg_write_barrier() looks correct to me, but don't we need corresponding read barriers wherever we obtain the pointers? FWIW I tend to review src/backend/storage/lmgr/README.barrier in its entirety whenever I deal with this stuff. -- Nathan Bossart Amazon Web Services: https://aws.amazon.com