Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pPolD-00062G-7P for pgsql-performance@arkaria.postgresql.org; Wed, 08 Feb 2023 18:01:35 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pPolC-0006CT-5a for pgsql-performance@arkaria.postgresql.org; Wed, 08 Feb 2023 18:01:34 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pPolB-0006CK-Ic for pgsql-performance@lists.postgresql.org; Wed, 08 Feb 2023 18:01:33 +0000 Received: from disco.pogo.org.uk ([93.93.128.62]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pPol4-0001U7-Qm for pgsql-performance@lists.postgresql.org; Wed, 08 Feb 2023 18:01:33 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xwax.org; s=swing; h=Content-Type:MIME-Version:Message-ID:Subject:To:From:Date:Sender: Reply-To:Cc:Content-Transfer-Encoding:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: In-Reply-To:References:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=0bA0nysQyH+8H4mq3Xxa7T8liQELYgy20DI0bVjfqcg=; b=jJ/rtMkk9uyx3TNST2QX/iae2P igMzvrz4E3AGf1pKyKHuse0I7WqFXtEEkF3O17DI7R0WLetyCDB9ZLgKazk8uifsGUSYa6yTvZhJX IB9vrfkeMgFbUG8QOSCYBz0gvKpydI/jgFPKRDwQReq2cDH7LlzsUP/hCtjuIni8Gv9Q=; Received: from cpc93788-hari17-2-0-cust1237.20-2.cable.virginm.net ([82.39.100.214] helo=stax) by disco.pogo.org.uk with esmtps (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.95 (FreeBSD)) (envelope-from ) id 1pPokv-0003Xu-0d for pgsql-performance@lists.postgresql.org; Wed, 08 Feb 2023 18:01:25 +0000 Received: from localhost (stax.localdomain [local]) by stax.localdomain (OpenSMTPD) with ESMTPA id 6a57485a for ; Wed, 8 Feb 2023 18:01:23 +0000 (UTC) Date: Wed, 8 Feb 2023 18:01:23 +0000 (GMT) From: Mark Hills To: pgsql-performance@lists.postgresql.org Subject: Domain check taking place unnecessarily? Message-ID: <2302081605290.5352@stax.localdomain> MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk I'm used to adding an empty column being instant in most cases, so my attention was drawn when it took a long lock. The timings below imply that each row is running the CHECK? I've come to expect addition of a NULL column to be fast, and what I'm seeing seems to contradict the docs [1]: > PostgreSQL assumes that CHECK constraints' conditions are immutable, > that is, they will always give the same result for the same input value. > This assumption is what justifies examining CHECK constraints only when > a value is first converted to be of a domain type, and not at other > times. I've ruled out waiting on a lock; nothing is reported with log_lock_waits=on. This is a test database with exclusive access (2.5 million rows): I don't think this is another index or constraint, as removing them does not affect performance. Also the "text" case below seems to prove this. Results are fully reproducable by repeatedly dropping and adding these columns. Reporting in case something is not as expected. I can't even think of a workaround here... This is PostgreSQL 14.5 on Alpine Linux. Thanks. [1] https://www.postgresql.org/docs/current/sql-createdomain.html --- CREATE DOMAIN hash AS text CHECK (VALUE ~ E'^[a-zA-Z0-9]{8,32}$'); devstats=> ALTER TABLE invite ADD COLUMN test text; ALTER TABLE Time: 8.988 ms devstats=> ALTER TABLE invite ADD COLUMN test hash; ALTER TABLE Time: 30923.380 ms (00:30.923) devstats=> ALTER TABLE invite ADD COLUMN test hash DEFAULT NULL; ALTER TABLE Time: 30344.272 ms (00:30.344) devstats=> ALTER TABLE invite ADD COLUMN test hash DEFAULT '123abc123'::hash; ALTER TABLE Time: 67439.232 ms (01:07.439) -- Mark