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 1mNpgG-0007ZY-BT for pgsql-general@arkaria.postgresql.org; Wed, 08 Sep 2021 04:59:28 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mNpgD-0001Jo-U7 for pgsql-general@arkaria.postgresql.org; Wed, 08 Sep 2021 04:59:25 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mNpgD-0001Jg-Im for pgsql-general@lists.postgresql.org; Wed, 08 Sep 2021 04:59:25 +0000 Received: from lana.depesz.com ([88.198.49.178] helo=depesz.com) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mNpgB-0003R5-0T for pgsql-general@postgresql.org; Wed, 08 Sep 2021 04:59:24 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=depesz.com; s=20170201; h=Content-Type:MIME-Version:Reply-To:Message-ID:Subject:To: Sender:From:Date: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=CP5MeWuU//EV7WE2yG+DJkvQzpxLxIwp6CepqnYcD/A=; b=TOahMbpG3adpDtcZm2huhQfA/a K9qJ7ZOeH4WLtOz/KpOu01uizXZ3scNMAlAMVsFvZAAwnf/erIxAlgpOw2wdOZV50NEjAtvUBpKW4 4AiIMZzzHGIltQCSnJNXU83dY+sxGRiOzy/ubNxgD8oWXzCWC7JRq1p5s2TYLyJVDH10=; Received: from lana.depesz.com ([88.198.49.178] helo=depesz.com) by depesz.com with esmtpa (Exim 4.92) (envelope-from ) id 1mNpg7-00015g-Li for pgsql-general@postgresql.org; Wed, 08 Sep 2021 06:59:19 +0200 Date: Wed, 8 Sep 2021 06:59:19 +0200 From: hubert depesz lubaczewski Sender: depesz@depesz.com To: pgsql-general mailing list Subject: Is there any technical reason why "alter table .. set not null" can't use index? Message-ID: <20210908045919.GA29476@depesz.com> Reply-To: depesz@depesz.com MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, we needed recently to add not null constraint on some fields, and it struck me that it took long. Obviously - pg has to check the data. But it seems that it can't use index. Made 4 test tables: create table test (a int4, b timestamptz); insert into test (a,b) select i, now() - i * '1 minute'::interval from generate_series(1,10000000) i; create table test2 as select * from test; create table test3 as select * from test; create table test4 as select * from test; The idea for test is that i want to add "not null" clause to column a. So I made 3 indexes that might help. Maybe: Simple index on a: create index i1 on test2 (a); Index on a, but with condition that it indexes only null values create index i2 on test3 (a) where a is null; Index on a, but with condition that it indexes only not null values create index i3 on test4 (a) where a is not null; Theoretically, index i2 should be the most helpful - if it's valid, and empty, then it shouldn't be impossible to make the check for alter table using it, and thus reducing check time by "a lot". But: $ alter table test alter column a set not null; ALTER TABLE Time: 352.682 ms $ alter table test2 alter column a set not null; ALTER TABLE Time: 362.031 ms $ alter table test3 alter column a set not null; ALTER TABLE Time: 384.409 ms $ alter table test4 alter column a set not null; ALTER TABLE Time: 392.173 ms All alter tables took more or less the same time. So. I understand that to be 100% sure we need seq scan, but perhaps this requirement could be relaxed to use index if it's there, and is valid, has appropriate where and is empty? Best regards, depesz