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 1tqbkS-007evl-Fd for pgsql-hackers@arkaria.postgresql.org; Fri, 07 Mar 2025 17:44:36 +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 1tqbkR-00DM3F-5k for pgsql-hackers@arkaria.postgresql.org; Fri, 07 Mar 2025 17:44:35 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tqbkQ-00DM0t-Rm for pgsql-hackers@lists.postgresql.org; Fri, 07 Mar 2025 17:44:34 +0000 Received: from mail-ed1-x534.google.com ([2a00:1450:4864:20::534]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tqbkP-001XCX-09 for pgsql-hackers@postgresql.org; Fri, 07 Mar 2025 17:44:33 +0000 Received: by mail-ed1-x534.google.com with SMTP id 4fb4d7f45d1cf-5e5dce099f4so2361119a12.1 for ; Fri, 07 Mar 2025 09:44:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1741369472; x=1741974272; darn=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=RI666cnv9LydF0UKwQKON2BZOoKs9Tr6cFfWAU8SKnI=; b=agBkC1VuQ5Ggghrk6J9Ba43yadL+jtyw4Jdq46yaHaZDr4G2aRjrDw+S9mCly3pH2Y 5+dRjIQDTldb21aSnU/CtfsfyrqwWqIS4JKNaUU8n4FzKVnGN2ULo36rZOe8op+s5kVg +PqZ4bM6TTOLMyRd+T4RQsArBfJrkB+tEddBYiYybYy9tU2z2egiBSepHZ07YN15hCSN 6sCPN/FGKwa5ghGuGIK0XaV0R7io2m7+Lonqa8HQlHTszv6/4uszM7w0T4xfHHLqAZxz BfU82Bz6ismezvB0R3UNFysOqiEGUQslRdG1C9ppx0kv3LS9OYSZnwdxXlDlRO0cA6TT lWcA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741369472; x=1741974272; 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=RI666cnv9LydF0UKwQKON2BZOoKs9Tr6cFfWAU8SKnI=; b=EX3NiFMc6mR1/K66LR2j1BR5LV3EfXhZu2IQhOSWo0unH1IlhQEJg1W+w3vtEutzpt ZcHakKEGY7Ex6uhJC3YkpVgjFc8uwWyH+C+F/YXYlO7CIAuD/szSGKOnKCPaenSfOiyy hPCxndvCxlOlO4yQwxcM65PEw0DAPgCC41FyeldbOI7h8YegYNsxU5kccbdA4H5sHr8i fCrazM6AbxZess50ftrbqV/j3N8F53GOZmOQzD4lBdQuMgu/uPUk+CK7klVMzkkHp5pX v+EAUq6oNSEeMHk0ej+HXP6A2FG12TkUf+S2dIBnfNjg5fBEA+DE3S/+g6apyp9mANCW 78UQ== X-Gm-Message-State: AOJu0YzT2Rn85RBoy+ZTSJSVcBBzR1LtvWGNKQRJWtkiiGk1ofQ7rPZw k+pdjMl6zw+9yTSOKx7/wh+fh7Ldfyg8YAa5h6G4C4CnAZYne9WUyCRMN2Fwr4NtwRqwWR/50gM EiyE6W3nXEu76s2lr7qnF4Xl5uwU= X-Gm-Gg: ASbGncuyjwlcHpgRz8/H/dATMVhW91jij9/2yOyoDj5tokFsrAhQNpEywKmroOGi9TW bgVQDLszL8kD19ghVcNT5yAv+Xq4+Ri68GYQwaU8zYKy3GCeJyiXC51yyJ4hK+GbVUj2MF1Mdw9 DAmaN0rBsLTt5ppjasr45ArZEVRA== X-Google-Smtp-Source: AGHT+IFmRBtlIJP2QOE+PRyKDsXEwz4oSXha9OsPblFiSIUvxCFdcEfTg5krUG2qjkK0RYREjX6K+6nMkryApK3xP8I= X-Received: by 2002:a17:907:9703:b0:ac2:ad1:221b with SMTP id a640c23a62f3a-ac2525de582mr434173766b.6.1741369471475; Fri, 07 Mar 2025 09:44:31 -0800 (PST) MIME-Version: 1.0 References: <2muwyx6a5vojkg7iegknhnkcch3lfxptsxk7icwuh7szkvvu2y@vc3ukkfvnu6i> In-Reply-To: From: Alexander Korotkov Date: Fri, 7 Mar 2025 19:44:20 +0200 X-Gm-Features: AQ5f1JoYDka6nM2r6YblGTUGpM7T5zyTji0-OUjH97RSfJo8Qcf3gA-Az8aJg24 Message-ID: Subject: Re: pg_atomic_compare_exchange_*() and memory barriers To: Andres Freund Cc: pgsql-hackers 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, Mar 7, 2025 at 7:38=E2=80=AFPM Andres Freund w= rote: > On 2025-03-07 19:15:23 +0200, Alexander Korotkov wrote: > > On Fri, Mar 7, 2025 at 7:07=E2=80=AFPM Andres Freund wrote: > > > What is the access pattern and the observed problems with it that mad= e you > > > look at the disassembly? > > > > Check this code. > > > > l1: pg_atomic_write_u64(&XLogCtl->xlblocks[nextidx], NewPageEndPtr)= ; > > > /* > > * Try to advance XLogCtl->InitializedUpTo. > > * > > * If the CAS operation failed, then some of previous pages are= not > > * initialized yet, and this backend gives up. > > * > > * Since initializer of next page might give up on advancing of > > * InitializedUpTo, this backend have to attempt advancing unti= l it > > * find page "in the past" or concurrent backend succeeded at > > * advancing. When we finish advancing XLogCtl->InitializedUpT= o, we > > * notify all the waiters with XLogCtl->InitializedUpToCondVar. > > */ > > l2: while (pg_atomic_compare_exchange_u64(&XLogCtl->InitializedUpTo= , > > &NewPageBeginPtr, NewPageEndPtr)) > > { > > NewPageBeginPtr =3D NewPageEndPtr; > > NewPageEndPtr =3D NewPageBeginPtr + XLOG_BLCKSZ; > > nextidx =3D XLogRecPtrToBufIdx(NewPageBeginPtr); > > > > l3: if (pg_atomic_read_u64(&XLogCtl->xlblocks[nextidx]) !=3D > > NewPageEndPtr) > > { > > /* > > * Page at nextidx wasn't initialized yet, so we cann't= move > > * InitializedUpto further. It will be moved by backend > > which > > * will initialize nextidx. > > */ > > > > ConditionVariableBroadcast(&XLogCtl->InitializedUpToCondVar); > > break; > > } > > } > > > > Consider the following execution order with process 1 (p1) and process = 2 > > (p2). > > On 2025-03-07 19:24:39 +0200, Alexander Korotkov wrote: > > Sorry, I messed this up. > > The correct sequence is following. > > > > 1. p1 executes l1 > > 2. p1 executes l2 with failure > > 3. p2 executes l2 with success > > 4. p2 execute l3, but doesn't see the results of step 1, because 3 > > didn't provide enough of memory barrier > > Did you mean because 2) didn't provide enough of a memory barrier? Becaus= e 3) > does, right? Yes, exactly. > You could get in exactly same the situation if the p1 is scheduled out by= the > OS after step 1, no? No. In that case, p1 will execute l2 with success. p1 executes l2 with failure only because it goes before p2 executes l2. ------ Regards, Alexander Korotkov Supabase