gpt4 book ai didi

c - 在 C 聚合函数中使用 PG_GETARG_POINTER 安全吗?

转载 作者:行者123 更新时间:2023-11-30 16:36:16 26 4
gpt4 key购买 nike

我正在为 PostgreSQL 编写一个 C 聚合函数,目前我使用 PG_GETARG_POINTER 如下:

Datum my_transfunc(PG_FUNCTION_ARGS) {
mystype *trans_data = (mystype *) PG_GETARG_POINTER(0);
// Do some operations on trans_data pointer.
// That make trans_data's memory content change. Then
PG_RETURN_POINTER(trans_data);
}

我的代码是否存在任何问题(内存,...)?

最佳答案

backend/executor/nodeAgg.c 说:

We compute aggregate input expressions and run the transition functions
in a temporary econtext (aggstate->tmpcontext). This is reset at least
once per input tuple, so when the transvalue datatype is
pass-by-reference, we have to be careful to copy it into a longer-lived
memory context, and free the prior value to avoid memory leakage. We
store transvalues in another set of econtexts, aggstate->aggcontexts
(one per grouping set, see below), which are also used for the hashtable
structures in AGG_HASHED mode. These econtexts are rescanned, not just
reset, at group boundaries so that aggregate transition functions can
register shutdown callbacks via AggRegisterCallback.

由于转换值会被复制,因此它必须是一个 Datum (例如 bytea),以便 copyDatum 可以正确复制它.

由于您使用的是指针(可能在 SQL 中声明为internal),所以应该没问题; 内部是按值传递。

您必须确保您的 mystype 分配在一个内存上下文中,该上下文在整个语句中都存在,但随后被清除。

关于c - 在 C 聚合函数中使用 PG_GETARG_POINTER 安全吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48558241/

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