Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit c8bcd5c

Browse files
committed
Fix problems with dropped columns in pltcl triggers, per report from Patrick Samson.
1 parent 0152f14 commit c8bcd5c

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

doc/src/sgml/pltcl.sgml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/pltcl.sgml,v 2.28 2003/11/29 19:51:37 pgsql Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/pltcl.sgml,v 2.29 2004/01/24 23:06:29 tgl Exp $
33
-->
44

55
<chapter id="pltcl">
@@ -516,7 +516,10 @@ SELECT 'doesn''t' AS ret
516516
element. So looking up a column name in the list with <application>Tcl</>'s
517517
<function>lsearch</> command returns the element's number starting
518518
with 1 for the first column, the same way the columns are customarily
519-
numbered in <productname>PostgreSQL</productname>.
519+
numbered in <productname>PostgreSQL</productname>. (Empty list
520+
elements also appear in the positions of columns that have been
521+
dropped, so that the attribute numbering is correct for columns
522+
to their right.)
520523
</para>
521524
</listitem>
522525
</varlistentry>

src/pl/tcl/pltcl.c

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
* ENHANCEMENTS, OR MODIFICATIONS.
3232
*
3333
* IDENTIFICATION
34-
* $PostgreSQL: pgsql/src/pl/tcl/pltcl.c,v 1.81 2004/01/06 23:55:19 tgl Exp $
34+
* $PostgreSQL: pgsql/src/pl/tcl/pltcl.c,v 1.82 2004/01/24 23:06:29 tgl Exp $
3535
*
3636
**********************************************************************/
3737

@@ -695,11 +695,15 @@ pltcl_trigger_handler(PG_FUNCTION_ARGS)
695695
pfree(stroid);
696696

697697
/* A list of attribute names for argument TG_relatts */
698-
/* note: we deliberately include dropped atts here */
699698
Tcl_DStringAppendElement(&tcl_trigtup, "");
700699
for (i = 0; i < tupdesc->natts; i++)
701-
Tcl_DStringAppendElement(&tcl_trigtup,
702-
NameStr(tupdesc->attrs[i]->attname));
700+
{
701+
if (tupdesc->attrs[i]->attisdropped)
702+
Tcl_DStringAppendElement(&tcl_trigtup, "");
703+
else
704+
Tcl_DStringAppendElement(&tcl_trigtup,
705+
NameStr(tupdesc->attrs[i]->attname));
706+
}
703707
Tcl_DStringAppendElement(&tcl_cmd, Tcl_DStringValue(&tcl_trigtup));
704708
Tcl_DStringFree(&tcl_trigtup);
705709
Tcl_DStringInit(&tcl_trigtup);
@@ -881,34 +885,36 @@ pltcl_trigger_handler(PG_FUNCTION_ARGS)
881885
siglongjmp(Warn_restart, 1);
882886
}
883887

884-
i = 0;
885-
while (i < ret_numvals)
888+
for (i = 0; i < ret_numvals; i += 2)
886889
{
890+
CONST84 char *ret_name = ret_values[i];
891+
CONST84 char *ret_value = ret_values[i + 1];
887892
int attnum;
888893
HeapTuple typeTup;
889894
Oid typinput;
890895
Oid typelem;
891896
FmgrInfo finfo;
892897

893898
/************************************************************
894-
* Ignore pseudo elements with a dot name
899+
* Ignore ".tupno" pseudo elements (see pltcl_set_tuple_values)
895900
************************************************************/
896-
if (*(ret_values[i]) == '.')
897-
{
898-
i += 2;
901+
if (strcmp(ret_name, ".tupno") == 0)
899902
continue;
900-
}
901903

902904
/************************************************************
903905
* Get the attribute number
904906
************************************************************/
905-
attnum = SPI_fnumber(tupdesc, ret_values[i++]);
907+
attnum = SPI_fnumber(tupdesc, ret_name);
906908
if (attnum == SPI_ERROR_NOATTRIBUTE)
907-
elog(ERROR, "invalid attribute \"%s\"",
908-
ret_values[--i]);
909+
elog(ERROR, "invalid attribute \"%s\"", ret_name);
909910
if (attnum <= 0)
910-
elog(ERROR, "cannot set system attribute \"%s\"",
911-
ret_values[--i]);
911+
elog(ERROR, "cannot set system attribute \"%s\"", ret_name);
912+
913+
/************************************************************
914+
* Ignore dropped columns
915+
************************************************************/
916+
if (tupdesc->attrs[attnum - 1]->attisdropped)
917+
continue;
912918

913919
/************************************************************
914920
* Lookup the attribute type in the syscache
@@ -932,7 +938,7 @@ pltcl_trigger_handler(PG_FUNCTION_ARGS)
932938
UTF_BEGIN;
933939
modvalues[attnum - 1] =
934940
FunctionCall3(&finfo,
935-
CStringGetDatum(UTF_U2E(ret_values[i++])),
941+
CStringGetDatum(UTF_U2E(ret_value)),
936942
ObjectIdGetDatum(typelem),
937943
Int32GetDatum(tupdesc->attrs[attnum - 1]->atttypmod));
938944
UTF_END;

0 commit comments

Comments
 (0)