Skip to content

Commit 747ddd3

Browse files
peterejianhe-fun
andcommitted
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
1 parent 9a9ead1 commit 747ddd3

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

src/backend/executor/execMain.c

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1855,7 +1855,6 @@ ExecRelCheck(ResultRelInfo *resultRelInfo,
18551855
ConstrCheck *check = rel->rd_att->constr->check;
18561856
ExprContext *econtext;
18571857
MemoryContext oldContext;
1858-
int i;
18591858

18601859
/*
18611860
* CheckConstraintFetch let this pass with only a warning, but now we
@@ -1874,9 +1873,8 @@ ExecRelCheck(ResultRelInfo *resultRelInfo,
18741873
if (resultRelInfo->ri_CheckConstraintExprs == NULL)
18751874
{
18761875
oldContext = MemoryContextSwitchTo(estate->es_query_cxt);
1877-
resultRelInfo->ri_CheckConstraintExprs =
1878-
(ExprState **) palloc0(ncheck * sizeof(ExprState *));
1879-
for (i = 0; i < ncheck; i++)
1876+
resultRelInfo->ri_CheckConstraintExprs = palloc0_array(ExprState *, ncheck);
1877+
for (int i = 0; i < ncheck; i++)
18801878
{
18811879
Expr *checkconstr;
18821880

@@ -1902,7 +1900,7 @@ ExecRelCheck(ResultRelInfo *resultRelInfo,
19021900
econtext->ecxt_scantuple = slot;
19031901

19041902
/* And evaluate the constraints */
1905-
for (i = 0; i < ncheck; i++)
1903+
for (int i = 0; i < ncheck; i++)
19061904
{
19071905
ExprState *checkconstr = resultRelInfo->ri_CheckConstraintExprs[i];
19081906

@@ -2061,16 +2059,16 @@ ExecConstraints(ResultRelInfo *resultRelInfo,
20612059

20622060
Assert(constr); /* we should not be called otherwise */
20632061

2062+
/*
2063+
* Verify not-null constraints.
2064+
*/
20642065
if (constr->has_not_null)
20652066
{
2066-
int natts = tupdesc->natts;
2067-
int attrChk;
2068-
2069-
for (attrChk = 1; attrChk <= natts; attrChk++)
2067+
for (AttrNumber attnum = 1; attnum <= tupdesc->natts; attnum++)
20702068
{
2071-
Form_pg_attribute att = TupleDescAttr(tupdesc, attrChk - 1);
2069+
Form_pg_attribute att = TupleDescAttr(tupdesc, attnum - 1);
20722070

2073-
if (att->attnotnull && slot_attisnull(slot, attrChk))
2071+
if (att->attnotnull && slot_attisnull(slot, attnum))
20742072
{
20752073
char *val_desc;
20762074
Relation orig_rel = rel;
@@ -2115,16 +2113,19 @@ ExecConstraints(ResultRelInfo *resultRelInfo,
21152113
64);
21162114

21172115
ereport(ERROR,
2118-
(errcode(ERRCODE_NOT_NULL_VIOLATION),
2119-
errmsg("null value in column \"%s\" of relation \"%s\" violates not-null constraint",
2120-
NameStr(att->attname),
2121-
RelationGetRelationName(orig_rel)),
2122-
val_desc ? errdetail("Failing row contains %s.", val_desc) : 0,
2123-
errtablecol(orig_rel, attrChk)));
2116+
errcode(ERRCODE_NOT_NULL_VIOLATION),
2117+
errmsg("null value in column \"%s\" of relation \"%s\" violates not-null constraint",
2118+
NameStr(att->attname),
2119+
RelationGetRelationName(orig_rel)),
2120+
val_desc ? errdetail("Failing row contains %s.", val_desc) : 0,
2121+
errtablecol(orig_rel, attnum));
21242122
}
21252123
}
21262124
}
21272125

2126+
/*
2127+
* Verify check constraints.
2128+
*/
21282129
if (rel->rd_rel->relchecks > 0)
21292130
{
21302131
const char *failed;

0 commit comments

Comments
 (0)