在PostgreSQL 9上用C创建一个触发器

时间:2012-01-30 12:57:30

标签: c postgresql triggers

我在Ubuntu 10.04上有一个PostgreSQL 9.0服务器,我尝试在C代码中创建一个触发器,跟随下一个链接:

DocumentationCompile

目前,我的代码只应显示记录中列的值(并返回“已编辑”记录):

#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);
}

此文件与 postgres.h 的路径相同,并且有以下文件: executor / spi.h commands / trigger.h 。但是,当我运行命令时:

cc -fpic -c trigger_test.c

我收到错误:

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
...

所有文件都存在,我不想更改文件中的所有包含: elog.h spi.h 等,以了解可能产生的后果。有没有人设置这样的触发器,可以告诉我哪里错了?

提前致谢。

1 个答案:

答案 0 :(得分:1)

您忘记在函数中包含fmgr.h标头,因此魔术块可以正常工作