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 1tE5w1-0055qF-0b for pgsql-general@arkaria.postgresql.org; Thu, 21 Nov 2024 12:05:21 +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 1tE5vz-00Exvj-Bc for pgsql-general@arkaria.postgresql.org; Thu, 21 Nov 2024 12:05:19 +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 1tDihH-004qUV-K4 for pgsql-general@lists.postgresql.org; Wed, 20 Nov 2024 11:16:35 +0000 Received: from mail-wm1-x329.google.com ([2a00:1450:4864:20::329]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1tDihE-002uk1-Uy for pgsql-general@lists.postgresql.org; Wed, 20 Nov 2024 11:16:35 +0000 Received: by mail-wm1-x329.google.com with SMTP id 5b1f17b1804b1-4316f3d3c21so55510715e9.3 for ; Wed, 20 Nov 2024 03:16:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1732101392; x=1732706192; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=D0xxGQU+Q9qo9bCJh6czNmBbhlr4w2pX7ltG6QVA6MM=; b=jWTlqVO0gXN1ROMikVxY3F54qMAR+6LgMMll9JtY9AaHVs0hf0CNTV7vFexvZ4DXob +zY5TnR8rx2IHpvX7nKphM9BojEbADdgd5tF7euj7WO8HEhtlYg2SrevSh/9/OxkPgYS wxy67FbWPj+yuzil4FDilX1QGxmXlwzMqZJ0nJHp7AfKyhEs6UCj4fzBPdPC6c6AANNX OUi3fRzdkMEEQjZkGTRUgk1XOzmB3DOPNG73ZYf93IrDi3QfL8zOWIg/fOrDMv0QuWI1 SMQr4R4/YhNGR1rAvkaQ+6EmpWy1kyPrG4yTZit52gaebElSqfYBRuda2cvT2RJW0VOR ppFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732101392; x=1732706192; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=D0xxGQU+Q9qo9bCJh6czNmBbhlr4w2pX7ltG6QVA6MM=; b=eMn/ZzYlSFEsoRaoW8DiYZ78YNLq53XNmAwsASUehdlav0cLD8LW28fH8QxKGZykHS Xsmg4V9MzWmni/ZvtkJX6nLaIh/Bjrkz5z0zAYPaClzp09dmXdr2EfAbRSkUVU5g+IzQ AQ8N14jPJLWKNAK9JXTgizibI23UjLXeGtK9jKsuHCJdiOMoRepTOn5gri9IhgKE5dVf ge/V+ZP8D5xGVAionutLfteBmLXX49d4sw3NSIzinAUDinWETt+LvbwWdS4h2lH2oZHc 6uq/wtrmQdWo7yKvI3fYZdyWJkfj9CaYeBIlQfL6Bfg2JylXTSjDScWuXHQLH/Bz5SQo so0g== X-Gm-Message-State: AOJu0YxtYTOwrC4iBVb4gZ2a1oJRRo9yIkgTnwECcljcV65CS2DeqmSA 8ymIYBaKwBPJPAKEWuZsrSi6e1twVFbucGLiPjO/nb4KMPVJ4tkqfFl+F3axHZtSJMRxumAChV/ xxQJK9h8ugQmOGYW8lMMbyaBAnR1faJ5I X-Google-Smtp-Source: AGHT+IHAQPHskq2R++aAOpPsMtrU5qREdjACr56fAg0+blX6lTL4dBS8zeWKqJLAi9p5AX42ibaHypist2pNDd3C3kM= X-Received: by 2002:a05:6000:154e:b0:382:22f4:7773 with SMTP id ffacd0b85a97d-38254a83f8fmr2100037f8f.0.1732101391894; Wed, 20 Nov 2024 03:16:31 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Philip Couling Date: Wed, 20 Nov 2024 11:16:20 +0000 Message-ID: Subject: Re: Validating check constraints without a table scan? To: jian he Cc: pgsql-general@lists.postgresql.org Content-Type: multipart/alternative; boundary="0000000000002dcfdf0627564853" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000002dcfdf0627564853 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Jian He The context here is constraints for partitioning as suggested in documentation https://www.postgresql.org/docs/current/ddl-partitioning.html#DDL-PARTITION= ING-DECLARATIVE-MAINTENANCE An example constraint from the documentation: ALTER TABLE measurement_y2008m02 ADD CONSTRAINT y2008m02 CHECK ( logdate >=3D DATE '2008-02-01' AND logdate < DATE '2008-03-01' )= ; If logdate is indexed, then this constraint can be manually validated very quickly using a SELECT that will take advantage of the index SELECT 1 FROM measurement_y2008m02 WHERE logdate < DATE '2008-02-01' OR logdate >=3D DATE '2008-03-01' LIMIT 1 If the constraint is valid the query will return quickly with no rows, if any rows violate the constraint it will also return very quickly but return with a single row with column value: 1. I guess that validating constraints doesn't invoke the query planner, or otherwise the conversion is too complex for the query planner. The conversion being: - from: NOT (logdate >=3D DATE '2008-02-01' AND logdate < DATE '2008-03-01') - to: logdate < DATE '2008-02-01' OR logdate >=3D DATE '2008-03-01' Hope that clarifies it. On Wed, 20 Nov 2024 at 09:45, jian he wrote: > On Fri, Nov 15, 2024 at 4:38=E2=80=AFPM Philip Couling wrote: > > > > Is there a solid reason why adding a check constraint does not use > existing indexes for validation. > > > > can you give an sql example (except not-null) > where indexes can be used for check constraint validation? > i am not sure I understand it correctly. > --0000000000002dcfdf0627564853 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Jian He

The context here is const= raints for partitioning as suggested in documentation=C2=A0
https://www.postgresql.org/docs/current/ddl-pa= rtitioning.html#DDL-PARTITIONING-DECLARATIVE-MAINTENANCE

=
An example constraint from the documentation:
ALTER TABLE measurement_y2008m02 ADD CONSTRAINT y2008m02
=C2= =A0 =C2=A0CHECK ( logdate >=3D DATE '2008-02-01' AND logdate <= ; DATE '2008-03-01' );
If logdate=C2=A0is inde= xed, then this constraint can be manually validated very quickly using a SE= LECT that will take advantage of the index
SELECT 1 FROM=C2=A0m= easurement_y2008m02=C2=A0 WHERE=C2=A0logdate < DATE '2008-02-01' OR logdate >=3D DATE '= 2008-03-01' LIMIT 1

If the constraint i= s valid the query will return quickly with no rows, if any rows violate the= constraint it will also return very quickly but return with a single row w= ith column value: 1.

I guess that validating const= raints doesn't invoke the query planner, or otherwise the conversion is= too complex for the query planner. The conversion being:
    from:=C2=A0 NOT (logdate >=3D DATE '2008-02-01' AND logdate <= ; DATE '2008-03-01')
  • to: logdate < DATE '2008-02-01&= #39; OR logdate >=3D DATE '2008-03-01'
Hope that c= larifies it.

On Wed, 20 Nov 2024 at 09:45, jian he <jian.universality@gmail.com= > wrote:
On F= ri, Nov 15, 2024 at 4:38=E2=80=AFPM Philip Couling <couling@gmail.com> wrote:
>
> Is there a solid reason why adding a check constraint does not use exi= sting indexes for validation.
>

can you give an sql example (except not-null)
where indexes can be used for check constraint validation?
i am not sure I understand it correctly.
--0000000000002dcfdf0627564853--