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 1tBrqk-006ced-Gg for pgsql-general@arkaria.postgresql.org; Fri, 15 Nov 2024 08:38:41 +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 1tBrqh-008MzG-Ot for pgsql-general@arkaria.postgresql.org; Fri, 15 Nov 2024 08:38:40 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tBhTM-006qrz-5M for pgsql-general@lists.postgresql.org; Thu, 14 Nov 2024 21:33:52 +0000 Received: from mail-wm1-x331.google.com ([2a00:1450:4864:20::331]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1tBhTG-001tkw-1e for pgsql-general@lists.postgresql.org; Thu, 14 Nov 2024 21:33:51 +0000 Received: by mail-wm1-x331.google.com with SMTP id 5b1f17b1804b1-432d866f70fso9613665e9.2 for ; Thu, 14 Nov 2024 13:33:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731620024; x=1732224824; darn=lists.postgresql.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=jfueDAv+AhRhyhPRMsJtdlKFKRdkr3nN10azQzKFMYs=; b=GYQkleE+C67Yod7boUEW252wSs872cewwa/f5O7eZ86lG6chOBGiIzOs6DhqOZLz+f nh7p0r9aHboXXTUM14NOg4cmtIkRZ3BiInVsVvd9jylq8dYILCx4rVyrhqpab4Gzj4rE A++Dy/a1GokBCpftdTXo6yxUDy7MjjVBnAJhC4TmQj/zqrrJcCU47+F5oOnTnLXg4nVu SjHKKUkgafe/Faoe6lx08UgjYxddyGRKGA4DlSzfLi96mDazGgMciNfBms6TudG+z7eM cWNaAve23PfvjzxKUUgcW/B06iG49BvMU9/911yVSNN/X22vZ2BgHnl5poQkf0tY+lCR Ms0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731620024; x=1732224824; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=jfueDAv+AhRhyhPRMsJtdlKFKRdkr3nN10azQzKFMYs=; b=Xo9fI3VjhzLoNDEe2rjLyXOAqQrMYh8aEKfjvxlqoY/rA2BXGNLW3RsrD0jGw22hqG UN7DfyMh3S5OTx/xtaQm6tUINVEIp+RJEEYZDrlovl+BrQzmYWiH2SEqL4NZJcg3qVXs 3mGpLY/3zqoQK52zNDFTi9CTw5iAWHhsFp4Utcp5YcCgLkX+8/hVZh56GBYVg0F5wGu9 wJfQeG59ouExCA3iQFIkes2JeY2D8yPd4jvEn0cUJqS2ZCrTexBLp9Mc56F6BFDYD0+r oNwWD1Aobw1tf9/XNZWOBZrS53eKBpEg5QnRPWVbVuaRcclnCaBUuJeXzJxpm4ffme/j DToA== X-Gm-Message-State: AOJu0YwtnA7HmtWr5YFSBLOyJPPqkxtjXtgrejPn0hfC0hvqu31sdBoJ PF41YpC4i65OfNxuWAlqqVW7+bLybF2gRRTp3yCLfcV41uUQ6q5ITKKkx8Xla5MAagp8pMIa/2L 5NRgagYnt19KIQ/HJoLzFpA2WIrKD2Q== X-Google-Smtp-Source: AGHT+IHXU1oof6cCDJEXeHVHvoARkzbVpEI9e3j4r2iWEyCcNGK0x+O0qI1daubynTUj11vC9YaG3RvHIsNqIL2E+1Q= X-Received: by 2002:a5d:6dab:0:b0:37d:4f1b:35b with SMTP id ffacd0b85a97d-38225a49984mr255442f8f.34.1731620023568; Thu, 14 Nov 2024 13:33:43 -0800 (PST) MIME-Version: 1.0 From: Philip Couling Date: Thu, 14 Nov 2024 21:33:31 +0000 Message-ID: Subject: Validating check constraints without a table scan? To: pgsql-general@lists.postgresql.org Content-Type: multipart/alternative; boundary="0000000000006413290626e63424" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000006413290626e63424 Content-Type: text/plain; charset="UTF-8" Is there a solid reason why adding a check constraint does not use existing indexes for validation. We are currently looking at partitioning a multi TB table leaving all existing data in place and simply attaching it as a partition to a new table. To prevent locking, we are trying to add an INVALID check constraint first and then validate it. I can trivially prove the invalid constraint is valid with a simple SELECT which will use an existing index and return instantaneously. But AFAIK Theres no way to mark a constraint as valid without scanning all the rows. This operation is really problematic on a production database with heavy IO load. Is there a solid ready why validating check constraints cannot use existing indexes? If I can prove the constraint is valid so trivially with a SELECT, then why can Postgres not do the same (or similar)? --0000000000006413290626e63424 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Is there a solid reason why adding a check constraint does not use existing= indexes for validation.

We ar= e currently looking at partitioning a multi TB table leaving all existing d= ata in place and simply attaching it as a partition to a new table. To prev= ent locking, we are trying to add an INVALID check constraint first and the= n validate it.

I can tri= vially prove the invalid constraint is valid with a simple SELECT which wil= l use an existing index and return instantaneously. But AFAIK Theres no way= to mark a constraint as valid without scanning all the rows.

This operation is really problematic = on a production database with heavy IO load.

Is there a solid ready why validating check constraint= s cannot use existing indexes? If I can prove the constraint is valid so tr= ivially with a SELECT, then why can Postgres not do the same (or similar)?<= /div> --0000000000006413290626e63424--