1 /*------------------------------------------------------------------------- 2 * 3 * fmgr.h 4 * Definitions for the Postgres function manager and function-call 5 * interface. 6 * 7 * This file must be included by all Postgres modules that either define 8 * or call fmgr-callable functions. 9 * 10 * 11 * Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group 12 * Portions Copyright (c) 1994, Regents of the University of California 13 * 14 * src/include/fmgr.h 15 * 16 *------------------------------------------------------------------------- 17 */ 18 #ifndef FMGR_H 19 #define FMGR_H 20 21 /* We don't want to include primnodes.h here, so make some stub references */ 22 typedef struct Node *fmNodePtr; 23 typedef struct Aggref *fmAggrefPtr; 24 25 /* Likewise, avoid including execnodes.h here */ 26 typedef void (*fmExprContextCallbackFunction) (Datum arg); 27 28 /* Likewise, avoid including stringinfo.h here */ 29 typedef struct StringInfoData *fmStringInfo; 30 31 32 /* 33 * All functions that can be called directly by fmgr must have this signature. 34 * (Other functions can be called by using a handler that does have this 35 * signature.) 36 */ 37 38 typedef struct FunctionCallInfoData *FunctionCallInfo; 39 40 typedef Datum (*PGFunction) (FunctionCallInfo fcinfo); 41 42 /* 43 * This struct holds the system-catalog information that must be looked up 44 * before a function can be called through fmgr. If the same function is 45 * to be called multiple times, the lookup need be done only once and the 46 * info struct saved for re-use. 47 * 48 * Note that fn_expr really is parse-time-determined information about the 49 * arguments, rather than about the function itself. But it's convenient 50 * to store it here rather than in FunctionCallInfoData, where it might more 51 * logically belong. 52 * 53 * fn_extra is available for use by the called function; all other fields 54 * should be treated as read-only after the struct is created. 55 */ 56 typedef struct FmgrInfo 57 { 58 PGFunction fn_addr; /* pointer to function or handler to be called */ 59 Oid fn_oid; /* OID of function (NOT of handler, if any) */ 60 short fn_nargs; /* number of input args (0..FUNC_MAX_ARGS) */ 61 bool fn_strict; /* function is "strict" (NULL in => NULL out) */ 62 bool fn_retset; /* function returns a set */ 63 unsigned char fn_stats; /* collect stats if track_functions > this */ 64 void *fn_extra; /* extra space for use by handler */ 65 MemoryContext fn_mcxt; /* memory context to store fn_extra in */ 66 fmNodePtr fn_expr; /* expression parse tree for call, or NULL */ 67 } FmgrInfo; 68 69 /* 70 * This struct is the data actually passed to an fmgr-called function. 71 * 72 * The called function is expected to set isnull, and possibly resultinfo or 73 * fields in whatever resultinfo points to. It should not change any other 74 * fields. (In particular, scribbling on the argument arrays is a bad idea, 75 * since some callers assume they can re-call with the same arguments.) 76 */ 77 typedef struct FunctionCallInfoData 78 { 79 FmgrInfo *flinfo; /* ptr to lookup info used for this call */ 80 fmNodePtr context; /* pass info about context of call */ 81 fmNodePtr resultinfo; /* pass or return extra info about result */ 82 Oid fncollation; /* collation for function to use */ 83 #define FIELDNO_FUNCTIONCALLINFODATA_ISNULL 4 84 bool isnull; /* function must set true if result is NULL */ 85 short nargs; /* # arguments actually passed */ 86 #define FIELDNO_FUNCTIONCALLINFODATA_ARG 6 87 Datum arg[FUNC_MAX_ARGS]; /* Arguments passed to function */ 88 #define FIELDNO_FUNCTIONCALLINFODATA_ARGNULL 7 89 bool argnull[FUNC_MAX_ARGS]; /* T if arg[i] is actually NULL */ 90 } FunctionCallInfoData; 91 92 /* 93 * This routine fills a FmgrInfo struct, given the OID 94 * of the function to be called. 95 */ 96 extern void fmgr_info(Oid functionId, FmgrInfo *finfo); 97 98 /* 99 * Same, when the FmgrInfo struct is in a memory context longer-lived than 100 * CurrentMemoryContext. The specified context will be set as fn_mcxt 101 * and used to hold all subsidiary data of finfo. 102 */ 103 extern void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, 104 MemoryContext mcxt); 105 106 /* Convenience macro for setting the fn_expr field */ 107 #define fmgr_info_set_expr(expr, finfo) \ 108 ((finfo)->fn_expr = (expr)) 109 110 /* 111 * Copy an FmgrInfo struct 112 */ 113 extern void fmgr_info_copy(FmgrInfo *dstinfo, FmgrInfo *srcinfo, 114 MemoryContext destcxt); 115 116 extern void fmgr_symbol(Oid functionId, char **mod, char **fn); 117 118 /* 119 * This macro initializes all the fields of a FunctionCallInfoData except 120 * for the arg[] and argnull[] arrays. Performance testing has shown that 121 * the fastest way to set up argnull[] for small numbers of arguments is to 122 * explicitly set each required element to false, so we don't try to zero 123 * out the argnull[] array in the macro. 124 */ 125 #define InitFunctionCallInfoData(Fcinfo, Flinfo, Nargs, Collation, Context, Resultinfo) \ 126 do { \ 127 (Fcinfo).flinfo = (Flinfo); \ 128 (Fcinfo).context = (Context); \ 129 (Fcinfo).resultinfo = (Resultinfo); \ 130 (Fcinfo).fncollation = (Collation); \ 131 (Fcinfo).isnull = false; \ 132 (Fcinfo).nargs = (Nargs); \ 133 } while (0) 134 135 /* 136 * This macro invokes a function given a filled-in FunctionCallInfoData 137 * struct. The macro result is the returned Datum --- but note that 138 * caller must still check fcinfo->isnull! Also, if function is strict, 139 * it is caller's responsibility to verify that no null arguments are present 140 * before calling. 141 * 142 * Some code performs multiple calls without redoing InitFunctionCallInfoData, 143 * possibly altering the argument values. This is okay, but be sure to reset 144 * the fcinfo->isnull flag before each call, since callees are permitted to 145 * assume that starts out false. 146 */ 147 #define FunctionCallInvoke(fcinfo) ((* (fcinfo)->flinfo->fn_addr) (fcinfo)) 148 149 150 /*------------------------------------------------------------------------- 151 * Support macros to ease writing fmgr-compatible functions 152 * 153 * A C-coded fmgr-compatible function should be declared as 154 * 155 * Datum 156 * function_name(PG_FUNCTION_ARGS) 157 * { 158 * ... 159 * } 160 * 161 * It should access its arguments using appropriate PG_GETARG_xxx macros 162 * and should return its result using PG_RETURN_xxx. 163 * 164 *------------------------------------------------------------------------- 165 */ 166 167 /* Standard parameter list for fmgr-compatible functions */ 168 #define PG_FUNCTION_ARGS FunctionCallInfo fcinfo 169 170 /* 171 * Get collation function should use. 172 */ 173 #define PG_GET_COLLATION() (fcinfo->fncollation) 174 175 /* 176 * Get number of arguments passed to function. 177 */ 178 #define PG_NARGS() (fcinfo->nargs) 179 180 /* 181 * If function is not marked "proisstrict" in pg_proc, it must check for 182 * null arguments using this macro. Do not try to GETARG a null argument! 183 */ 184 #define PG_ARGISNULL(n) (fcinfo->argnull[n]) 185 186 /* 187 * Support for fetching detoasted copies of toastable datatypes (all of 188 * which are varlena types). pg_detoast_datum() gives you either the input 189 * datum (if not toasted) or a detoasted copy allocated with palloc(). 190 * pg_detoast_datum_copy() always gives you a palloc'd copy --- use it 191 * if you need a modifiable copy of the input. Caller is expected to have 192 * checked for null inputs first, if necessary. 193 * 194 * pg_detoast_datum_packed() will return packed (1-byte header) datums 195 * unmodified. It will still expand an externally toasted or compressed datum. 196 * The resulting datum can be accessed using VARSIZE_ANY() and VARDATA_ANY() 197 * (beware of multiple evaluations in those macros!) 198 * 199 * In consumers oblivious to data alignment, call PG_DETOAST_DATUM_PACKED(), 200 * VARDATA_ANY(), VARSIZE_ANY() and VARSIZE_ANY_EXHDR(). Elsewhere, call 201 * PG_DETOAST_DATUM(), VARDATA() and VARSIZE(). Directly fetching an int16, 202 * int32 or wider field in the struct representing the datum layout requires 203 * aligned data. memcpy() is alignment-oblivious, as are most operations on 204 * datatypes, such as text, whose layout struct contains only char fields. 205 * 206 * Note: it'd be nice if these could be macros, but I see no way to do that 207 * without evaluating the arguments multiple times, which is NOT acceptable. 208 */ 209 extern struct varlena *pg_detoast_datum(struct varlena *datum); 210 extern struct varlena *pg_detoast_datum_copy(struct varlena *datum); 211 extern struct varlena *pg_detoast_datum_slice(struct varlena *datum, 212 int32 first, int32 count); 213 extern struct varlena *pg_detoast_datum_packed(struct varlena *datum); 214 215 #define PG_DETOAST_DATUM(datum) \ 216 pg_detoast_datum((struct varlena *) DatumGetPointer(datum)) 217 #define PG_DETOAST_DATUM_COPY(datum) \ 218 pg_detoast_datum_copy((struct varlena *) DatumGetPointer(datum)) 219 #define PG_DETOAST_DATUM_SLICE(datum,f,c) \ 220 pg_detoast_datum_slice((struct varlena *) DatumGetPointer(datum), \ 221 (int32) (f), (int32) (c)) 222 /* WARNING -- unaligned pointer */ 223 #define PG_DETOAST_DATUM_PACKED(datum) \ 224 pg_detoast_datum_packed((struct varlena *) DatumGetPointer(datum)) 225 226 /* 227 * Support for cleaning up detoasted copies of inputs. This must only 228 * be used for pass-by-ref datatypes, and normally would only be used 229 * for toastable types. If the given pointer is different from the 230 * original argument, assume it's a palloc'd detoasted copy, and pfree it. 231 * NOTE: most functions on toastable types do not have to worry about this, 232 * but we currently require that support functions for indexes not leak 233 * memory. 234 */ 235 #define PG_FREE_IF_COPY(ptr,n) \ 236 do { \ 237 if ((Pointer) (ptr) != PG_GETARG_POINTER(n)) \ 238 pfree(ptr); \ 239 } while (0) 240 241 /* Macros for fetching arguments of standard types */ 242 243 #define PG_GETARG_DATUM(n) (fcinfo->arg[n]) 244 #define PG_GETARG_INT32(n) DatumGetInt32(PG_GETARG_DATUM(n)) 245 #define PG_GETARG_UINT32(n) DatumGetUInt32(PG_GETARG_DATUM(n)) 246 #define PG_GETARG_INT16(n) DatumGetInt16(PG_GETARG_DATUM(n)) 247 #define PG_GETARG_UINT16(n) DatumGetUInt16(PG_GETARG_DATUM(n)) 248 #define PG_GETARG_CHAR(n) DatumGetChar(PG_GETARG_DATUM(n)) 249 #define PG_GETARG_BOOL(n) DatumGetBool(PG_GETARG_DATUM(n)) 250 #define PG_GETARG_OID(n) DatumGetObjectId(PG_GETARG_DATUM(n)) 251 #define PG_GETARG_POINTER(n) DatumGetPointer(PG_GETARG_DATUM(n)) 252 #define PG_GETARG_CSTRING(n) DatumGetCString(PG_GETARG_DATUM(n)) 253 #define PG_GETARG_NAME(n) DatumGetName(PG_GETARG_DATUM(n)) 254 /* these macros hide the pass-by-reference-ness of the datatype: */ 255 #define PG_GETARG_FLOAT4(n) DatumGetFloat4(PG_GETARG_DATUM(n)) 256 #define PG_GETARG_FLOAT8(n) DatumGetFloat8(PG_GETARG_DATUM(n)) 257 #define PG_GETARG_INT64(n) DatumGetInt64(PG_GETARG_DATUM(n)) 258 /* use this if you want the raw, possibly-toasted input datum: */ 259 #define PG_GETARG_RAW_VARLENA_P(n) ((struct varlena *) PG_GETARG_POINTER(n)) 260 /* use this if you want the input datum de-toasted: */ 261 #define PG_GETARG_VARLENA_P(n) PG_DETOAST_DATUM(PG_GETARG_DATUM(n)) 262 /* and this if you can handle 1-byte-header datums: */ 263 #define PG_GETARG_VARLENA_PP(n) PG_DETOAST_DATUM_PACKED(PG_GETARG_DATUM(n)) 264 /* DatumGetFoo macros for varlena types will typically look like this: */ 265 #define DatumGetByteaPP(X) ((bytea *) PG_DETOAST_DATUM_PACKED(X)) 266 #define DatumGetTextPP(X) ((text *) PG_DETOAST_DATUM_PACKED(X)) 267 #define DatumGetBpCharPP(X) ((BpChar *) PG_DETOAST_DATUM_PACKED(X)) 268 #define DatumGetVarCharPP(X) ((VarChar *) PG_DETOAST_DATUM_PACKED(X)) 269 #define DatumGetHeapTupleHeader(X) ((HeapTupleHeader) PG_DETOAST_DATUM(X)) 270 /* And we also offer variants that return an OK-to-write copy */ 271 #define DatumGetByteaPCopy(X) ((bytea *) PG_DETOAST_DATUM_COPY(X)) 272 #define DatumGetTextPCopy(X) ((text *) PG_DETOAST_DATUM_COPY(X)) 273 #define DatumGetBpCharPCopy(X) ((BpChar *) PG_DETOAST_DATUM_COPY(X)) 274 #define DatumGetVarCharPCopy(X) ((VarChar *) PG_DETOAST_DATUM_COPY(X)) 275 #define DatumGetHeapTupleHeaderCopy(X) ((HeapTupleHeader) PG_DETOAST_DATUM_COPY(X)) 276 /* Variants which return n bytes starting at pos. m */ 277 #define DatumGetByteaPSlice(X,m,n) ((bytea *) PG_DETOAST_DATUM_SLICE(X,m,n)) 278 #define DatumGetTextPSlice(X,m,n) ((text *) PG_DETOAST_DATUM_SLICE(X,m,n)) 279 #define DatumGetBpCharPSlice(X,m,n) ((BpChar *) PG_DETOAST_DATUM_SLICE(X,m,n)) 280 #define DatumGetVarCharPSlice(X,m,n) ((VarChar *) PG_DETOAST_DATUM_SLICE(X,m,n)) 281 /* GETARG macros for varlena types will typically look like this: */ 282 #define PG_GETARG_BYTEA_PP(n) DatumGetByteaPP(PG_GETARG_DATUM(n)) 283 #define PG_GETARG_TEXT_PP(n) DatumGetTextPP(PG_GETARG_DATUM(n)) 284 #define PG_GETARG_BPCHAR_PP(n) DatumGetBpCharPP(PG_GETARG_DATUM(n)) 285 #define PG_GETARG_VARCHAR_PP(n) DatumGetVarCharPP(PG_GETARG_DATUM(n)) 286 #define PG_GETARG_HEAPTUPLEHEADER(n) DatumGetHeapTupleHeader(PG_GETARG_DATUM(n)) 287 /* And we also offer variants that return an OK-to-write copy */ 288 #define PG_GETARG_BYTEA_P_COPY(n) DatumGetByteaPCopy(PG_GETARG_DATUM(n)) 289 #define PG_GETARG_TEXT_P_COPY(n) DatumGetTextPCopy(PG_GETARG_DATUM(n)) 290 #define PG_GETARG_BPCHAR_P_COPY(n) DatumGetBpCharPCopy(PG_GETARG_DATUM(n)) 291 #define PG_GETARG_VARCHAR_P_COPY(n) DatumGetVarCharPCopy(PG_GETARG_DATUM(n)) 292 #define PG_GETARG_HEAPTUPLEHEADER_COPY(n) DatumGetHeapTupleHeaderCopy(PG_GETARG_DATUM(n)) 293 /* And a b-byte slice from position a -also OK to write */ 294 #define PG_GETARG_BYTEA_P_SLICE(n,a,b) DatumGetByteaPSlice(PG_GETARG_DATUM(n),a,b) 295 #define PG_GETARG_TEXT_P_SLICE(n,a,b) DatumGetTextPSlice(PG_GETARG_DATUM(n),a,b) 296 #define PG_GETARG_BPCHAR_P_SLICE(n,a,b) DatumGetBpCharPSlice(PG_GETARG_DATUM(n),a,b) 297 #define PG_GETARG_VARCHAR_P_SLICE(n,a,b) DatumGetVarCharPSlice(PG_GETARG_DATUM(n),a,b) 298 /* 299 * Obsolescent variants that guarantee INT alignment for the return value. 300 * Few operations on these particular types need alignment, mainly operations 301 * that cast the VARDATA pointer to a type like int16[]. Most code should use 302 * the ...PP(X) counterpart. Nonetheless, these appear frequently in code 303 * predating the PostgreSQL 8.3 introduction of the ...PP(X) variants. 304 */ 305 #define DatumGetByteaP(X) ((bytea *) PG_DETOAST_DATUM(X)) 306 #define DatumGetTextP(X) ((text *) PG_DETOAST_DATUM(X)) 307 #define DatumGetBpCharP(X) ((BpChar *) PG_DETOAST_DATUM(X)) 308 #define DatumGetVarCharP(X) ((VarChar *) PG_DETOAST_DATUM(X)) 309 #define PG_GETARG_BYTEA_P(n) DatumGetByteaP(PG_GETARG_DATUM(n)) 310 #define PG_GETARG_TEXT_P(n) DatumGetTextP(PG_GETARG_DATUM(n)) 311 #define PG_GETARG_BPCHAR_P(n) DatumGetBpCharP(PG_GETARG_DATUM(n)) 312 #define PG_GETARG_VARCHAR_P(n) DatumGetVarCharP(PG_GETARG_DATUM(n)) 313 314 /* To return a NULL do this: */ 315 #define PG_RETURN_NULL() \ 316 do { fcinfo->isnull = true; return (Datum) 0; } while (0) 317 318 /* A few internal functions return void (which is not the same as NULL!) */ 319 #define PG_RETURN_VOID() return (Datum) 0 320 321 /* Macros for returning results of standard types */ 322 323 #define PG_RETURN_DATUM(x) return (x) 324 #define PG_RETURN_INT32(x) return Int32GetDatum(x) 325 #define PG_RETURN_UINT32(x) return UInt32GetDatum(x) 326 #define PG_RETURN_INT16(x) return Int16GetDatum(x) 327 #define PG_RETURN_UINT16(x) return UInt16GetDatum(x) 328 #define PG_RETURN_CHAR(x) return CharGetDatum(x) 329 #define PG_RETURN_BOOL(x) return BoolGetDatum(x) 330 #define PG_RETURN_OID(x) return ObjectIdGetDatum(x) 331 #define PG_RETURN_POINTER(x) return PointerGetDatum(x) 332 #define PG_RETURN_CSTRING(x) return CStringGetDatum(x) 333 #define PG_RETURN_NAME(x) return NameGetDatum(x) 334 /* these macros hide the pass-by-reference-ness of the datatype: */ 335 #define PG_RETURN_FLOAT4(x) return Float4GetDatum(x) 336 #define PG_RETURN_FLOAT8(x) return Float8GetDatum(x) 337 #define PG_RETURN_INT64(x) return Int64GetDatum(x) 338 #define PG_RETURN_UINT64(x) return UInt64GetDatum(x) 339 /* RETURN macros for other pass-by-ref types will typically look like this: */ 340 #define PG_RETURN_BYTEA_P(x) PG_RETURN_POINTER(x) 341 #define PG_RETURN_TEXT_P(x) PG_RETURN_POINTER(x) 342 #define PG_RETURN_BPCHAR_P(x) PG_RETURN_POINTER(x) 343 #define PG_RETURN_VARCHAR_P(x) PG_RETURN_POINTER(x) 344 #define PG_RETURN_HEAPTUPLEHEADER(x) return HeapTupleHeaderGetDatum(x) 345 346 347 /*------------------------------------------------------------------------- 348 * Support for detecting call convention of dynamically-loaded functions 349 * 350 * Dynamically loaded functions currently can only use the version-1 ("new 351 * style") calling convention. Version-0 ("old style") is not supported 352 * anymore. Version 1 is the call convention defined in this header file, and 353 * must be accompanied by the macro call 354 * 355 * PG_FUNCTION_INFO_V1(function_name); 356 * 357 * Note that internal functions do not need this decoration since they are 358 * assumed to be version-1. 359 * 360 *------------------------------------------------------------------------- 361 */ 362 363 typedef struct 364 { 365 int api_version; /* specifies call convention version number */ 366 /* More fields may be added later, for version numbers > 1. */ 367 } Pg_finfo_record; 368 369 /* Expected signature of an info function */ 370 typedef const Pg_finfo_record *(*PGFInfoFunction) (void); 371 372 /* 373 * Macro to build an info function associated with the given function name. 374 * 375 * As a convenience, also provide an "extern" declaration for the given 376 * function name, so that writers of C functions need not write that too. 377 * 378 * On Windows, the function and info function must be exported. Our normal 379 * build processes take care of that via .DEF files or --export-all-symbols. 380 * Module authors using a different build process might need to manually 381 * declare the function PGDLLEXPORT. We do that automatically here for the 382 * info function, since authors shouldn't need to be explicitly aware of it. 383 */ 384 #define PG_FUNCTION_INFO_V1(funcname) \ 385 extern Datum funcname(PG_FUNCTION_ARGS); \ 386 extern PGDLLEXPORT const Pg_finfo_record * CppConcat(pg_finfo_,funcname)(void); \ 387 const Pg_finfo_record * \ 388 CppConcat(pg_finfo_,funcname) (void) \ 389 { \ 390 static const Pg_finfo_record my_finfo = { 1 }; \ 391 return &my_finfo; \ 392 } \ 393 extern int no_such_variable 394 395 396 /*------------------------------------------------------------------------- 397 * Support for verifying backend compatibility of loaded modules 398 * 399 * We require dynamically-loaded modules to include the macro call 400 * PG_MODULE_MAGIC; 401 * so that we can check for obvious incompatibility, such as being compiled 402 * for a different major PostgreSQL version. 403 * 404 * To compile with versions of PostgreSQL that do not support this, 405 * you may put an #ifdef/#endif test around it. Note that in a multiple- 406 * source-file module, the macro call should only appear once. 407 * 408 * The specific items included in the magic block are intended to be ones that 409 * are custom-configurable and especially likely to break dynamically loaded 410 * modules if they were compiled with other values. Also, the length field 411 * can be used to detect definition changes. 412 * 413 * Note: we compare magic blocks with memcmp(), so there had better not be 414 * any alignment pad bytes in them. 415 * 416 * Note: when changing the contents of magic blocks, be sure to adjust the 417 * incompatible_module_error() function in dfmgr.c. 418 *------------------------------------------------------------------------- 419 */ 420 421 /* Definition of the magic block structure */ 422 typedef struct 423 { 424 int len; /* sizeof(this struct) */ 425 int version; /* PostgreSQL major version */ 426 int funcmaxargs; /* FUNC_MAX_ARGS */ 427 int indexmaxkeys; /* INDEX_MAX_KEYS */ 428 int namedatalen; /* NAMEDATALEN */ 429 int float4byval; /* FLOAT4PASSBYVAL */ 430 int float8byval; /* FLOAT8PASSBYVAL */ 431 } Pg_magic_struct; 432 433 /* The actual data block contents */ 434 #define PG_MODULE_MAGIC_DATA \ 435 { \ 436 sizeof(Pg_magic_struct), \ 437 PG_VERSION_NUM / 100, \ 438 FUNC_MAX_ARGS, \ 439 INDEX_MAX_KEYS, \ 440 NAMEDATALEN, \ 441 FLOAT4PASSBYVAL, \ 442 FLOAT8PASSBYVAL \ 443 } 444 445 /* 446 * Declare the module magic function. It needs to be a function as the dlsym 447 * in the backend is only guaranteed to work on functions, not data 448 */ 449 typedef const Pg_magic_struct *(*PGModuleMagicFunction) (void); 450 451 #define PG_MAGIC_FUNCTION_NAME Pg_magic_func 452 #define PG_MAGIC_FUNCTION_NAME_STRING "Pg_magic_func" 453 454 #define PG_MODULE_MAGIC \ 455 extern PGDLLEXPORT const Pg_magic_struct *PG_MAGIC_FUNCTION_NAME(void); \ 456 const Pg_magic_struct * \ 457 PG_MAGIC_FUNCTION_NAME(void) \ 458 { \ 459 static const Pg_magic_struct Pg_magic_data = PG_MODULE_MAGIC_DATA; \ 460 return &Pg_magic_data; \ 461 } \ 462 extern int no_such_variable 463 464 465 /*------------------------------------------------------------------------- 466 * Support routines and macros for callers of fmgr-compatible functions 467 *------------------------------------------------------------------------- 468 */ 469 470 /* These are for invocation of a specifically named function with a 471 * directly-computed parameter list. Note that neither arguments nor result 472 * are allowed to be NULL. 473 */ 474 extern Datum DirectFunctionCall1Coll(PGFunction func, Oid collation, 475 Datum arg1); 476 extern Datum DirectFunctionCall2Coll(PGFunction func, Oid collation, 477 Datum arg1, Datum arg2); 478 extern Datum DirectFunctionCall3Coll(PGFunction func, Oid collation, 479 Datum arg1, Datum arg2, 480 Datum arg3); 481 extern Datum DirectFunctionCall4Coll(PGFunction func, Oid collation, 482 Datum arg1, Datum arg2, 483 Datum arg3, Datum arg4); 484 extern Datum DirectFunctionCall5Coll(PGFunction func, Oid collation, 485 Datum arg1, Datum arg2, 486 Datum arg3, Datum arg4, Datum arg5); 487 extern Datum DirectFunctionCall6Coll(PGFunction func, Oid collation, 488 Datum arg1, Datum arg2, 489 Datum arg3, Datum arg4, Datum arg5, 490 Datum arg6); 491 extern Datum DirectFunctionCall7Coll(PGFunction func, Oid collation, 492 Datum arg1, Datum arg2, 493 Datum arg3, Datum arg4, Datum arg5, 494 Datum arg6, Datum arg7); 495 extern Datum DirectFunctionCall8Coll(PGFunction func, Oid collation, 496 Datum arg1, Datum arg2, 497 Datum arg3, Datum arg4, Datum arg5, 498 Datum arg6, Datum arg7, Datum arg8); 499 extern Datum DirectFunctionCall9Coll(PGFunction func, Oid collation, 500 Datum arg1, Datum arg2, 501 Datum arg3, Datum arg4, Datum arg5, 502 Datum arg6, Datum arg7, Datum arg8, 503 Datum arg9); 504 505 /* 506 * These functions work like the DirectFunctionCall functions except that 507 * they use the flinfo parameter to initialise the fcinfo for the call. 508 * It's recommended that the callee only use the fn_extra and fn_mcxt 509 * fields, as other fields will typically describe the calling function 510 * not the callee. Conversely, the calling function should not have 511 * used fn_extra, unless its use is known to be compatible with the callee's. 512 */ 513 extern Datum CallerFInfoFunctionCall1(PGFunction func, FmgrInfo *flinfo, 514 Oid collation, Datum arg1); 515 extern Datum CallerFInfoFunctionCall2(PGFunction func, FmgrInfo *flinfo, 516 Oid collation, Datum arg1, Datum arg2); 517 518 /* These are for invocation of a previously-looked-up function with a 519 * directly-computed parameter list. Note that neither arguments nor result 520 * are allowed to be NULL. 521 */ 522 extern Datum FunctionCall1Coll(FmgrInfo *flinfo, Oid collation, 523 Datum arg1); 524 extern Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, 525 Datum arg1, Datum arg2); 526 extern Datum FunctionCall3Coll(FmgrInfo *flinfo, Oid collation, 527 Datum arg1, Datum arg2, 528 Datum arg3); 529 extern Datum FunctionCall4Coll(FmgrInfo *flinfo, Oid collation, 530 Datum arg1, Datum arg2, 531 Datum arg3, Datum arg4); 532 extern Datum FunctionCall5Coll(FmgrInfo *flinfo, Oid collation, 533 Datum arg1, Datum arg2, 534 Datum arg3, Datum arg4, Datum arg5); 535 extern Datum FunctionCall6Coll(FmgrInfo *flinfo, Oid collation, 536 Datum arg1, Datum arg2, 537 Datum arg3, Datum arg4, Datum arg5, 538 Datum arg6); 539 extern Datum FunctionCall7Coll(FmgrInfo *flinfo, Oid collation, 540 Datum arg1, Datum arg2, 541 Datum arg3, Datum arg4, Datum arg5, 542 Datum arg6, Datum arg7); 543 extern Datum FunctionCall8Coll(FmgrInfo *flinfo, Oid collation, 544 Datum arg1, Datum arg2, 545 Datum arg3, Datum arg4, Datum arg5, 546 Datum arg6, Datum arg7, Datum arg8); 547 extern Datum FunctionCall9Coll(FmgrInfo *flinfo, Oid collation, 548 Datum arg1, Datum arg2, 549 Datum arg3, Datum arg4, Datum arg5, 550 Datum arg6, Datum arg7, Datum arg8, 551 Datum arg9); 552 553 /* These are for invocation of a function identified by OID with a 554 * directly-computed parameter list. Note that neither arguments nor result 555 * are allowed to be NULL. These are essentially fmgr_info() followed by 556 * FunctionCallN(). If the same function is to be invoked repeatedly, do the 557 * fmgr_info() once and then use FunctionCallN(). 558 */ 559 extern Datum OidFunctionCall0Coll(Oid functionId, Oid collation); 560 extern Datum OidFunctionCall1Coll(Oid functionId, Oid collation, 561 Datum arg1); 562 extern Datum OidFunctionCall2Coll(Oid functionId, Oid collation, 563 Datum arg1, Datum arg2); 564 extern Datum OidFunctionCall3Coll(Oid functionId, Oid collation, 565 Datum arg1, Datum arg2, 566 Datum arg3); 567 extern Datum OidFunctionCall4Coll(Oid functionId, Oid collation, 568 Datum arg1, Datum arg2, 569 Datum arg3, Datum arg4); 570 extern Datum OidFunctionCall5Coll(Oid functionId, Oid collation, 571 Datum arg1, Datum arg2, 572 Datum arg3, Datum arg4, Datum arg5); 573 extern Datum OidFunctionCall6Coll(Oid functionId, Oid collation, 574 Datum arg1, Datum arg2, 575 Datum arg3, Datum arg4, Datum arg5, 576 Datum arg6); 577 extern Datum OidFunctionCall7Coll(Oid functionId, Oid collation, 578 Datum arg1, Datum arg2, 579 Datum arg3, Datum arg4, Datum arg5, 580 Datum arg6, Datum arg7); 581 extern Datum OidFunctionCall8Coll(Oid functionId, Oid collation, 582 Datum arg1, Datum arg2, 583 Datum arg3, Datum arg4, Datum arg5, 584 Datum arg6, Datum arg7, Datum arg8); 585 extern Datum OidFunctionCall9Coll(Oid functionId, Oid collation, 586 Datum arg1, Datum arg2, 587 Datum arg3, Datum arg4, Datum arg5, 588 Datum arg6, Datum arg7, Datum arg8, 589 Datum arg9); 590 591 /* These macros allow the collation argument to be omitted (with a default of 592 * InvalidOid, ie, no collation). They exist mostly for backwards 593 * compatibility of source code. 594 */ 595 #define DirectFunctionCall1(func, arg1) \ 596 DirectFunctionCall1Coll(func, InvalidOid, arg1) 597 #define DirectFunctionCall2(func, arg1, arg2) \ 598 DirectFunctionCall2Coll(func, InvalidOid, arg1, arg2) 599 #define DirectFunctionCall3(func, arg1, arg2, arg3) \ 600 DirectFunctionCall3Coll(func, InvalidOid, arg1, arg2, arg3) 601 #define DirectFunctionCall4(func, arg1, arg2, arg3, arg4) \ 602 DirectFunctionCall4Coll(func, InvalidOid, arg1, arg2, arg3, arg4) 603 #define DirectFunctionCall5(func, arg1, arg2, arg3, arg4, arg5) \ 604 DirectFunctionCall5Coll(func, InvalidOid, arg1, arg2, arg3, arg4, arg5) 605 #define DirectFunctionCall6(func, arg1, arg2, arg3, arg4, arg5, arg6) \ 606 DirectFunctionCall6Coll(func, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6) 607 #define DirectFunctionCall7(func, arg1, arg2, arg3, arg4, arg5, arg6, arg7) \ 608 DirectFunctionCall7Coll(func, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6, arg7) 609 #define DirectFunctionCall8(func, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8) \ 610 DirectFunctionCall8Coll(func, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8) 611 #define DirectFunctionCall9(func, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9) \ 612 DirectFunctionCall9Coll(func, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9) 613 #define FunctionCall1(flinfo, arg1) \ 614 FunctionCall1Coll(flinfo, InvalidOid, arg1) 615 #define FunctionCall2(flinfo, arg1, arg2) \ 616 FunctionCall2Coll(flinfo, InvalidOid, arg1, arg2) 617 #define FunctionCall3(flinfo, arg1, arg2, arg3) \ 618 FunctionCall3Coll(flinfo, InvalidOid, arg1, arg2, arg3) 619 #define FunctionCall4(flinfo, arg1, arg2, arg3, arg4) \ 620 FunctionCall4Coll(flinfo, InvalidOid, arg1, arg2, arg3, arg4) 621 #define FunctionCall5(flinfo, arg1, arg2, arg3, arg4, arg5) \ 622 FunctionCall5Coll(flinfo, InvalidOid, arg1, arg2, arg3, arg4, arg5) 623 #define FunctionCall6(flinfo, arg1, arg2, arg3, arg4, arg5, arg6) \ 624 FunctionCall6Coll(flinfo, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6) 625 #define FunctionCall7(flinfo, arg1, arg2, arg3, arg4, arg5, arg6, arg7) \ 626 FunctionCall7Coll(flinfo, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6, arg7) 627 #define FunctionCall8(flinfo, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8) \ 628 FunctionCall8Coll(flinfo, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8) 629 #define FunctionCall9(flinfo, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9) \ 630 FunctionCall9Coll(flinfo, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9) 631 #define OidFunctionCall0(functionId) \ 632 OidFunctionCall0Coll(functionId, InvalidOid) 633 #define OidFunctionCall1(functionId, arg1) \ 634 OidFunctionCall1Coll(functionId, InvalidOid, arg1) 635 #define OidFunctionCall2(functionId, arg1, arg2) \ 636 OidFunctionCall2Coll(functionId, InvalidOid, arg1, arg2) 637 #define OidFunctionCall3(functionId, arg1, arg2, arg3) \ 638 OidFunctionCall3Coll(functionId, InvalidOid, arg1, arg2, arg3) 639 #define OidFunctionCall4(functionId, arg1, arg2, arg3, arg4) \ 640 OidFunctionCall4Coll(functionId, InvalidOid, arg1, arg2, arg3, arg4) 641 #define OidFunctionCall5(functionId, arg1, arg2, arg3, arg4, arg5) \ 642 OidFunctionCall5Coll(functionId, InvalidOid, arg1, arg2, arg3, arg4, arg5) 643 #define OidFunctionCall6(functionId, arg1, arg2, arg3, arg4, arg5, arg6) \ 644 OidFunctionCall6Coll(functionId, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6) 645 #define OidFunctionCall7(functionId, arg1, arg2, arg3, arg4, arg5, arg6, arg7) \ 646 OidFunctionCall7Coll(functionId, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6, arg7) 647 #define OidFunctionCall8(functionId, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8) \ 648 OidFunctionCall8Coll(functionId, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8) 649 #define OidFunctionCall9(functionId, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9) \ 650 OidFunctionCall9Coll(functionId, InvalidOid, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9) 651 652 653 /* Special cases for convenient invocation of datatype I/O functions. */ 654 extern Datum InputFunctionCall(FmgrInfo *flinfo, char *str, 655 Oid typioparam, int32 typmod); 656 extern Datum OidInputFunctionCall(Oid functionId, char *str, 657 Oid typioparam, int32 typmod); 658 extern char *OutputFunctionCall(FmgrInfo *flinfo, Datum val); 659 extern char *OidOutputFunctionCall(Oid functionId, Datum val); 660 extern Datum ReceiveFunctionCall(FmgrInfo *flinfo, fmStringInfo buf, 661 Oid typioparam, int32 typmod); 662 extern Datum OidReceiveFunctionCall(Oid functionId, fmStringInfo buf, 663 Oid typioparam, int32 typmod); 664 extern bytea *SendFunctionCall(FmgrInfo *flinfo, Datum val); 665 extern bytea *OidSendFunctionCall(Oid functionId, Datum val); 666 667 668 /* 669 * Routines in fmgr.c 670 */ 671 extern const Pg_finfo_record *fetch_finfo_record(void *filehandle, const char *funcname); 672 extern void clear_external_function_hash(void *filehandle); 673 extern Oid fmgr_internal_function(const char *proname); 674 extern Oid get_fn_expr_rettype(FmgrInfo *flinfo); 675 extern Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum); 676 extern Oid get_call_expr_argtype(fmNodePtr expr, int argnum); 677 extern bool get_fn_expr_arg_stable(FmgrInfo *flinfo, int argnum); 678 extern bool get_call_expr_arg_stable(fmNodePtr expr, int argnum); 679 extern bool get_fn_expr_variadic(FmgrInfo *flinfo); 680 extern bool CheckFunctionValidatorAccess(Oid validatorOid, Oid functionOid); 681 682 /* 683 * Routines in dfmgr.c 684 */ 685 extern char *Dynamic_library_path; 686 687 extern PGFunction load_external_function(const char *filename, const char *funcname, 688 bool signalNotFound, void **filehandle); 689 extern PGFunction lookup_external_function(void *filehandle, const char *funcname); 690 extern void load_file(const char *filename, bool restricted); 691 extern void **find_rendezvous_variable(const char *varName); 692 extern Size EstimateLibraryStateSpace(void); 693 extern void SerializeLibraryState(Size maxsize, char *start_address); 694 extern void RestoreLibraryState(char *start_address); 695 696 /* 697 * Support for aggregate functions 698 * 699 * These are actually in executor/nodeAgg.c, but we declare them here since 700 * the whole point is for callers to not be overly friendly with nodeAgg. 701 */ 702 703 /* AggCheckCallContext can return one of the following codes, or 0: */ 704 #define AGG_CONTEXT_AGGREGATE 1 /* regular aggregate */ 705 #define AGG_CONTEXT_WINDOW 2 /* window function */ 706 707 extern int AggCheckCallContext(FunctionCallInfo fcinfo, 708 MemoryContext *aggcontext); 709 extern fmAggrefPtr AggGetAggref(FunctionCallInfo fcinfo); 710 extern MemoryContext AggGetTempMemoryContext(FunctionCallInfo fcinfo); 711 extern bool AggStateIsShared(FunctionCallInfo fcinfo); 712 extern void AggRegisterCallback(FunctionCallInfo fcinfo, 713 fmExprContextCallbackFunction func, 714 Datum arg); 715 716 /* 717 * We allow plugin modules to hook function entry/exit. This is intended 718 * as support for loadable security policy modules, which may want to 719 * perform additional privilege checks on function entry or exit, or to do 720 * other internal bookkeeping. To make this possible, such modules must be 721 * able not only to support normal function entry and exit, but also to trap 722 * the case where we bail out due to an error; and they must also be able to 723 * prevent inlining. 724 */ 725 typedef enum FmgrHookEventType 726 { 727 FHET_START, 728 FHET_END, 729 FHET_ABORT 730 } FmgrHookEventType; 731 732 typedef bool (*needs_fmgr_hook_type) (Oid fn_oid); 733 734 typedef void (*fmgr_hook_type) (FmgrHookEventType event, 735 FmgrInfo *flinfo, Datum *arg); 736 737 extern PGDLLIMPORT needs_fmgr_hook_type needs_fmgr_hook; 738 extern PGDLLIMPORT fmgr_hook_type fmgr_hook; 739 740 #define FmgrHookIsNeeded(fn_oid) \ 741 (!needs_fmgr_hook ? false : (*needs_fmgr_hook)(fn_oid)) 742 743 #endif /* FMGR_H */ 744