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 1qwEhh-00AVN8-Vy for pgsql-hackers@arkaria.postgresql.org; Fri, 27 Oct 2023 04:44:14 +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 1qwEhg-00AO4s-Jt for pgsql-hackers@arkaria.postgresql.org; Fri, 27 Oct 2023 04:44:12 +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 1qwEhg-00AO4k-AC for pgsql-hackers@lists.postgresql.org; Fri, 27 Oct 2023 04:44:12 +0000 Received: from mail-yw1-x112c.google.com ([2607:f8b0:4864:20::112c]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qwEhd-0032XA-OF for pgsql-hackers@postgresql.org; Fri, 27 Oct 2023 04:44:11 +0000 Received: by mail-yw1-x112c.google.com with SMTP id 00721157ae682-5a84204e7aeso12459227b3.0 for ; Thu, 26 Oct 2023 21:44:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=leadboat.com; s=google; t=1698381848; x=1698986648; darn=postgresql.org; h=user-agent: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=Ck7QpqkFMVKI6klSqauCxkSlGJ3DRRgQKMXshS/B7o8=; b=cY+U+qtFliM5hY9iAFtDbOjSV46uSh96N02WKsv8a7KLFT+UvCng1rMPkjaikKtaMK slVoPwk9Q+OZE20d5oMjxx2iV6LmWJOZtNbHrA5OX1bWEPlI4gkPuEbjU/Axqb9BLc3/ tgpXwn2g25OeUz4RhRDSTxepU0o4xQDYGS6cs= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698381848; x=1698986648; h=user-agent: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=Ck7QpqkFMVKI6klSqauCxkSlGJ3DRRgQKMXshS/B7o8=; b=J1m7xWwlllnzpgNyagNv+JDueGxu4fsT04tYLFNXEgibzp50ji3k+lxOUHQYaC3M4l P6/rwJcKncup14VPgs70CBLtfFFnliO35tvuieZ0zYx6OegCtGXh4yleSbvonG6QzLlP FN0QqVdfjgee6yhzHppEeiZXiVXvVw500sObae05aANMBXgmdUqdz8He1fVElIjjTvAM Ymb8uqrs+hDK2OMOcgqz5U982/mR+ujsgtHKHZP0cF3DM/86dVlRFCO6e4KXwbZcFTlU wDMLemnz2AXnf13PRnOYkwOzD2eKDHmPP/0nDKhmUMZJ+AGUJzoXeibVh4R9oPpjXlTB clZg== X-Gm-Message-State: AOJu0Ywz7pLY2PJi0+5BB+SYBlgN6RFpK9YJdTxISww5VHtS03kcYKly lBMMlETLWLz/xclvr4QJt0avRHlqRI7DgevFPTg= X-Google-Smtp-Source: AGHT+IHkUTe3tU3ujLWmDe6ecHUqSv3e9EfPEEQPTRlGsD55bNjw+owMgU4SRx6rRnCNE4JS0HapFw== X-Received: by 2002:a05:690c:734:b0:5a7:db27:7794 with SMTP id bt20-20020a05690c073400b005a7db277794mr1651968ywb.25.1698381847745; Thu, 26 Oct 2023 21:44:07 -0700 (PDT) Received: from google.com ([2600:1702:a20:5750::48]) by smtp.gmail.com with ESMTPSA id d141-20020a814f93000000b00583e52232f1sm223422ywb.112.2023.10.26.21.44.06 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 26 Oct 2023 21:44:07 -0700 (PDT) Date: Thu, 26 Oct 2023 21:44:04 -0700 From: Noah Misch To: Smolkin Grigory Cc: pgsql-hackers@postgresql.org Subject: Re: race condition in pg_class Message-ID: <20231027044404.7e.nmisch@google.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: 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 Wed, Oct 25, 2023 at 01:39:41PM +0300, Smolkin Grigory wrote: > We are running PG13.10 and recently we have encountered what appears to be > a bug due to some race condition between ALTER TABLE ... ADD CONSTRAINT and > some other catalog-writer, possibly ANALYZE. > The problem is that after successfully creating index on relation (which > previosly didnt have any indexes), its pg_class.relhasindex remains set to > "false", which is illegal, I think. > Index was built using the following statement: > ALTER TABLE "example" ADD constraint "example_pkey" PRIMARY KEY (id); This is going to be a problem with any operation that does a transactional pg_class update without taking a lock that conflicts with ShareLock. GRANT doesn't lock the table at all, so I can reproduce this in v17 as follows: == session 1 create table t (c int); begin; grant select on t to public; == session 2 alter table t add primary key (c); == back in session 1 commit; We'll likely need to change how we maintain relhasindex or perhaps take a lock in GRANT. > Looking into the WAL via waldump given us the following picture (full > waldump output is attached): > 1202295045 - create index statement > 1202298790 and 1202298791 are some other concurrent operations, > unfortunately I wasnt able to determine what are they Can you explore that as follows? - PITR to just before the COMMIT record. - Save all rows of pg_class. - PITR to just after the COMMIT record. - Save all rows of pg_class. - Diff the two sets of saved rows. Which columns changed? The evidence you've shown would be consistent with a transaction doing GRANT or REVOKE on dozens of tables. If the changed column is something other than relacl, that would be great to know. On the off-chance it's relevant, what extensions do you have (\dx in psql)?