What arguments get passed with PG_FUNCTION_ARGS (with the V1 convention)?

All we need is an easy explanation of the problem, so here it is.

PostgreSQL Documents all their C functions with a "V1" interface, but they don’t actually show what they get,

PG_FUNCTION_INFO_V1(add_one);

Datum
add_one(PG_FUNCTION_ARGS)
{
    int32   arg = PG_GETARG_INT32(0);

    PG_RETURN_INT32(arg + 1);
}

In the above what does PG_FUNCTION_ARGS declare the function to accept? Many V1 function seem to have a fcinfo magically show up in the definition, I’m guessing it’s brought in here but what is it and does anything else get brought in with this macro?

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

The actual definition for this is in fmgr.h,

#define PG_FUNCTION_ARGS    FunctionCallInfo fcinfo

This is a pointer to FunctionCallInfoBaseData

typedef struct FunctionCallInfoBaseData *FunctionCallInfo;

And you can see the definition of that in the same file,

typedef struct FunctionCallInfoBaseData
{
    FmgrInfo   *flinfo;         /* ptr to lookup info used for this call */
    fmNodePtr   context;        /* pass info about context of call */
    fmNodePtr   resultinfo;     /* pass or return extra info about result */
    Oid         fncollation;    /* collation for function to use */
#define FIELDNO_FUNCTIONCALLINFODATA_ISNULL 4
    bool        isnull;         /* function must set true if result is NULL */
    short       nargs;          /* # arguments actually passed */
#define FIELDNO_FUNCTIONCALLINFODATA_ARGS 6
    NullableDatum args[FLEXIBLE_ARRAY_MEMBER];
} FunctionCallInfoBaseData;

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply