Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VeVA9-0004fj-7X for pgsql-bugs@arkaria.postgresql.org; Thu, 07 Nov 2013 19:22:41 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1VeVA8-0005kT-Is for pgsql-bugs@arkaria.postgresql.org; Thu, 07 Nov 2013 19:22:40 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VeVA7-0005kM-GD for pgsql-bugs@postgresql.org; Thu, 07 Nov 2013 19:22:39 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VeVA4-0006Cq-9M for pgsql-bugs@postgresql.org; Thu, 07 Nov 2013 19:22:39 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id rA7JMYvS009963; Thu, 7 Nov 2013 14:22:34 -0500 From: Tom Lane To: Michael Paquier cc: maxim.boguk@gmail.com, pgsql-bugs@postgresql.org Subject: Re: BUG #8408: Not exactly correct error messages for failed check in presence of dropped columns. In-reply-to: References: Comments: In-reply-to Michael Paquier message dated "Sun, 01 Sep 2013 22:16:18 +0900" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <9928.1383852033.0@sss.pgh.pa.us> Date: Thu, 07 Nov 2013 14:22:34 -0500 Message-ID: <9962.1383852154@sss.pgh.pa.us> X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <9928.1383852033.1@sss.pgh.pa.us> Michael Paquier writes: > On Fri, Aug 30, 2013 at 2:13 PM, wrote: >> However, I think error message should filter dropped column and strange null >> values (because if a table has a lot dropped column this error message >> quickly become completely unreadable). > Yes, this is not really user-friendly. This comes from > ExecBuildSlotValueDescription@execMain.c when generating a string > representation of a tuple. Attached is a patch to make this function > aware of dropped columns when generating the string (usable down to > 9.2). However, after some debugging I am seeing that the dropped > column is not seen inside TupleDesc (only the column name is correct) > when calling this function in executor. I got around to looking at this finally, and found out that the reason Michael's patch doesn't work is that the slot's tuple descriptor is one that's been made by ExecAssignResultTypeFromTL from the result targetlist the planner generates, so it has no dropped-column markings. (The targetlist includes a simple null constant at any dropped column's position; but TargetEntry has no way to mark itself as a dropped column.) I think it was coded that way to minimize ExecBuildSlotValueDescription's API complexity, but really what we ought to do is pass the actual target relation's tuple descriptor, which will have the necessary markings. The attached revision of Michael's patch fixes it. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="slot-value-description-fix.patch"; charset="us-ascii" Content-ID: <9928.1383852033.2@sss.pgh.pa.us> Content-Description: slot-value-description-fix.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/executor/execMain.c b/src/backend/executor/execMai= n.c index 0b47106..6be17a9 100644 *** a/src/backend/executor/execMain.c --- b/src/backend/executor/execMain.c *************** static void ExecutePlan(EState *estate,=20 *** 83,88 **** --- 83,89 ---- static bool ExecCheckRTEPerms(RangeTblEntry *rte); static void ExecCheckXactReadOnly(PlannedStmt *plannedstmt); static char *ExecBuildSlotValueDescription(TupleTableSlot *slot, + TupleDesc tupdesc, int maxfieldlen); static void EvalPlanQualStart(EPQState *epqstate, EState *parentestate, Plan *planTree); *************** ExecConstraints(ResultRelInfo *resultRel *** 1586,1610 **** TupleTableSlot *slot, EState *estate) { Relation rel =3D resultRelInfo->ri_RelationDesc; ! TupleConstr *constr =3D rel->rd_att->constr; =20=20 Assert(constr); =20=20 if (constr->has_not_null) { ! int natts =3D rel->rd_att->natts; int attrChk; =20=20 for (attrChk =3D 1; attrChk <=3D natts; attrChk++) { ! if (rel->rd_att->attrs[attrChk - 1]->attnotnull && slot_attisnull(slot, attrChk)) ereport(ERROR, (errcode(ERRCODE_NOT_NULL_VIOLATION), errmsg("null value in column \"%s\" violates not-null constraint", ! NameStr(rel->rd_att->attrs[attrChk - 1]->attname)), errdetail("Failing row contains %s.", ! ExecBuildSlotValueDescription(slot, 64)), errtablecol(rel, attrChk))); } } --- 1587,1614 ---- TupleTableSlot *slot, EState *estate) { Relation rel =3D resultRelInfo->ri_RelationDesc; ! TupleDesc tupdesc =3D RelationGetDescr(rel); ! TupleConstr *constr =3D tupdesc->constr; =20=20 Assert(constr); =20=20 if (constr->has_not_null) { ! int natts =3D tupdesc->natts; int attrChk; =20=20 for (attrChk =3D 1; attrChk <=3D natts; attrChk++) { ! if (tupdesc->attrs[attrChk - 1]->attnotnull && slot_attisnull(slot, attrChk)) ereport(ERROR, (errcode(ERRCODE_NOT_NULL_VIOLATION), errmsg("null value in column \"%s\" violates not-null constraint", ! NameStr(tupdesc->attrs[attrChk - 1]->attname)), errdetail("Failing row contains %s.", ! ExecBuildSlotValueDescription(slot, ! tupdesc, ! 64)), errtablecol(rel, attrChk))); } } *************** ExecConstraints(ResultRelInfo *resultRel *** 1619,1625 **** errmsg("new row for relation \"%s\" violates check constraint \"%s\= "", RelationGetRelationName(rel), failed), errdetail("Failing row contains %s.", ! ExecBuildSlotValueDescription(slot, 64)), errtableconstraint(rel, failed))); } } --- 1623,1631 ---- errmsg("new row for relation \"%s\" violates check constraint \"%s\= "", RelationGetRelationName(rel), failed), errdetail("Failing row contains %s.", ! ExecBuildSlotValueDescription(slot, ! tupdesc, ! 64)), errtableconstraint(rel, failed))); } } *************** ExecWithCheckOptions(ResultRelInfo *resu *** 1663,1669 **** errmsg("new row violates WITH CHECK OPTION for view \"%s\"", wco->viewname), errdetail("Failing row contains %s.", ! ExecBuildSlotValueDescription(slot, 64)))); } } =20=20 --- 1669,1677 ---- errmsg("new row violates WITH CHECK OPTION for view \"%s\"", wco->viewname), errdetail("Failing row contains %s.", ! ExecBuildSlotValueDescription(slot, ! RelationGetDescr(resultRelInfo->ri_RelationDesc), ! 64)))); } } =20=20 *************** ExecWithCheckOptions(ResultRelInfo *resu *** 1671,1685 **** * ExecBuildSlotValueDescription -- construct a string representing a tup= le * * This is intentionally very similar to BuildIndexValueDescription, but ! * unlike that function, we truncate long field values. That seems neces= sary ! * here since heap field values could be very long, whereas index entries ! * typically aren't so wide. */ static char * ! ExecBuildSlotValueDescription(TupleTableSlot *slot, int maxfieldlen) { StringInfoData buf; ! TupleDesc tupdesc =3D slot->tts_tupleDescriptor; int i; =20=20 /* Make sure the tuple is fully deconstructed */ --- 1679,1700 ---- * ExecBuildSlotValueDescription -- construct a string representing a tup= le * * This is intentionally very similar to BuildIndexValueDescription, but ! * unlike that function, we truncate long field values (to at most maxfie= ldlen ! * bytes). That seems necessary here since heap field values could be very ! * long, whereas index entries typically aren't so wide. ! * ! * Also, unlike the case with index entries, we need to be prepared to ig= nore ! * dropped columns. We used to use the slot's tuple descriptor to decode= the ! * data, but the slot's descriptor doesn't identify dropped columns, so we ! * now need to be passed the relation's descriptor. */ static char * ! ExecBuildSlotValueDescription(TupleTableSlot *slot, ! TupleDesc tupdesc, ! int maxfieldlen) { StringInfoData buf; ! bool write_comma =3D false; int i; =20=20 /* Make sure the tuple is fully deconstructed */ *************** ExecBuildSlotValueDescription(TupleTable *** 1694,1699 **** --- 1709,1718 ---- char *val; int vallen; =20=20 + /* ignore dropped columns */ + if (tupdesc->attrs[i]->attisdropped) + continue; +=20 if (slot->tts_isnull[i]) val =3D "null"; else *************** ExecBuildSlotValueDescription(TupleTable *** 1706,1713 **** val =3D OidOutputFunctionCall(foutoid, slot->tts_values[i]); } =20=20 ! if (i > 0) appendStringInfoString(&buf, ", "); =20=20 /* truncate if needed */ vallen =3D strlen(val); --- 1725,1734 ---- val =3D OidOutputFunctionCall(foutoid, slot->tts_values[i]); } =20=20 ! if (write_comma) appendStringInfoString(&buf, ", "); + else + write_comma =3D true; =20=20 /* truncate if needed */ vallen =3D strlen(val); diff --git a/src/test/regress/expected/alter_table.out b/src/test/regress/e= xpected/alter_table.out index 7cc0084..7366392 100644 *** a/src/test/regress/expected/alter_table.out --- b/src/test/regress/expected/alter_table.out *************** select * from atacc1; *** 1197,1202 **** --- 1197,1216 ---- (1 row) =20=20 drop table atacc1; + -- test constraint error reporting in presence of dropped columns + create table atacc1 (id serial primary key, value int check (value < 10)); + insert into atacc1(value) values (100); + ERROR: new row for relation "atacc1" violates check constraint "atacc1_v= alue_check" + DETAIL: Failing row contains (1, 100). + alter table atacc1 drop column value; + alter table atacc1 add column value int check (value < 10); + insert into atacc1(value) values (100); + ERROR: new row for relation "atacc1" violates check constraint "atacc1_v= alue_check" + DETAIL: Failing row contains (2, 100). + insert into atacc1(id, value) values (null, 0); + ERROR: null value in column "id" violates not-null constraint + DETAIL: Failing row contains (null, 0). + drop table atacc1; -- test inheritance create table parent (a int, b int, c int); insert into parent values (1, 2, 3); diff --git a/src/test/regress/sql/alter_table.sql b/src/test/regress/sql/al= ter_table.sql index a546ba7..0d3b79b 100644 *** a/src/test/regress/sql/alter_table.sql --- b/src/test/regress/sql/alter_table.sql *************** select * from atacc1; *** 874,879 **** --- 874,888 ---- =20=20 drop table atacc1; =20=20 + -- test constraint error reporting in presence of dropped columns + create table atacc1 (id serial primary key, value int check (value < 10)); + insert into atacc1(value) values (100); + alter table atacc1 drop column value; + alter table atacc1 add column value int check (value < 10); + insert into atacc1(value) values (100); + insert into atacc1(id, value) values (null, 0); + drop table atacc1; +=20 -- test inheritance create table parent (a int, b int, c int); insert into parent values (1, 2, 3); ------- =_aaaaaaaaaa0 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs ------- =_aaaaaaaaaa0--