gpt4 book ai didi

c - Postgres C 扩展集合 : How to detect first time aggregate function is called

转载 作者:太空宇宙 更新时间:2023-11-03 23:40:05 24 4
gpt4 key购买 nike

我正在为 PostgreSQL 编写一个 C 扩展聚合函数,在 C 代码中我想知道这是否是第一次调用聚合的转换函数。

比如我定义了一个聚合函数如:

CREATE AGGREGATE my_aggregate (text) (
sfunc = my_transfunc,
stype = text,
finalfunc = my_finalfn,
initcond = '');

那么在my_transfunc的C代码中,我怎么知道my_transfunc是不是第一次被调用(而不是第二次,第三次...)。

Datum my_transfunc(PG_FUNCTION_ARGS) {
// How to check if the first time function called
if (first_time) { then do something }
else { do some other things }
}

我不想在这里使用全局变量或静态变量,因为这使我的函数不是线程安全的,这会严重影响我的函数的要求。

最佳答案

通常这是一个适当设置 initcond 的问题。通常只要算法设计得当,就不需要知道函数是否是第一次执行。

在您的情况下,假设函数返回非空字符串,您可以检查参数是否为空(即等于 initcond)。当然,你可以将initcond设置为一个特殊的值,而不是一个空字符串。

Datum my_transfunc(PG_FUNCTION_ARGS) {
text *arg = PG_GETARG_TEXT_PP(0);
int32 arg_size = VARSIZE_ANY_EXHDR(arg);

if (arg_size == 0) { // arg == initcond }
else { // do some other things }
}

关于c - Postgres C 扩展集合 : How to detect first time aggregate function is called,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48393256/

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