Re: NOT ENFORCED constraint feature

From: Amul Sul <sulamul(at)gmail(dot)com>
To: jian he <jian(dot)universality(at)gmail(dot)com>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>, Joel Jacobson <joel(at)compiler(dot)org>, Peter Eisentraut <peter(at)eisentraut(dot)org>
Subject: Re: NOT ENFORCED constraint feature
Date: 2024-12-04 11:04:23
Message-ID: CAAJ_b97Up5FoyX8OGh_Lg2GtqKw14W8S9J5sDR5jd_h3SJ+=jA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Wed, Dec 4, 2024 at 1:40 PM jian he <jian(dot)universality(at)gmail(dot)com> wrote:
>
> i just only apply v5-0001 for now.
>
> create table t(a int);
> alter table t ADD CONSTRAINT cc CHECK (a > 0);
> alter table t alter CONSTRAINT cc NOT ENFORCED;
> alter table t alter CONSTRAINT cc ENFORCED;
>
> the last two queries will fail, which means
> ALTER CONSTRAINT constraint_name [ DEFERRABLE | NOT DEFERRABLE ] [
> INITIALLY DEFERRED | INITIALLY IMMEDIATE ] [ ENFORCED | NOT ENFORCED ]
> in doc/src/sgml/ref/alter_table.sgml is not correct?
> also no code change in ATExecAlterConstraint.
>

Your are correct, will move this to 0005 patch.

> errmsg("cannot validated NOT ENFORCED constraint")));
> should be
> errmsg("cannot validate NOT ENFORCED constraint")));
> ?
>

Yes, I realized that while working on Peter's last review comments.

> typedef struct ConstrCheck
> {
> char *ccname;
> char *ccbin; /* nodeToString representation of expr */
> bool ccenforced;
> bool ccvalid;
> bool ccnoinherit; /* this is a non-inheritable constraint */
> } ConstrCheck
>
> ConstraintImpliedByRelConstraint,
> get_relation_constraints
> need skip notenforced check constraint?
>

That gets skipped since ccvalid is false for NOT ENFORCED constraints.
However, for better readability, I've added an assertion with a
comment in my local changes.

>
> put domain related tests from constraints.sql to domain.sql would be better.

Ok.

> looking at it again.
>
> if (!con->conenforced)
> ereport(ERROR,
> (errcode(ERRCODE_WRONG_OBJECT_TYPE),
> errmsg("cannot validated NOT ENFORCED constraint")));
>
> ERRCODE_WRONG_OBJECT_TYPE is not that ok? maybe
> ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE
> or
> ERRCODE_INVALID_TABLE_DEFINITION
>

I think ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE would be much suitable.

>
> if (!con->conenforced)
> ereport(ERROR,
> (errcode(ERRCODE_WRONG_OBJECT_TYPE),
> errmsg("cannot validated NOT ENFORCED constraint")));
> if (!con->convalidated)
> {
> ....
> if (con->contype == CONSTRAINT_FOREIGN)
> {
> /*
> * Queue validation for phase 3 only if constraint is enforced;
> * otherwise, adding it to the validation queue won't be very
> * effective, as the verification will be skipped.
> */
> if (con->conenforced)
> ......
> }
>
> in ATExecValidateConstraint "" if (con->conenforced)""" will always be true?

Yes, the changes from that patch have been reverted in my local code, which
I will post soon.

Thanks again for your review comments; they were very helpful.

Regards,
Amul

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Kirill Reshke 2024-12-04 11:12:07 Re: Implement waiting for wal lsn replay: reloaded
Previous Message Amit Kapila 2024-12-04 10:59:23 Re: Conflict detection for update_deleted in logical replication