在编写PostgreSQL的UDF时是否应该释放DirectFunctionCall函数返回的指针?

时间:2018-08-31 09:34:44

标签: c postgresql user-defined-functions

如标题中所述,我正在用PostgreSQL的C语言编写UDF。我正在通过DirectFunctionCall1DirectFunctionCall2来调用某些函数,...其中一些函数返回POD(例如interval_finitetimestamp_in),而其中一些返回指针(像interval_inint4_to_char)。

问题1:在调用返回指针的函数之后,应该调用pfree释放那些指针吗?

Q2:如果Q1的答案是肯定的,我应该在将指针传递给另一个DirectFunctionCall1函数作为参数之后释放Q1中的那些指针吗?

Interval       *real_interval = NULL;

real_interval = DatumGetIntervalP(DirectFunctionCall2(
                                    timestamp_mi,
                                    TimestampTzGetDatum(timestamp_floor_small_period),
                                    TimestampTzGetDatum(timestamp_floor_big_period)));
// Q1: should I call pfree(real_interval)?

result = DatumGetInt32(
            DirectFunctionCall1(  // Q2: Should I free the returned pointer of DirectFunctionCall?
                numeric_int4,
                DirectFunctionCall2(  // Q2: Should I free the returned pointer of DirectFunctionCall?
                    numeric_div,
                    NumericGetDatum(
                        DatumGetNumeric(
                            DirectFunctionCall1(  // Q2: Should I free the returned pointer of DirectFunctionCall?
                                intervaltonum,
                                IntervalPGetDatum(real_interval)
                            )
                        )
                    ),
                    NumericGetDatum(
                        DatumGetNumeric(
                            DirectFunctionCall1(  // Q2: Should I free the returned pointer of DirectFunctionCall?
                                intervaltonum,
                                IntervalPGetDatum(&small_interval)
                            )
                        )
                    )
                )
            )
        );

0 个答案:

没有答案