0

I have a PostgreSQL 9.0 server on Ubuntu 10.04, and I try to create an trigger in C code, following the next links:

Documentation, Compile

For the time being, my code should show only the value of the column in a record (and return the "edited" record):

#include "postgres.h"
#include "executor/spi.h"
#include "commands/trigger.h"

#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif

extern Datum trigger_test(PG_FUNCTION_ARGS);

PG_FUNCTION_INFO_V1(trigger_test);

Datum
trigger_test(PG_FUNCTION_ARGS)
{
  TriggerData *trigdata = (TriggerData *) fcinfo->context;
  TupleDesc   tupdesc;
  HeapTuple   rettuple;

  if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
    rettuple = trigdata->tg_newtuple;
  else
    rettuple = trigdata->tg_trigtuple;

  tupdesc = trigdata->tg_relation->rd_att;

  bool isnull = false;
  int64 att = DatumGetInt64(heap_getattr(rettuple, 2, tupdesc, &isnull));

  elog(INFO,"Value second column is: %d",att);
  return PointerGetDatum(rettuple);
}

This file is in the same path of postgres.h and there are the files: executor/spi.h and commands/trigger.h. However, when I run the command:

cc -fpic -c trigger_test.c

I receive the errors:

In file included from postgres.h:48,
from trigger_test.c:1:
   utils/elog.h:69:28: error: utils/errcodes.h: Not exists the file or directory
In file included from trigger_test.c:2:
  executor/spi.h:16:30: error: nodes/parsenodes.h: Not exists the file or directory
  executor/spi.h:17:26: error: utils/portal.h: Not exists the file or directory
  executor/spi.h:18:28: error: utils/relcache.h: Not exists the file or directory
  executor/spi.h:19:28: error: utils/snapshot.h: Not exists the file or directory
...

All files exists and I don't want to change all includes in the files: elog.h, spi.h, etc, for the consequences that might have. Has anyone set up such triggers and can tell me where I'm wrong?

Thanks in advance.

2
  • 1
    Try adding -I. to the command line: cc -fpic -c -I. trigger_test.c. Commented Jan 30, 2012 at 13:05
  • Thanks a lot, at least compiles now. Commented Jan 30, 2012 at 13:41

1 Answer 1

1

You forgot to include fmgr.h header in your function, so the magic block could work poperly

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.