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 1mOkjj-0005iR-A7 for pgsql-general@arkaria.postgresql.org; Fri, 10 Sep 2021 17:54:51 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mOkji-0004PU-8E for pgsql-general@arkaria.postgresql.org; Fri, 10 Sep 2021 17:54:50 +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 1mOkjh-0004PM-Sp for pgsql-general@lists.postgresql.org; Fri, 10 Sep 2021 17:54:49 +0000 Received: from mail-qk1-x730.google.com ([2607:f8b0:4864:20::730]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1mOkjf-00071H-Ki for pgsql-general@postgresql.org; Fri, 10 Sep 2021 17:54:48 +0000 Received: by mail-qk1-x730.google.com with SMTP id m21so2865791qkm.13 for ; Fri, 10 Sep 2021 10:54:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:subject:from:in-reply-to :date:cc:message-id:references:to; bh=fEB9JW+GvmG9rjYoyZEBVrth3JhdVm72+7l/DAtIZpU=; b=Ry8g+o8N1aJ5esqAHCE39G3VHq42kaGXEJ4hkpICCNa3OCBsGlk6FAEVWYveQ+icn9 oiylEZ1JBpwI7Tdd1jykyvWGa4St4LksCwVKpZR6t6Uy2Kc+7kYFWkDD2G6BVzR6ILRx kUYJGiVSqDoCr/oLgeFwyoodKxh3LiHiE52o5elgHqINrAAMIXn+oB1xF23dhPiIfpFW PNkO8G9x3E4KlCteYF58VLzJvYiqvRjBxmJW1Z9LKIBuOpK4TeSlzUPstsDCuXz/NTAR 1XIUp5mB5yyFF9tjKmtsexjKPVcPTFlKW/UBld6rx17vug7785Ol9anujTLQanc+PS0H tjww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:content-transfer-encoding:mime-version:subject :from:in-reply-to:date:cc:message-id:references:to; bh=fEB9JW+GvmG9rjYoyZEBVrth3JhdVm72+7l/DAtIZpU=; b=Abpw+43oRDsF+ZxsXbGv2uYyvtcXX5Kbowr8XMsRsvXfJMrHOFY5Diy8IUKatsPtnC i7R6CI9G1vRh4RR2lsU2A7gt3sRyI8UWOIbNO8jcV0pA6Ken09dsO+W7naDcHm2kP0Uf mlH8kBw9tpcWGu2zRfEGZjPbqhetrifWkutUpeUqZ+XsG+r6trfbwek99+HjHa7evq5u AQKHEUIhh69VfHC34xPSprKotPciFx+j3TVZG14j0MEG8zE4uJB00FOIYLUpvcSaGiuz S9di8Dsl899LeAgvSIP9IBTIO4O1pueAY/Jd2MAG+mtupQFamk57u9qDZmbdVUCnPVAW gnbQ== X-Gm-Message-State: AOAM533SbYMUlCfdWW3iHR2vIecEd3yGClkddZ2C8KTgrL17M9FVVOWy CwuWtWlDb9CeXLYD9e31+F0= X-Google-Smtp-Source: ABdhPJz0y8Z2SD032GAq2P0sCiz6KXjzKBVev3q0sUn/40pIcNldDad5ip3rGS6JuadHtzJu/jU7Gw== X-Received: by 2002:a05:620a:4001:: with SMTP id h1mr8959144qko.454.1631296486589; Fri, 10 Sep 2021 10:54:46 -0700 (PDT) Received: from smtpclient.apple (216-24-67-3.access.naxs.com. [216.24.67.3]) by smtp.gmail.com with ESMTPSA id u13sm3746166qtg.29.2021.09.10.10.54.45 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 10 Sep 2021 10:54:46 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Mime-Version: 1.0 (1.0) Subject: Re: Is there any technical reason why "alter table .. set not null" can't use index? From: Gus Spier In-Reply-To: <20210908051453.GB20274@depesz.com> Date: Fri, 10 Sep 2021 13:26:46 -0400 Cc: Alexander Kukushkin , pgsql-general mailing list Message-Id: <7E44EA26-9343-444F-B01C-30944AB8C60A@gmail.com> References: <20210908051453.GB20274@depesz.com> To: depesz@depesz.com X-Mailer: iPhone Mail (18G82) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Would it help to create a new not null column in the target table, and then u= pdate the table by copying values from old column to the new, not null colum= n? Of course you=E2=80=99d have to ignore errors, etc. but wouldn=E2=80=99t t= hat perform at enough for your needs? Sent from my iPhone > On Sep 8, 2021, at 1:15 AM, hubert depesz lubaczewski w= rote: >=20 > =EF=BB=BFOn Wed, Sep 08, 2021 at 07:09:31AM +0200, Alexander Kukushkin wro= te: >> Hi, >>=20 >>> On Wed, 8 Sep 2021, 06:59 hubert depesz lubaczewski, = >>> wrote: >>>=20 >>> 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. >>>=20 >>=20 >> It can't use the index, but can use an already existing CHECK CONSTRAINT,= >> that could be created as NOT VALID and validated without holding heavy >> locks. After adding not null you can drop the constraint. >=20 > Thanks. Forgot about these. >=20 > Best regards, >=20 > depesz >=20 >=20 >=20