2012-01-30 29 views
0

创建触发器我在Ubuntu 10.04一个PostgreSQL 9.0服务器,我尝试创建C代码的触发,之后的下一个环节:在C对PostgreSQL的9

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相同的路径,并有日e文件:executor/spi.hcommands/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.hspi.h等,可能产生的后果。有没有人设置这样的触发器,并可以告诉我我错在哪里?

在此先感谢。

+1

尝试增加'-I.'命令行:'CC -fpic -c -I。 trigger_test.c'。 – 2012-01-30 13:05:09

+0

非常感谢,至少现在编译完成。 – doctore 2012-01-30 13:41:46

回答

1

你忘了,包括你的功能fmgr.h头,所以魔块可以工作poperly