PostgreSQL Source Code  git master
fastpath.c File Reference
#include "postgres.h"
#include "access/htup_details.h"
#include "access/xact.h"
#include "catalog/objectaccess.h"
#include "catalog/pg_proc.h"
#include "libpq/libpq.h"
#include "libpq/pqformat.h"
#include "mb/pg_wchar.h"
#include "miscadmin.h"
#include "port/pg_bswap.h"
#include "tcop/fastpath.h"
#include "tcop/tcopprot.h"
#include "utils/acl.h"
#include "utils/lsyscache.h"
#include "utils/snapmgr.h"
#include "utils/syscache.h"
Include dependency graph for fastpath.c:

Go to the source code of this file.

Data Structures

struct  fp_info
 

Functions

static int16 parse_fcall_arguments (StringInfo msgBuf, struct fp_info *fip, FunctionCallInfo fcinfo)
 
static void SendFunctionResult (Datum retval, bool isnull, Oid rettype, int16 format)
 
static void fetch_fp_info (Oid func_id, struct fp_info *fip)
 
void HandleFunctionRequest (StringInfo msgBuf)
 

Function Documentation

◆ fetch_fp_info()

static void fetch_fp_info ( Oid  func_id,
struct fp_info fip 
)
static

Definition at line 119 of file fastpath.c.

References fp_info::argtypes, Assert, elog, ereport, errcode(), errmsg(), ERROR, fp_info::flinfo, fmgr_info(), fp_info::fname, FUNC_MAX_ARGS, fp_info::funcid, GETSTRUCT, HeapTupleIsValid, InvalidOid, MemSet, NAMEDATALEN, NameStr, ObjectIdGetDatum, OidIsValid, PROCOID, ReleaseSysCache(), fp_info::rettype, SearchSysCache1(), and strlcpy().

Referenced by HandleFunctionRequest().

120 {
121  HeapTuple func_htp;
122  Form_pg_proc pp;
123 
124  Assert(OidIsValid(func_id));
125  Assert(fip != NULL);
126 
127  /*
128  * Since the validity of this structure is determined by whether the
129  * funcid is OK, we clear the funcid here. It must not be set to the
130  * correct value until we are about to return with a good struct fp_info,
131  * since we can be interrupted (i.e., with an ereport(ERROR, ...)) at any
132  * time. [No longer really an issue since we don't save the struct
133  * fp_info across transactions anymore, but keep it anyway.]
134  */
135  MemSet(fip, 0, sizeof(struct fp_info));
136  fip->funcid = InvalidOid;
137 
138  fmgr_info(func_id, &fip->flinfo);
139 
140  func_htp = SearchSysCache1(PROCOID, ObjectIdGetDatum(func_id));
141  if (!HeapTupleIsValid(func_htp))
142  ereport(ERROR,
143  (errcode(ERRCODE_UNDEFINED_FUNCTION),
144  errmsg("function with OID %u does not exist", func_id)));
145  pp = (Form_pg_proc) GETSTRUCT(func_htp);
146 
147  /* watch out for catalog entries with more than FUNC_MAX_ARGS args */
148  if (pp->pronargs > FUNC_MAX_ARGS)
149  elog(ERROR, "function %s has more than %d arguments",
150  NameStr(pp->proname), FUNC_MAX_ARGS);
151 
152  fip->namespace = pp->pronamespace;
153  fip->rettype = pp->prorettype;
154  memcpy(fip->argtypes, pp->proargtypes.values, pp->pronargs * sizeof(Oid));
155  strlcpy(fip->fname, NameStr(pp->proname), NAMEDATALEN);
156 
157  ReleaseSysCache(func_htp);
158 
159  /*
160  * This must be last!
161  */
162  fip->funcid = func_id;
163 }
FmgrInfo flinfo
Definition: fastpath.c:51
char fname[NAMEDATALEN]
Definition: fastpath.c:55
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
Oid rettype
Definition: fastpath.c:52
int errcode(int sqlerrcode)
Definition: elog.c:698
#define MemSet(start, val, len)
Definition: c.h:1008
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:710
#define FUNC_MAX_ARGS
#define NAMEDATALEN
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:126
Oid argtypes[FUNC_MAX_ARGS]
Definition: fastpath.c:54
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:136
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:157
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:804
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
#define NameStr(name)
Definition: c.h:681
Oid funcid
Definition: fastpath.c:50

◆ HandleFunctionRequest()

void HandleFunctionRequest ( StringInfo  msgBuf)

Definition at line 182 of file fastpath.c.

References ACL_EXECUTE, ACL_USAGE, aclcheck_error(), ACLCHECK_OK, CHECK_FOR_INTERRUPTS, check_log_duration(), ereport, errcode(), errmsg(), ERROR, fetch_fp_info(), fp_info::flinfo, FmgrInfo::fn_strict, fp_info::fname, FUNC_MAX_ARGS, FunctionCallInvoke, get_func_name(), get_namespace_name(), GetTransactionSnapshot(), GetUserId(), i, InitFunctionCallInfoData, InvalidOid, InvokeFunctionExecuteHook, InvokeNamespaceSearchHook, IsAbortedTransactionBlockState(), LOCAL_FCINFO, LOG, log_statement, LOGSTMT_ALL, OBJECT_FUNCTION, OBJECT_SCHEMA, parse_fcall_arguments(), pg_namespace_aclcheck(), pg_proc_aclcheck(), PopActiveSnapshot(), pq_getmsgend(), pq_getmsgint(), PushActiveSnapshot(), fp_info::rettype, and SendFunctionResult().

Referenced by PostgresMain().

183 {
184  LOCAL_FCINFO(fcinfo, FUNC_MAX_ARGS);
185  Oid fid;
186  AclResult aclresult;
187  int16 rformat;
188  Datum retval;
189  struct fp_info my_fp;
190  struct fp_info *fip;
191  bool callit;
192  bool was_logged = false;
193  char msec_str[32];
194 
195  /*
196  * We only accept COMMIT/ABORT if we are in an aborted transaction, and
197  * COMMIT/ABORT cannot be executed through the fastpath interface.
198  */
200  ereport(ERROR,
201  (errcode(ERRCODE_IN_FAILED_SQL_TRANSACTION),
202  errmsg("current transaction is aborted, "
203  "commands ignored until end of transaction block")));
204 
205  /*
206  * Now that we know we are in a valid transaction, set snapshot in case
207  * needed by function itself or one of the datatype I/O routines.
208  */
210 
211  /*
212  * Begin parsing the buffer contents.
213  */
214  fid = (Oid) pq_getmsgint(msgBuf, 4); /* function oid */
215 
216  /*
217  * There used to be a lame attempt at caching lookup info here. Now we
218  * just do the lookups on every call.
219  */
220  fip = &my_fp;
221  fetch_fp_info(fid, fip);
222 
223  /* Log as soon as we have the function OID and name */
224  if (log_statement == LOGSTMT_ALL)
225  {
226  ereport(LOG,
227  (errmsg("fastpath function call: \"%s\" (OID %u)",
228  fip->fname, fid)));
229  was_logged = true;
230  }
231 
232  /*
233  * Check permission to access and call function. Since we didn't go
234  * through a normal name lookup, we need to check schema usage too.
235  */
236  aclresult = pg_namespace_aclcheck(fip->namespace, GetUserId(), ACL_USAGE);
237  if (aclresult != ACLCHECK_OK)
238  aclcheck_error(aclresult, OBJECT_SCHEMA,
239  get_namespace_name(fip->namespace));
240  InvokeNamespaceSearchHook(fip->namespace, true);
241 
242  aclresult = pg_proc_aclcheck(fid, GetUserId(), ACL_EXECUTE);
243  if (aclresult != ACLCHECK_OK)
244  aclcheck_error(aclresult, OBJECT_FUNCTION,
245  get_func_name(fid));
247 
248  /*
249  * Prepare function call info block and insert arguments.
250  *
251  * Note: for now we pass collation = InvalidOid, so collation-sensitive
252  * functions can't be called this way. Perhaps we should pass
253  * DEFAULT_COLLATION_OID, instead?
254  */
255  InitFunctionCallInfoData(*fcinfo, &fip->flinfo, 0, InvalidOid, NULL, NULL);
256 
257  rformat = parse_fcall_arguments(msgBuf, fip, fcinfo);
258 
259  /* Verify we reached the end of the message where expected. */
260  pq_getmsgend(msgBuf);
261 
262  /*
263  * If func is strict, must not call it for null args.
264  */
265  callit = true;
266  if (fip->flinfo.fn_strict)
267  {
268  int i;
269 
270  for (i = 0; i < fcinfo->nargs; i++)
271  {
272  if (fcinfo->args[i].isnull)
273  {
274  callit = false;
275  break;
276  }
277  }
278  }
279 
280  if (callit)
281  {
282  /* Okay, do it ... */
283  retval = FunctionCallInvoke(fcinfo);
284  }
285  else
286  {
287  fcinfo->isnull = true;
288  retval = (Datum) 0;
289  }
290 
291  /* ensure we do at least one CHECK_FOR_INTERRUPTS per function call */
293 
294  SendFunctionResult(retval, fcinfo->isnull, fip->rettype, rformat);
295 
296  /* We no longer need the snapshot */
298 
299  /*
300  * Emit duration logging if appropriate.
301  */
302  switch (check_log_duration(msec_str, was_logged))
303  {
304  case 1:
305  ereport(LOG,
306  (errmsg("duration: %s ms", msec_str)));
307  break;
308  case 2:
309  ereport(LOG,
310  (errmsg("duration: %s ms fastpath function call: \"%s\" (OID %u)",
311  msec_str, fip->fname, fid)));
312  break;
313  }
314 }
signed short int16
Definition: c.h:428
FmgrInfo flinfo
Definition: fastpath.c:51
char fname[NAMEDATALEN]
Definition: fastpath.c:55
Oid rettype
Definition: fastpath.c:52
Oid GetUserId(void)
Definition: miscinit.c:478
bool IsAbortedTransactionBlockState(void)
Definition: xact.c:391
int errcode(int sqlerrcode)
Definition: elog.c:698
static void SendFunctionResult(Datum retval, bool isnull, Oid rettype, int16 format)
Definition: fastpath.c:67
void PopActiveSnapshot(void)
Definition: snapmgr.c:759
#define LOG
Definition: elog.h:26
unsigned int Oid
Definition: postgres_ext.h:31
Snapshot GetTransactionSnapshot(void)
Definition: snapmgr.c:250
AclResult pg_namespace_aclcheck(Oid nsp_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4761
#define FUNC_MAX_ARGS
#define InvokeNamespaceSearchHook(objectId, ereport_on_violation)
Definition: objectaccess.h:186
int check_log_duration(char *msec_str, bool was_logged)
Definition: postgres.c:2306
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3313
#define ERROR
Definition: elog.h:46
bool fn_strict
Definition: fmgr.h:61
char * get_func_name(Oid funcid)
Definition: lsyscache.c:1579
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3316
#define FunctionCallInvoke(fcinfo)
Definition: fmgr.h:172
void PushActiveSnapshot(Snapshot snap)
Definition: snapmgr.c:680
#define ACL_USAGE
Definition: parsenodes.h:90
#define InvokeFunctionExecuteHook(objectId)
Definition: objectaccess.h:191
AclResult
Definition: acl.h:177
uintptr_t Datum
Definition: postgres.h:411
static int16 parse_fcall_arguments(StringInfo msgBuf, struct fp_info *fip, FunctionCallInfo fcinfo)
Definition: fastpath.c:323
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:157
#define LOCAL_FCINFO(name, nargs)
Definition: fmgr.h:110
#define InitFunctionCallInfoData(Fcinfo, Flinfo, Nargs, Collation, Context, Resultinfo)
Definition: fmgr.h:150
int log_statement
Definition: postgres.c:97
static void fetch_fp_info(Oid func_id, struct fp_info *fip)
Definition: fastpath.c:119
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define ACL_EXECUTE
Definition: parsenodes.h:89
AclResult pg_proc_aclcheck(Oid proc_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4723
int i
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:417
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:102
void pq_getmsgend(StringInfo msg)
Definition: pqformat.c:637

◆ parse_fcall_arguments()

static int16 parse_fcall_arguments ( StringInfo  msgBuf,
struct fp_info fip,
FunctionCallInfo  fcinfo 
)
static

Definition at line 323 of file fastpath.c.

References appendBinaryStringInfo(), FunctionCallInfoBaseData::args, fp_info::argtypes, StringInfoData::cursor, StringInfoData::data, ereport, errcode(), errmsg(), ERROR, fp_info::flinfo, FmgrInfo::fn_nargs, FUNC_MAX_ARGS, getTypeBinaryInputInfo(), getTypeInputInfo(), i, initStringInfo(), NullableDatum::isnull, StringInfoData::len, FunctionCallInfoBaseData::nargs, OidInputFunctionCall(), OidReceiveFunctionCall(), palloc(), pfree(), pg_client_to_server(), pq_getmsgbytes(), pq_getmsgint(), resetStringInfo(), and NullableDatum::value.

Referenced by HandleFunctionRequest().

325 {
326  int nargs;
327  int i;
328  int numAFormats;
329  int16 *aformats = NULL;
330  StringInfoData abuf;
331 
332  /* Get the argument format codes */
333  numAFormats = pq_getmsgint(msgBuf, 2);
334  if (numAFormats > 0)
335  {
336  aformats = (int16 *) palloc(numAFormats * sizeof(int16));
337  for (i = 0; i < numAFormats; i++)
338  aformats[i] = pq_getmsgint(msgBuf, 2);
339  }
340 
341  nargs = pq_getmsgint(msgBuf, 2); /* # of arguments */
342 
343  if (fip->flinfo.fn_nargs != nargs || nargs > FUNC_MAX_ARGS)
344  ereport(ERROR,
345  (errcode(ERRCODE_PROTOCOL_VIOLATION),
346  errmsg("function call message contains %d arguments but function requires %d",
347  nargs, fip->flinfo.fn_nargs)));
348 
349  fcinfo->nargs = nargs;
350 
351  if (numAFormats > 1 && numAFormats != nargs)
352  ereport(ERROR,
353  (errcode(ERRCODE_PROTOCOL_VIOLATION),
354  errmsg("function call message contains %d argument formats but %d arguments",
355  numAFormats, nargs)));
356 
357  initStringInfo(&abuf);
358 
359  /*
360  * Copy supplied arguments into arg vector.
361  */
362  for (i = 0; i < nargs; ++i)
363  {
364  int argsize;
365  int16 aformat;
366 
367  argsize = pq_getmsgint(msgBuf, 4);
368  if (argsize == -1)
369  {
370  fcinfo->args[i].isnull = true;
371  }
372  else
373  {
374  fcinfo->args[i].isnull = false;
375  if (argsize < 0)
376  ereport(ERROR,
377  (errcode(ERRCODE_PROTOCOL_VIOLATION),
378  errmsg("invalid argument size %d in function call message",
379  argsize)));
380 
381  /* Reset abuf to empty, and insert raw data into it */
382  resetStringInfo(&abuf);
384  pq_getmsgbytes(msgBuf, argsize),
385  argsize);
386  }
387 
388  if (numAFormats > 1)
389  aformat = aformats[i];
390  else if (numAFormats > 0)
391  aformat = aformats[0];
392  else
393  aformat = 0; /* default = text */
394 
395  if (aformat == 0)
396  {
397  Oid typinput;
398  Oid typioparam;
399  char *pstring;
400 
401  getTypeInputInfo(fip->argtypes[i], &typinput, &typioparam);
402 
403  /*
404  * Since stringinfo.c keeps a trailing null in place even for
405  * binary data, the contents of abuf are a valid C string. We
406  * have to do encoding conversion before calling the typinput
407  * routine, though.
408  */
409  if (argsize == -1)
410  pstring = NULL;
411  else
412  pstring = pg_client_to_server(abuf.data, argsize);
413 
414  fcinfo->args[i].value = OidInputFunctionCall(typinput, pstring,
415  typioparam, -1);
416  /* Free result of encoding conversion, if any */
417  if (pstring && pstring != abuf.data)
418  pfree(pstring);
419  }
420  else if (aformat == 1)
421  {
422  Oid typreceive;
423  Oid typioparam;
424  StringInfo bufptr;
425 
426  /* Call the argument type's binary input converter */
427  getTypeBinaryInputInfo(fip->argtypes[i], &typreceive, &typioparam);
428 
429  if (argsize == -1)
430  bufptr = NULL;
431  else
432  bufptr = &abuf;
433 
434  fcinfo->args[i].value = OidReceiveFunctionCall(typreceive, bufptr,
435  typioparam, -1);
436 
437  /* Trouble if it didn't eat the whole buffer */
438  if (argsize != -1 && abuf.cursor != abuf.len)
439  ereport(ERROR,
440  (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
441  errmsg("incorrect binary data format in function argument %d",
442  i + 1)));
443  }
444  else
445  ereport(ERROR,
446  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
447  errmsg("unsupported format code: %d", aformat)));
448  }
449 
450  /* Return result format code */
451  return (int16) pq_getmsgint(msgBuf, 2);
452 }
signed short int16
Definition: c.h:428
FmgrInfo flinfo
Definition: fastpath.c:51
short fn_nargs
Definition: fmgr.h:60
int errcode(int sqlerrcode)
Definition: elog.c:698
unsigned int Oid
Definition: postgres_ext.h:31
const char * pq_getmsgbytes(StringInfo msg, int datalen)
Definition: pqformat.c:510
#define FUNC_MAX_ARGS
void pfree(void *pointer)
Definition: mcxt.c:1169
char * pg_client_to_server(const char *s, int len)
Definition: mbutils.c:660
#define ERROR
Definition: elog.h:46
NullableDatum args[FLEXIBLE_ARRAY_MEMBER]
Definition: fmgr.h:95
Datum OidReceiveFunctionCall(Oid functionId, StringInfo buf, Oid typioparam, int32 typmod)
Definition: fmgr.c:1665
void resetStringInfo(StringInfo str)
Definition: stringinfo.c:75
void getTypeBinaryInputInfo(Oid type, Oid *typReceive, Oid *typIOParam)
Definition: lsyscache.c:2887
void getTypeInputInfo(Oid type, Oid *typInput, Oid *typIOParam)
Definition: lsyscache.c:2821
Datum value
Definition: postgres.h:422
Oid argtypes[FUNC_MAX_ARGS]
Definition: fastpath.c:54
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
#define ereport(elevel,...)
Definition: elog.h:157
void * palloc(Size size)
Definition: mcxt.c:1062
int errmsg(const char *fmt,...)
Definition: elog.c:909
int i
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:417
Datum OidInputFunctionCall(Oid functionId, char *str, Oid typioparam, int32 typmod)
Definition: fmgr.c:1647
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:227

◆ SendFunctionResult()

static void SendFunctionResult ( Datum  retval,
bool  isnull,
Oid  rettype,
int16  format 
)
static

Definition at line 67 of file fastpath.c.

References buf, ereport, errcode(), errmsg(), ERROR, getTypeBinaryOutputInfo(), getTypeOutputInfo(), OidOutputFunctionCall(), OidSendFunctionCall(), pfree(), pq_beginmessage(), pq_endmessage(), pq_sendbytes(), pq_sendcountedtext(), pq_sendint32(), VARDATA, VARHDRSZ, and VARSIZE.

Referenced by HandleFunctionRequest().

68 {
70 
71  pq_beginmessage(&buf, 'V');
72 
73  if (isnull)
74  {
75  pq_sendint32(&buf, -1);
76  }
77  else
78  {
79  if (format == 0)
80  {
81  Oid typoutput;
82  bool typisvarlena;
83  char *outputstr;
84 
85  getTypeOutputInfo(rettype, &typoutput, &typisvarlena);
86  outputstr = OidOutputFunctionCall(typoutput, retval);
87  pq_sendcountedtext(&buf, outputstr, strlen(outputstr), false);
88  pfree(outputstr);
89  }
90  else if (format == 1)
91  {
92  Oid typsend;
93  bool typisvarlena;
94  bytea *outputbytes;
95 
96  getTypeBinaryOutputInfo(rettype, &typsend, &typisvarlena);
97  outputbytes = OidSendFunctionCall(typsend, retval);
98  pq_sendint32(&buf, VARSIZE(outputbytes) - VARHDRSZ);
99  pq_sendbytes(&buf, VARDATA(outputbytes),
100  VARSIZE(outputbytes) - VARHDRSZ);
101  pfree(outputbytes);
102  }
103  else
104  ereport(ERROR,
105  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
106  errmsg("unsupported format code: %d", format)));
107  }
108 
109  pq_endmessage(&buf);
110 }
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2854
#define VARDATA(PTR)
Definition: postgres.h:315
Oid rettype
Definition: fastpath.c:52
#define VARSIZE(PTR)
Definition: postgres.h:316
#define VARHDRSZ
Definition: c.h:627
int errcode(int sqlerrcode)
Definition: elog.c:698
unsigned int Oid
Definition: postgres_ext.h:31
void pq_beginmessage(StringInfo buf, char msgtype)
Definition: pqformat.c:87
static void pq_sendint32(StringInfo buf, uint32 i)
Definition: pqformat.h:145
void pfree(void *pointer)
Definition: mcxt.c:1169
#define ERROR
Definition: elog.h:46
bytea * OidSendFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:1675
static char * buf
Definition: pg_test_fsync.c:68
void pq_sendcountedtext(StringInfo buf, const char *str, int slen, bool countincludesself)
Definition: pqformat.c:142
void getTypeBinaryOutputInfo(Oid type, Oid *typSend, bool *typIsVarlena)
Definition: lsyscache.c:2920
#define ereport(elevel,...)
Definition: elog.h:157
void pq_sendbytes(StringInfo buf, const char *data, int datalen)
Definition: pqformat.c:125
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:1656
int errmsg(const char *fmt,...)
Definition: elog.c:909
void pq_endmessage(StringInfo buf)
Definition: pqformat.c:298
Definition: c.h:621
static char format