modifying new tuple on insert in c trigger

From: "Damjan Pipan" <damjan(dot)pipan(at)xlab(dot)si>
To: <pgsql-general(at)postgresql(dot)org>
Subject: modifying new tuple on insert in c trigger
Date: 2002-11-19 21:52:44
Message-ID: 002801c29015$fd9ad190$6700010a@valhala
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hello!

I'm trying to write a c trigger function which would change an
attribute of tuple being inserted. I have read this value
from some other table and now I have a problem, because
even if I change the tuple with SPI_modifytuple and return
modified tuple it does not show in db (it did not change the tuple)
but tuple is inserted.

the main part of code

--------
HeapTuple tmptuple;
Datum new_value[2];
int attnum[2];
char r[3]=" ";

new_value[0] = SPI_getvalue(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1);
new_value[1] = SPI_getvalue(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1);
attnum[0] = SPI_fnumber(tupdesc, fk_key_c);
attnum[1] = SPI_fnumber(tupdesc, fk_key_c);
tmptuple = SPI_modifytuple(rel, tuple, 1, attnum, new_value, r);
if (tuple == NULL)
elog(ERROR, "pltcl: SPI_modifytuple() failed - RC = %d\n", SPI_result);

return PointerGetDatum(tmptuple);
----

here is the whole function:

-------
PG_FUNCTION_INFO_V1(ccc);

Datum
ccc(PG_FUNCTION_ARGS)
{
TriggerData *trigdata = (TriggerData *) fcinfo->context;
HeapTuple tuple;
HeapTuple tmptuple;
Relation rel; /* triggered relation */
TupleDesc tupdesc; /* tuple description */
char query[200];
int ret;

tuple = trigdata->tg_trigtuple;
rel = trigdata->tg_relation;
tupdesc = rel->rd_att;

SPI_connect();

sprintf(query, "select pk_table, pk_key_i, pk_key_c, fk_table, fk_key_i, fk_key_c FROM c00_doubles WHERE fk_table = '%s';",
SPI_getrelname(trigdata->tg_relation));

if ((ret = SPI_exec(query, 0)) < 0)
elog(ERROR, "ccc (fired %s): SPI_exec returned %d", query, ret);

if (SPI_processed > 0) {
char pk_table[51];
char pk_key_i[51];
char pk_key_c[51];
char fk_table[51];
char fk_key_i[51];
char fk_key_c[51];
int key_i;
char key_c[51];
bool isnull;
TUPLE Tuple = (TUPLE) PG_GETARG_POINTER(0);

strcpy(pk_table, DatumGetCString(SPI_getvalue(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1)));
strcpy(pk_key_i, DatumGetCString(SPI_getvalue(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 2)));
strcpy(pk_key_c, DatumGetCString(SPI_getvalue(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 3)));
strcpy(fk_table, DatumGetCString(SPI_getvalue(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 4)));
strcpy(fk_key_i, DatumGetCString(SPI_getvalue(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 5)));
strcpy(fk_key_c, DatumGetCString(SPI_getvalue(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 6)));

{
bool isnull;
key_i = DatumGetInt32(SPI_getbinval(tuple, tupdesc, SPI_fnumber(tupdesc, fk_key_i), &isnull));

if (isnull)
elog(ERROR, "NULLLLL");
}

sprintf(query, "SELECT %s FROM %s WHERE %s = %d", pk_key_c, pk_table, pk_key_i, key_i);

if ((ret = SPI_exec(query, 0)) < 0)
elog(ERROR, "ccc (fired %s): SPI_exec returned %d", query, ret);

if (SPI_processed > 0) {
strcpy(key_c, DatumGetCString(SPI_getvalue(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1)));
{
// HeapTuple tmptuple;
Datum new_value[2];
int attnum[2];
char r[3]=" ";

new_value[0] = SPI_getvalue(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1);//CStringGetDatum(key_c);
new_value[1] = SPI_getvalue(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1);//CStringGetDatum(key_c);
attnum[0] = SPI_fnumber(tupdesc, fk_key_c);
attnum[1] = SPI_fnumber(tupdesc, fk_key_c);
// elog (ERROR, "'%s'", key_c);
// tmptuple = SPI_copytuple(tuple);
tmptuple = SPI_modifytuple(rel, tuple, 1, attnum, new_value, r);
// SPI_freetuple(tmptuple);
if (tuple == NULL)
elog(ERROR, "pltcl: SPI_modifytuple() failed - RC = %d\n", SPI_result);

}
}
SPI_finish(); /* don't forget say Bye to SPI mgr */

}
// PG_RETURN_BOOL(salary > 699);
return PointerGetDatum(tmptuple);
}

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Andrew Magnus 2002-11-19 21:52:52 Getting a list of tables in a database with Perl
Previous Message Andreas Tille 2002-11-19 21:27:15 Handling images using Python