1
0
mirror of https://github.com/postgres/postgres.git synced 2025-04-20 00:42:27 +03:00

Modernize some code a bit

Modernize code in ExecRelCheck() and ExecConstraints() a bit,
preparing the way for some new code.

Co-authored-by: jian he <jian.universality@gmail.com>
Reviewed-by: Xuneng Zhou <xunengzhou@gmail.com>
Reviewed-by: Navneet Kumar <thanit3111@gmail.com>
Reviewed-by: Álvaro Herrera <alvherre@alvh.no-ip.org>
Discussion: https://postgr.es/m/CACJufxHArQysbDkWFmvK+D1TPHQWWTxWN15cMuUaTYX3xhQXgg@mail.gmail.com
This commit is contained in:
Peter Eisentraut 2025-03-28 10:49:15 +01:00
parent 9a9ead1105
commit 747ddd38cb

View File

@ -1855,7 +1855,6 @@ ExecRelCheck(ResultRelInfo *resultRelInfo,
ConstrCheck *check = rel->rd_att->constr->check; ConstrCheck *check = rel->rd_att->constr->check;
ExprContext *econtext; ExprContext *econtext;
MemoryContext oldContext; MemoryContext oldContext;
int i;
/* /*
* CheckConstraintFetch let this pass with only a warning, but now we * CheckConstraintFetch let this pass with only a warning, but now we
@ -1874,9 +1873,8 @@ ExecRelCheck(ResultRelInfo *resultRelInfo,
if (resultRelInfo->ri_CheckConstraintExprs == NULL) if (resultRelInfo->ri_CheckConstraintExprs == NULL)
{ {
oldContext = MemoryContextSwitchTo(estate->es_query_cxt); oldContext = MemoryContextSwitchTo(estate->es_query_cxt);
resultRelInfo->ri_CheckConstraintExprs = resultRelInfo->ri_CheckConstraintExprs = palloc0_array(ExprState *, ncheck);
(ExprState **) palloc0(ncheck * sizeof(ExprState *)); for (int i = 0; i < ncheck; i++)
for (i = 0; i < ncheck; i++)
{ {
Expr *checkconstr; Expr *checkconstr;
@ -1902,7 +1900,7 @@ ExecRelCheck(ResultRelInfo *resultRelInfo,
econtext->ecxt_scantuple = slot; econtext->ecxt_scantuple = slot;
/* And evaluate the constraints */ /* And evaluate the constraints */
for (i = 0; i < ncheck; i++) for (int i = 0; i < ncheck; i++)
{ {
ExprState *checkconstr = resultRelInfo->ri_CheckConstraintExprs[i]; ExprState *checkconstr = resultRelInfo->ri_CheckConstraintExprs[i];
@ -2061,16 +2059,16 @@ ExecConstraints(ResultRelInfo *resultRelInfo,
Assert(constr); /* we should not be called otherwise */ Assert(constr); /* we should not be called otherwise */
/*
* Verify not-null constraints.
*/
if (constr->has_not_null) if (constr->has_not_null)
{ {
int natts = tupdesc->natts; for (AttrNumber attnum = 1; attnum <= tupdesc->natts; attnum++)
int attrChk;
for (attrChk = 1; attrChk <= natts; attrChk++)
{ {
Form_pg_attribute att = TupleDescAttr(tupdesc, attrChk - 1); Form_pg_attribute att = TupleDescAttr(tupdesc, attnum - 1);
if (att->attnotnull && slot_attisnull(slot, attrChk)) if (att->attnotnull && slot_attisnull(slot, attnum))
{ {
char *val_desc; char *val_desc;
Relation orig_rel = rel; Relation orig_rel = rel;
@ -2115,16 +2113,19 @@ ExecConstraints(ResultRelInfo *resultRelInfo,
64); 64);
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_NOT_NULL_VIOLATION), errcode(ERRCODE_NOT_NULL_VIOLATION),
errmsg("null value in column \"%s\" of relation \"%s\" violates not-null constraint", errmsg("null value in column \"%s\" of relation \"%s\" violates not-null constraint",
NameStr(att->attname), NameStr(att->attname),
RelationGetRelationName(orig_rel)), RelationGetRelationName(orig_rel)),
val_desc ? errdetail("Failing row contains %s.", val_desc) : 0, val_desc ? errdetail("Failing row contains %s.", val_desc) : 0,
errtablecol(orig_rel, attrChk))); errtablecol(orig_rel, attnum));
} }
} }
} }
/*
* Verify check constraints.
*/
if (rel->rd_rel->relchecks > 0) if (rel->rd_rel->relchecks > 0)
{ {
const char *failed; const char *failed;