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 1rtGv1-00Dcju-4O for pgsql-bugs@arkaria.postgresql.org; Sun, 07 Apr 2024 01:01: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 1rtGv0-001NvY-0p for pgsql-bugs@arkaria.postgresql.org; Sun, 07 Apr 2024 01:01: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 1rtGuz-001NuS-Gw for pgsql-bugs@lists.postgresql.org; Sun, 07 Apr 2024 01:01:57 +0000 Received: from mail-yw1-x1129.google.com ([2607:f8b0:4864:20::1129]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rtGuu-001DAm-Jc for pgsql-bugs@lists.postgresql.org; Sun, 07 Apr 2024 01:01:56 +0000 Received: by mail-yw1-x1129.google.com with SMTP id 00721157ae682-617cd7bd929so21488247b3.3 for ; Sat, 06 Apr 2024 18:01:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=leadboat.com; s=google; t=1712451710; x=1713056510; darn=lists.postgresql.org; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:to:from:date:from:to:cc:subject:date:message-id :reply-to; bh=4SUeNLGSer7lNOC1pQfbSYnEfQ3TEK4S8WcdzsKXQBw=; b=Xzk+bcZTBssatf9UDQnm6ZiOPbF98oHdmonIW49KsP1dGRWS4JKSwqUcg+TvSSFpas s55mRe7pbXKRQBYoFAlmlqSR4FIqLy+vfkALd7DYJ+6ICN20dNrzvcWM+HX3fTBkIBJx /+V3Q39YT/Ko84yIj+nVyNtVPbvAEJzvG9h1A= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712451710; x=1713056510; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=4SUeNLGSer7lNOC1pQfbSYnEfQ3TEK4S8WcdzsKXQBw=; b=enKqw4mlH0nc2SP7rlaKrx8X1tUXM5w6k5jm19SsDX1ch3YMIhC/3g7B7WYH0kIlvX q2Kg89B7537bBygCFR6+hfPblMXb7GtrVbyyxs2YKjMhfJlINuwx7K0Qn4Szxr/APjiw VvvEkPDq75tjeUGy8U/AVBAS+qX8UJ/qF7eFWxPuFRkBDMbcSSVUlM2ghF+LAwrB6zaM 5fPKLP6pPAbVIiyL6lauQMwgMCAQjJfbLgEaM4dCYDzhIE1TMC2v783y0XTX/nUmwKTw Eefsiq4ANCJrS5qf8PzGUFR/gzWNsVPACXrLjrq4aTHSvilfNcBc027V7LSztMcVAoAc poXg== X-Forwarded-Encrypted: i=1; AJvYcCX/UMoDTEg6czLNoYFjBUgvpCHczeNU2sqd32xdWQKwqHzOxG/QeZsEsmx/TjxfKBqQ6u8z6KVgQj9fXcaai13CPIJq7HaeIfHlDh01QHXh X-Gm-Message-State: AOJu0YyYumiSySnSJA5xMYPJdthEZKnnzu/MYmczqhQCxA1+B4OXTBP3 KkZy2qyGqBhh1c/4obQDn394LdmMnAPoZHan/g/bGCbBtyLuv+YcyWERWytbVQ== X-Google-Smtp-Source: AGHT+IE+D6w6OEq+vvxq96IArFUUFvnqHycCYPf5Ztt7evwC8JBahjKLRZ4egE9P7MtlTl/+D2/7+g== X-Received: by 2002:a25:ec08:0:b0:dcc:467c:cecb with SMTP id j8-20020a25ec08000000b00dcc467ccecbmr4166806ybh.37.1712451710622; Sat, 06 Apr 2024 18:01:50 -0700 (PDT) Received: from rfd.leadboat.com ([2600:1702:a20:5750::48]) by smtp.gmail.com with ESMTPSA id fs1-20020a0569020c0100b00dc73705ec59sm870777ybb.0.2024.04.06.18.01.49 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 06 Apr 2024 18:01:50 -0700 (PDT) Date: Sat, 6 Apr 2024 18:01:46 -0700 From: Noah Misch To: exclusion@gmail.com, pgsql-bugs@lists.postgresql.org Subject: Re: BUG #18404: Select from pg_stat_activity in a plpgsql block can lead to a global locking issue Message-ID: <20240407010146.9b@rfd.leadboat.com> References: <18404-e0475cbc8837b9b4@postgresql.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <18404-e0475cbc8837b9b4@postgresql.org> User-Agent: Mutt/2.2.12 (2023-09-09) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, Mar 22, 2024 at 01:00:01PM +0000, PG Bug reporting form wrote: > The following bug has been logged on the website: > > Bug reference: 18404 > Logged by: Alexander Lakhin > Email address: exclusion@gmail.com > PostgreSQL version: 16.2 > Operating system: Ubuntu 22.04 > Description: > > The following script: > createuser u > > cat << 'EOF' | psql -U u & > DO ' > BEGIN > PERFORM count(*) FROM pg_stat_activity; > RAISE NOTICE ''sleeping...''; > PERFORM pg_sleep(1800); > END'; > EOF > sleep 0.5 > > cat << EOF | psql & > VACUUM; > REINDEX SYSTEM; > EOF > sleep 0.5 > > cat << EOF | psql -U u > SELECT 1 > EOF > > ends with the last session stuck on startup: > law 3318525 3318511 0 15:00 ? 00:00:00 postgres: u regression > [local] DO > law 3318530 3318511 0 15:00 ? 00:00:00 postgres: law regression > [local] REINDEX waiting > law 3318533 3318511 0 15:00 ? 00:00:00 postgres: u regression > [local] startup waiting > law 3318628 3318511 0 15:01 ? 00:00:00 postgres: autovacuum > worker waiting > law 3318654 3318511 0 15:01 ? 00:00:00 postgres: autovacuum > worker waiting > law 3318676 3318511 0 15:02 ? 00:00:00 postgres: autovacuum > worker waiting > > The backtrace of the last session backend is: [waiting for AccessShareLock on pg_authid_rolname_index] > The backtrace of the REINDEXing backend is: [waiting for AccessExclusiveLock on pg_authid_rolname_index] As a partial workaround, you can set lock_timeout before REINDEX SYSTEM. Each backend is doing the right thing in isolation, under current objectives. The first backend holds AccessShareLock on pg_authid_rolname_index, because pg_stat_activity is a view that joins to pg_authid. We follow https://en.wikipedia.org/wiki/Two-phase_locking#Strong_strict_two-phase_locking and hold relation locks till end of xact. That could be the easiest part to change, but it's still hard. REINDEX acquires AccessExclusiveLock to make trivial the code for making readers transition to the replacement index. Backend startup needs to read catalogs; that is least likely to change. I don't see a general way to keep the rest of the system productive when AccessExclusiveLock of a system relation is happening. I can imagine mostly one-off changes, like making pg_stat_activity release locks earlier.