0

我编写了以下内容来获取序列中的下一个值。它完美地工作:

static int64 _get_md_key_next_serial()
{
    int                  ret = SPI_execute("SELECT nextval('md_key_seq')", true, 1);

    if (ret <= 0)
        return (int64)ret;

    if (SPI_processed)
    {
        SPITupleTable   *tuptable = SPI_tuptable;
        bool             fieldNull;
        Datum            datum = SPI_getbinval(tuptable->vals[0], tuptable->tupdesc, 1, &fieldNull);

        if (!fieldNull)
            return DatumGetInt64(datum);
    }

    return NULL_ZERO;
}

但是,肯定有一个函数调用我可以不必通过 SPI 进行吗?

4

1 回答 1

2

不幸nextval_internal的是没有导出,但您可以尝试调用nextval_oid. 我没有测试这段代码,所以你可能需要调试它:

#include "fmgr.h"
#include "commands/sequence.h"

static int64 nextval(Oid sequenceID, FunctionCallInfo fcinfo)
{
    FunctionCallInfoData locfcinfo;

    InitFunctionCallInfoData(locfcinfo, fcinfo->flinfo, 1,
                             InvalidOid, NULL, NULL);
    locfcinfo.arg[0] = ObjectIdGetDatum(sequenceID);
    locfcinfo.argnull[0] = false;

    return DatumGetInt64(nextval_oid(&locfcinfo));
}

传递Oid 序列的 和fcinfo来自您自己的 SQL 函数。

于 2016-10-05T14:14:10.653 回答