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_namespace.h"
#include "catalog/pg_proc.h"
#include "libpq/pqformat.h"
#include "libpq/protocol.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 120 of file fastpath.c.

121 {
122  HeapTuple func_htp;
123  Form_pg_proc pp;
124 
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  func_htp = SearchSysCache1(PROCOID, ObjectIdGetDatum(func_id));
139  if (!HeapTupleIsValid(func_htp))
140  ereport(ERROR,
141  (errcode(ERRCODE_UNDEFINED_FUNCTION),
142  errmsg("function with OID %u does not exist", func_id)));
143  pp = (Form_pg_proc) GETSTRUCT(func_htp);
144 
145  /* reject pg_proc entries that are unsafe to call via fastpath */
146  if (pp->prokind != PROKIND_FUNCTION || pp->proretset)
147  ereport(ERROR,
148  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
149  errmsg("cannot call function \"%s\" via fastpath interface",
150  NameStr(pp->proname))));
151 
152  /* watch out for catalog entries with more than FUNC_MAX_ARGS args */
153  if (pp->pronargs > FUNC_MAX_ARGS)
154  elog(ERROR, "function %s has more than %d arguments",
155  NameStr(pp->proname), FUNC_MAX_ARGS);
156 
157  fip->namespace = pp->pronamespace;
158  fip->rettype = pp->prorettype;
159  memcpy(fip->argtypes, pp->proargtypes.values, pp->pronargs * sizeof(Oid));
160  strlcpy(fip->fname, NameStr(pp->proname), NAMEDATALEN);
161 
162  ReleaseSysCache(func_htp);
163 
164  fmgr_info(func_id, &fip->flinfo);
165 
166  /*
167  * This must be last!
168  */
169  fip->funcid = func_id;
170 }
#define NameStr(name)
Definition: c.h:746
#define Assert(condition)
Definition: c.h:858
#define MemSet(start, val, len)
Definition: c.h:1020
int errcode(int sqlerrcode)
Definition: elog.c:859
int errmsg(const char *fmt,...)
Definition: elog.c:1072
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:224
#define ereport(elevel,...)
Definition: elog.h:149
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:127
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define GETSTRUCT(TUP)
Definition: htup_details.h:653
#define NAMEDATALEN
#define FUNC_MAX_ARGS
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:136
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
Oid rettype
Definition: fastpath.c:53
Oid funcid
Definition: fastpath.c:51
char fname[NAMEDATALEN]
Definition: fastpath.c:56
Oid argtypes[FUNC_MAX_ARGS]
Definition: fastpath.c:55
FmgrInfo flinfo
Definition: fastpath.c:52
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:266
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:218

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(), ReleaseSysCache(), fp_info::rettype, SearchSysCache1(), and strlcpy().

Referenced by HandleFunctionRequest().

◆ HandleFunctionRequest()

void HandleFunctionRequest ( StringInfo  msgBuf)

Definition at line 189 of file fastpath.c.

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

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_aclcheck(), OBJECT_FUNCTION, OBJECT_SCHEMA, parse_fcall_arguments(), PopActiveSnapshot(), pq_getmsgend(), pq_getmsgint(), PushActiveSnapshot(), fp_info::rettype, and SendFunctionResult().

Referenced by PostgresMain().

◆ parse_fcall_arguments()

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

Definition at line 330 of file fastpath.c.

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

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().

◆ SendFunctionResult()

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

Definition at line 68 of file fastpath.c.

69 {
71 
73 
74  if (isnull)
75  {
76  pq_sendint32(&buf, -1);
77  }
78  else
79  {
80  if (format == 0)
81  {
82  Oid typoutput;
83  bool typisvarlena;
84  char *outputstr;
85 
86  getTypeOutputInfo(rettype, &typoutput, &typisvarlena);
87  outputstr = OidOutputFunctionCall(typoutput, retval);
88  pq_sendcountedtext(&buf, outputstr, strlen(outputstr));
89  pfree(outputstr);
90  }
91  else if (format == 1)
92  {
93  Oid typsend;
94  bool typisvarlena;
95  bytea *outputbytes;
96 
97  getTypeBinaryOutputInfo(rettype, &typsend, &typisvarlena);
98  outputbytes = OidSendFunctionCall(typsend, retval);
99  pq_sendint32(&buf, VARSIZE(outputbytes) - VARHDRSZ);
100  pq_sendbytes(&buf, VARDATA(outputbytes),
101  VARSIZE(outputbytes) - VARHDRSZ);
102  pfree(outputbytes);
103  }
104  else
105  ereport(ERROR,
106  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
107  errmsg("unsupported format code: %d", format)));
108  }
109 
110  pq_endmessage(&buf);
111 }
#define VARHDRSZ
Definition: c.h:692
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:1763
bytea * OidSendFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:1782
void getTypeBinaryOutputInfo(Oid type, Oid *typSend, bool *typIsVarlena)
Definition: lsyscache.c:2973
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2907
static char format
static char * buf
Definition: pg_test_fsync.c:73
void pq_sendbytes(StringInfo buf, const void *data, int datalen)
Definition: pqformat.c:126
void pq_endmessage(StringInfo buf)
Definition: pqformat.c:296
void pq_beginmessage(StringInfo buf, char msgtype)
Definition: pqformat.c:88
void pq_sendcountedtext(StringInfo buf, const char *str, int slen)
Definition: pqformat.c:142
static void pq_sendint32(StringInfo buf, uint32 i)
Definition: pqformat.h:144
#define PqMsg_FunctionCallResponse
Definition: protocol.h:53
Definition: c.h:687
#define VARDATA(PTR)
Definition: varatt.h:278
#define VARSIZE(PTR)
Definition: varatt.h:279

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

Referenced by HandleFunctionRequest().