gpt4 book ai didi

在 PostgreSQL 9 上用 C 创建触发器

转载 作者:行者123 更新时间:2023-11-30 15:59:19 25 4
gpt4 key购买 nike

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

Documentation , Compile

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

#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.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 等,以免产生可能的后果。有没有人设置了这样的触发器并可以告诉我哪里错了?

提前致谢。

最佳答案

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

关于在 PostgreSQL 9 上用 C 创建触发器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9064246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com