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.96) (envelope-from ) id 1vZFuz-006mFF-33 for pgsql-general@arkaria.postgresql.org; Fri, 26 Dec 2025 22:04:18 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vZFuy-00AVQm-2W for pgsql-general@arkaria.postgresql.org; Fri, 26 Dec 2025 22:04:17 +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.96) (envelope-from ) id 1vYN0q-004VOj-21 for pgsql-general@lists.postgresql.org; Wed, 24 Dec 2025 11:26:41 +0000 Received: from mail-qk1-x736.google.com ([2607:f8b0:4864:20::736]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vYN0o-002Vmo-37 for pgsql-general@postgresql.org; Wed, 24 Dec 2025 11:26:41 +0000 Received: by mail-qk1-x736.google.com with SMTP id af79cd13be357-8b144ec3aa8so607346785a.2 for ; Wed, 24 Dec 2025 03:26:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1766575597; x=1767180397; darn=postgresql.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=mfSUcMsIcUqym1xzSrQ2M35ynic7ZZ1Gh1NQ1sahlKY=; b=Pt73Y9lpgCMIwlgerK/v7/WylpiVcFCcijJNYhtogKEp4YTBNgAHGJJyGzgwUEcmXv ItOKUnpwdMe6BvyqB0gEg7essFHr6SkBXh/l0ztILB+5arNyXaTWO8ZiovVSMzbkEmYh bTqTnQdF1Cj3yUjo8p+hETucLFkYEm8ZHxl5bmUD+CQiiYsAzdymlqxUZ+4sur6qZux1 fW3QdhjKEsCjgOsPEVtu1JHO9MVkvtJ11lVYkKeKa8XFC+PN2QWwovBT29U5lO+E9PXX qaRvhKpVZdBHBBzycKJ6os/Aa2US9vob7/OgSJrW9WOzl2u+0Po5Oz9BH2biKU/rswHd Kn1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1766575597; x=1767180397; h=to:subject:message-id:date:from:mime-version:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=mfSUcMsIcUqym1xzSrQ2M35ynic7ZZ1Gh1NQ1sahlKY=; b=k8etbi5FfqWtBqLpKDnLBkWtmr20RFbrNQvv0nDnVhkuVzLFVXIDi1BcNcmE4UYTlj VwczrTkJ33OtE8pfiV954Y4YWM+gxJv75d2Eq+gMPGmvv/+ixnSpPOEIAENhM4mWLwoI 1wA/LySfFF6uAOPaukIUIEuf5B/HBP6VWiPuGnkOluN1GV6rP/XeD8OfkVG17ANDUi0F y+4oT5ZcLFwos+UGmpeKh4K9T5r3zN2AZjZfwPZSYXRwvJ2XHJtkRCriRQk2gKHeoBJd i8T4NpUrFZbRECGkUPGtvvBtd0ORlB8ExXZ2HLDp/QIfPcYD4jZtHIigfcELWQgcyNL9 gf7w== X-Gm-Message-State: AOJu0YyLlGdgk0fQFfQqfE+pyZFjrKfdmIfW+VktC/WN3zsbusnY/eYu WeGI3BdYsuz13FOFr+QZYEXRcd4s6OONWsuv2tvkyEkzOAOKX0EGV9jQ9RDIozAJN+gCd3bEnXR 8C5h+g40Jl6qsMLXBSCIwxYve7t1vtgUR2pk5 X-Gm-Gg: AY/fxX5X2/gsAyHXmsXhdtwcSWUeTzEvxRYrKaMapYDvWrOTo0QeV+RbPiMOTm0HvoB zW6SSSo+FdORIY676zvF4IVGSjhfx0oS5qzeR5pQA9WJGRoiYOgCubZGhKhZgk4z6BumdkpIdru U5Q2jrRY1gvNalDmKnZ8lnPBkoUxkJLoofKtFbVgt1jNXE0HU6SS4CIOSvVBz7HxsP8PnyOSBI/ q8IrjOCJJ4PYr6h6E/a0rgxU0J4NMhTnEJ6BFzSNMx/trivMMaRke1EG09yCSe7Lw5lsScQo13x GSairI2e3V8Is0ck6LWH1G6cckNfmMpkp2W9LrE3 X-Google-Smtp-Source: AGHT+IFv/MIs9FcxpZC3Um/JbJFFHXyyFoV9RsxrqkiwHc3R48wCUpx96Xpxph8xrh208y+URbLzBUQ6K4Qh5sSpRP8= X-Received: by 2002:a05:620a:44ca:b0:89f:5f63:68eb with SMTP id af79cd13be357-8c08f65121bmr2477453985a.13.1766575597089; Wed, 24 Dec 2025 03:26:37 -0800 (PST) MIME-Version: 1.0 From: Ian Dick Date: Wed, 24 Dec 2025 22:26:26 +1100 X-Gm-Features: AQt7F2p8ljy9IvUbUcoMcsaXsHwEQSgbQzNOF22dMdobTJuf9AQZd1QolWoErOs Message-ID: Subject: Some questions about CREATE INDEX CONCURRENTLY and pg_advisory_lock To: pgsql-general@postgresql.org Content-Type: multipart/alternative; boundary="000000000000eef64d0646b0ee1d" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000eef64d0646b0ee1d Content-Type: text/plain; charset="UTF-8" I recently noticed some behaviour regarding the CREATE INDEX CONCURRENTLY operation, combined with pg_advisory_lock, which I found surprising. I've looked online for a specific explanation of what's happening, but couldn't seem to find a concrete explanation for what I'm seeing. Here are the steps to reproduce: # from the first terminal $ docker run --name some-postgres -e POSTGRES_PASSWORD=password -p 5432:5432 -d postgres $ psql postgres://postgres:password@localhost:5432/postgres postgres=# CREATE TABLE mytable (mycol varchar(10)); # from the 2nd terminal $ psql postgres://postgres:password@localhost:5432/postgres postgres=# SELECT pg_advisory_lock(1234); # from the 3rd terminal $ psql postgres://postgres:password@localhost:5432/postgres postgres=# SELECT pg_advisory_lock(1234); # from the first terminal postgres=# CREATE INDEX CONCURRENTLY mycol_index ON mytable (mycol); At this point the index creation is blocked on the txn from terminal 3 (the blocked pg_advisory_lock(1234)). To unblock the index creation, you can cancel the blocked txn in terminal 3. According to the docs on concurrent index creation: https://www.postgresql.org/docs/current/sql-createindex.html the 2nd phase of the index creation needs to wait until all current transaction with a snapshot preceding index creation have finished: "After the second scan, the index build must wait for any transactions that have a snapshot (see Chapter 13 ) predating the second scan to terminate" I'm guessing that's where the index creation in my example is getting stuck, and the txn from terminal 3 is flagged by the index creation as a txn with a snapshot predating the index creation. The reason I'm surprised is that the statement from terminal 3 is just trying to obtain an advisory lock, it's not trying to access the table in question, or any table. But regardless, it still seems to block the concurrent index creation. I'm curious to know if this is the expected behaviour. I'm also curious if it might be possible to patch the code to ignore txns like this which should not interfere with the index creation. This is my first time posting in a Postgres mailing list, apologies if I'm missing any of the required details or formatting. Looking forward to learning more about the Postgres internals relevant to this issue. Thanks --000000000000eef64d0646b0ee1d Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
I recently noticed some behaviour regarding=C2=A0the = CREATE INDEX CONCURRENTLY operation, combined with pg_advisory_lock, which = I found surprising. I've looked online for a specific explanation of wh= at's happening, but couldn't seem to find a concrete explanation fo= r what I'm seeing.

Here are the steps to repro= duce:

# from the first terminal
$ docker run = --name some-postgres -e POSTGRES_PASSWORD=3Dpassword -p 5432:5432 -d postgr= es
$ psql postgres://postgres:password@localhost:5432/postgres
postgr= es=3D# CREATE TABLE mytable (mycol varchar(10));

# from the 2nd term= inal
$ psql postgres://postgres:password@localhost:5432/postgres
post= gres=3D# SELECT pg_advisory_lock(1234);

# from the 3rd terminal
$= psql postgres://postgres:password@localhost:5432/postgres
postgres=3D# = SELECT pg_advisory_lock(1234);

# from the first terminal
postgres= =3D# CREATE INDEX CONCURRENTLY mycol_index ON mytable (mycol);

= At this point the index creation is blocked on the txn from terminal 3 (the= blocked pg_advisory_lock(1234)). To unblock the index creation, you can ca= ncel the blocked txn in terminal 3.

According to t= he docs on concurrent index creation:


the 2nd phase of the index creation needs to wait until all current transa= ction with a snapshot preceding index creation have finished:
"After the second scan, the index build must wait for any = transactions that have a snapshot (see Chapter=C2=A013) predating the second scan to t= erminate"

I'm guessing that's where t= he index creation in my example is getting stuck, and the txn from terminal= 3 is flagged by the index creation as a txn with a snapshot predating the = index creation.

The reason I'm surprised is th= at the statement from terminal 3 is just trying to obtain an advisory lock,= it's not trying to access the table in question, or any table. But reg= ardless, it still seems to block the concurrent index creation. I'm cur= ious to know if this is the expected behaviour. I'm also curious if it = might be possible to patch the code to ignore txns like this which should n= ot interfere with the index creation.

This is my f= irst time posting in a Postgres mailing list, apologies if I'm missing = any of the required details or formatting. Looking forward to learning more= about the Postgres internals relevant to this issue.

<= div>Thanks --000000000000eef64d0646b0ee1d--