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 1r06IJ-003JUJ-Oc for pgsql-hackers@arkaria.postgresql.org; Mon, 06 Nov 2023 20:33:59 +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 1r06II-001JVb-4Q for pgsql-hackers@arkaria.postgresql.org; Mon, 06 Nov 2023 20:33:58 +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 1r06IH-001JVS-Qi for pgsql-hackers@lists.postgresql.org; Mon, 06 Nov 2023 20:33:57 +0000 Received: from mail-il1-x131.google.com ([2607:f8b0:4864:20::131]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1r06IE-0053dg-Vh for pgsql-hackers@postgresql.org; Mon, 06 Nov 2023 20:33:57 +0000 Received: by mail-il1-x131.google.com with SMTP id e9e14a558f8ab-35957d77afcso10050045ab.0 for ; Mon, 06 Nov 2023 12:33:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1699302833; x=1699907633; darn=postgresql.org; 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=dkQvzYMcr290+ICe6Wl52fy77939dJgqmqJ/7zkW5/Q=; b=YBFvS6G5CSTEEiNngCJ8p6SGBrYCafbZlsnFCnk5+i5ncaAgG3TixxcLbkJm+y01Qu ilY9khTw4kmoQM2wE4GqwdCcGdwcbKL9DKiQkzLmUrU6UIiPs1xEZ9dS7a6r/gLKrjak HvTWM1PK4wBu9lZkhYjThC/9qJWdL7omLxhUAb7IhnAXakWKWqyE6s3yDvmyvNbavM6Z /n81qJEjIYSJGzHNZy2hFjxRL6sxSgnLkQPLFOXWpgsqVJYdXmSDCsfMdydfVwVEOWYq H61iBecRLj6nNd38ZTlj7O89fDhRuINyrFz3vHOwyTo9TTf8bxva7qH6n2BbfydjlR3q 5IuA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699302833; x=1699907633; 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=dkQvzYMcr290+ICe6Wl52fy77939dJgqmqJ/7zkW5/Q=; b=mKKhEVchyyJgLWsE9y06H35VyFEho4A+CdfE2ilnHKJFPVg/o4cUp1BowjI/pGL58C tk9p0dw7bGa5/b5XNLca3hr3i+Dz557IHxtfyhHws/q6d2lbSGP1i/3c2VvqtwdTcidV 5FOgYQ/IAtNLIZcl+0diR0Fe4q9Q2fy3yzBlgVdOi881k6+73jF1vFeeTtlsDbN4mukt r55Sv0xxJQJPZanxEdY06FxEFKDFwk0ZsAsNvNtqRJKBYm3F8NhxVvhhzwp50cZl/gm8 zZgpl9njmYRj7C80xu48o+FpS+E3LU0cqBap4q8YXRDek/biiA/4Wfsbpn4U2j68aU+U p4gQ== X-Gm-Message-State: AOJu0YwNx8FTbacJhQi16U2cPN/K98gi9B2elCtjfeDLkaUunW79RbVR VZ5LCB+oHeII9qE7JpWnowo= X-Google-Smtp-Source: AGHT+IEY/fZF81c/2I88+PTGJrLKJ6KclJ9k3ZpJDtOEK9Xy1+oDQS6Jzamp7drLHHrIoTdodj6J/w== X-Received: by 2002:a92:d189:0:b0:359:3ff:17c6 with SMTP id z9-20020a92d189000000b0035903ff17c6mr635957ilz.4.1699302832717; Mon, 06 Nov 2023 12:33:52 -0800 (PST) Received: from nathanxps13 (162-195-168-172.lightspeed.stlsmo.sbcglobal.net. [162.195.168.172]) by smtp.gmail.com with ESMTPSA id e43-20020a02862e000000b0046400bb3462sm1474311jai.53.2023.11.06.12.33.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Nov 2023 12:33:52 -0800 (PST) Date: Mon, 6 Nov 2023 14:33:50 -0600 From: Nathan Bossart To: John Morris Cc: Bharath Rupireddy , Andres Freund , Stephen Frost , Michael Paquier , Robert Haas , "pgsql-hackers@postgresql.org" Subject: Re: Atomic ops for unlogged LSN Message-ID: <20231106203350.GA598180@nathanxps13> References: <20231026203433.GA1088329@nathanxps13> <20231102034006.GA85609@nathanxps13> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="5mCyUwZo2JvN/JJP" Content-Disposition: inline In-Reply-To: <20231102034006.GA85609@nathanxps13> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --5mCyUwZo2JvN/JJP Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Wed, Nov 01, 2023 at 10:40:06PM -0500, Nathan Bossart wrote: > Since this isn't a tremendously performance-sensitive area, IMHO we should > code defensively to eliminate any doubts about correctness and to make it > easier to reason about. Concretely, like this. -- Nathan Bossart Amazon Web Services: https://aws.amazon.com --5mCyUwZo2JvN/JJP Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="unlogged_lsn_v4.patch" diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index b541be8eec..1068f96c2d 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -469,9 +469,8 @@ typedef struct XLogCtlData XLogSegNo lastRemovedSegNo; /* latest removed/recycled XLOG segment */ - /* Fake LSN counter, for unlogged relations. Protected by ulsn_lck. */ - XLogRecPtr unloggedLSN; - slock_t ulsn_lck; + /* Fake LSN counter, for unlogged relations. */ + pg_atomic_uint64 unloggedLSN; /* Time and LSN of last xlog segment switch. Protected by WALWriteLock. */ pg_time_t lastSegSwitchTime; @@ -4294,14 +4293,7 @@ DataChecksumsEnabled(void) XLogRecPtr GetFakeLSNForUnloggedRel(void) { - XLogRecPtr nextUnloggedLSN; - - /* increment the unloggedLSN counter, need SpinLock */ - SpinLockAcquire(&XLogCtl->ulsn_lck); - nextUnloggedLSN = XLogCtl->unloggedLSN++; - SpinLockRelease(&XLogCtl->ulsn_lck); - - return nextUnloggedLSN; + return pg_atomic_fetch_add_u64(&XLogCtl->unloggedLSN, 1); } /* @@ -4714,7 +4706,7 @@ XLOGShmemInit(void) SpinLockInit(&XLogCtl->Insert.insertpos_lck); SpinLockInit(&XLogCtl->info_lck); - SpinLockInit(&XLogCtl->ulsn_lck); + pg_atomic_init_u64(&XLogCtl->unloggedLSN, 0); } /* @@ -5319,9 +5311,9 @@ StartupXLOG(void) * the unlogged LSN counter can be reset too. */ if (ControlFile->state == DB_SHUTDOWNED) - XLogCtl->unloggedLSN = ControlFile->unloggedLSN; + pg_atomic_write_u64(&XLogCtl->unloggedLSN, ControlFile->unloggedLSN); else - XLogCtl->unloggedLSN = FirstNormalUnloggedLSN; + pg_atomic_write_u64(&XLogCtl->unloggedLSN, FirstNormalUnloggedLSN); /* * Copy any missing timeline history files between 'now' and the recovery @@ -6902,10 +6894,12 @@ CreateCheckPoint(int flags) * Persist unloggedLSN value. It's reset on crash recovery, so this goes * unused on non-shutdown checkpoints, but seems useful to store it always * for debugging purposes. + * + * pg_atomic_read_u64() isn't guaranteed to return the most up-to-date + * value, so this is implemented via a compare/exchange with 0 to ensure + * the necessary cache coherency interactions. */ - SpinLockAcquire(&XLogCtl->ulsn_lck); - ControlFile->unloggedLSN = XLogCtl->unloggedLSN; - SpinLockRelease(&XLogCtl->ulsn_lck); + pg_atomic_compare_exchange_u64(&XLogCtl->unloggedLSN, &ControlFile->unloggedLSN, 0); UpdateControlFile(); LWLockRelease(ControlFileLock); --5mCyUwZo2JvN/JJP--