PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
fastpath.h File Reference
#include "lib/stringinfo.h"
Include dependency graph for fastpath.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

int GetOldFunctionMessage (StringInfo buf)
 
int HandleFunctionRequest (StringInfo msgBuf)
 

Function Documentation

int GetOldFunctionMessage ( StringInfo  buf)

Definition at line 79 of file fastpath.c.

References appendBinaryStringInfo(), StringInfoData::data, enlargeStringInfo(), ereport, errcode(), errmsg(), FATAL, StringInfoData::len, pq_getbytes(), and pq_getstring().

Referenced by SocketBackend().

80 {
81  int32 ibuf;
82  int nargs;
83 
84  /* Dummy string argument */
85  if (pq_getstring(buf))
86  return EOF;
87  /* Function OID */
88  if (pq_getbytes((char *) &ibuf, 4))
89  return EOF;
90  appendBinaryStringInfo(buf, (char *) &ibuf, 4);
91  /* Number of arguments */
92  if (pq_getbytes((char *) &ibuf, 4))
93  return EOF;
94  appendBinaryStringInfo(buf, (char *) &ibuf, 4);
95  nargs = ntohl(ibuf);
96  /* For each argument ... */
97  while (nargs-- > 0)
98  {
99  int argsize;
100 
101  /* argsize */
102  if (pq_getbytes((char *) &ibuf, 4))
103  return EOF;
104  appendBinaryStringInfo(buf, (char *) &ibuf, 4);
105  argsize = ntohl(ibuf);
106  if (argsize < -1)
107  {
108  /* FATAL here since no hope of regaining message sync */
109  ereport(FATAL,
110  (errcode(ERRCODE_PROTOCOL_VIOLATION),
111  errmsg("invalid argument size %d in function call message",
112  argsize)));
113  }
114  /* and arg contents */
115  if (argsize > 0)
116  {
117  /* Allocate space for arg */
118  enlargeStringInfo(buf, argsize);
119  /* And grab it */
120  if (pq_getbytes(buf->data + buf->len, argsize))
121  return EOF;
122  buf->len += argsize;
123  /* Place a trailing null per StringInfo convention */
124  buf->data[buf->len] = '\0';
125  }
126  }
127  return 0;
128 }
int errcode(int sqlerrcode)
Definition: elog.c:575
signed int int32
Definition: c.h:256
#define FATAL
Definition: elog.h:52
int pq_getbytes(char *s, size_t len)
Definition: pqcomm.c:1075
void enlargeStringInfo(StringInfo str, int needed)
Definition: stringinfo.c:277
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797
int pq_getstring(StringInfo s)
Definition: pqcomm.c:1147
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:240
int HandleFunctionRequest ( StringInfo  msgBuf)

Definition at line 270 of file fastpath.c.

References ACL_EXECUTE, ACL_KIND_NAMESPACE, ACL_KIND_PROC, ACL_USAGE, aclcheck_error(), ACLCHECK_OK, FunctionCallInfoData::argnull, CHECK_FOR_INTERRUPTS, check_log_duration(), ereport, errcode(), errmsg(), ERROR, fetch_fp_info(), fp_info::flinfo, FmgrInfo::fn_strict, fp_info::fname, FrontendProtocol, FunctionCallInvoke, get_func_name(), get_namespace_name(), GetTransactionSnapshot(), GetUserId(), i, InitFunctionCallInfoData, InvalidOid, InvokeFunctionExecuteHook, InvokeNamespaceSearchHook, IsAbortedTransactionBlockState(), FunctionCallInfoData::isnull, LOG, log_statement, LOGSTMT_ALL, FunctionCallInfoData::nargs, NULL, parse_fcall_arguments(), parse_fcall_arguments_20(), pg_namespace_aclcheck(), pg_proc_aclcheck(), PG_PROTOCOL_MAJOR, PopActiveSnapshot(), pq_getmsgend(), pq_getmsgint(), pq_getmsgstring(), PushActiveSnapshot(), fp_info::rettype, and SendFunctionResult().

Referenced by PostgresMain().

271 {
272  Oid fid;
273  AclResult aclresult;
274  FunctionCallInfoData fcinfo;
275  int16 rformat;
276  Datum retval;
277  struct fp_info my_fp;
278  struct fp_info *fip;
279  bool callit;
280  bool was_logged = false;
281  char msec_str[32];
282 
283  /*
284  * Now that we've eaten the input message, check to see if we actually
285  * want to do the function call or not. It's now safe to ereport(); we
286  * won't lose sync with the frontend.
287  */
289  ereport(ERROR,
290  (errcode(ERRCODE_IN_FAILED_SQL_TRANSACTION),
291  errmsg("current transaction is aborted, "
292  "commands ignored until end of transaction block")));
293 
294  /*
295  * Now that we know we are in a valid transaction, set snapshot in case
296  * needed by function itself or one of the datatype I/O routines.
297  */
299 
300  /*
301  * Begin parsing the buffer contents.
302  */
304  (void) pq_getmsgstring(msgBuf); /* dummy string */
305 
306  fid = (Oid) pq_getmsgint(msgBuf, 4); /* function oid */
307 
308  /*
309  * There used to be a lame attempt at caching lookup info here. Now we
310  * just do the lookups on every call.
311  */
312  fip = &my_fp;
313  fetch_fp_info(fid, fip);
314 
315  /* Log as soon as we have the function OID and name */
316  if (log_statement == LOGSTMT_ALL)
317  {
318  ereport(LOG,
319  (errmsg("fastpath function call: \"%s\" (OID %u)",
320  fip->fname, fid)));
321  was_logged = true;
322  }
323 
324  /*
325  * Check permission to access and call function. Since we didn't go
326  * through a normal name lookup, we need to check schema usage too.
327  */
328  aclresult = pg_namespace_aclcheck(fip->namespace, GetUserId(), ACL_USAGE);
329  if (aclresult != ACLCHECK_OK)
331  get_namespace_name(fip->namespace));
332  InvokeNamespaceSearchHook(fip->namespace, true);
333 
334  aclresult = pg_proc_aclcheck(fid, GetUserId(), ACL_EXECUTE);
335  if (aclresult != ACLCHECK_OK)
336  aclcheck_error(aclresult, ACL_KIND_PROC,
337  get_func_name(fid));
339 
340  /*
341  * Prepare function call info block and insert arguments.
342  *
343  * Note: for now we pass collation = InvalidOid, so collation-sensitive
344  * functions can't be called this way. Perhaps we should pass
345  * DEFAULT_COLLATION_OID, instead?
346  */
347  InitFunctionCallInfoData(fcinfo, &fip->flinfo, 0, InvalidOid, NULL, NULL);
348 
350  rformat = parse_fcall_arguments(msgBuf, fip, &fcinfo);
351  else
352  rformat = parse_fcall_arguments_20(msgBuf, fip, &fcinfo);
353 
354  /* Verify we reached the end of the message where expected. */
355  pq_getmsgend(msgBuf);
356 
357  /*
358  * If func is strict, must not call it for null args.
359  */
360  callit = true;
361  if (fip->flinfo.fn_strict)
362  {
363  int i;
364 
365  for (i = 0; i < fcinfo.nargs; i++)
366  {
367  if (fcinfo.argnull[i])
368  {
369  callit = false;
370  break;
371  }
372  }
373  }
374 
375  if (callit)
376  {
377  /* Okay, do it ... */
378  retval = FunctionCallInvoke(&fcinfo);
379  }
380  else
381  {
382  fcinfo.isnull = true;
383  retval = (Datum) 0;
384  }
385 
386  /* ensure we do at least one CHECK_FOR_INTERRUPTS per function call */
388 
389  SendFunctionResult(retval, fcinfo.isnull, fip->rettype, rformat);
390 
391  /* We no longer need the snapshot */
393 
394  /*
395  * Emit duration logging if appropriate.
396  */
397  switch (check_log_duration(msec_str, was_logged))
398  {
399  case 1:
400  ereport(LOG,
401  (errmsg("duration: %s ms", msec_str)));
402  break;
403  case 2:
404  ereport(LOG,
405  (errmsg("duration: %s ms fastpath function call: \"%s\" (OID %u)",
406  msec_str, fip->fname, fid)));
407  break;
408  }
409 
410  return 0;
411 }
signed short int16
Definition: c.h:255
FmgrInfo flinfo
Definition: fastpath.c:53
char fname[NAMEDATALEN]
Definition: fastpath.c:57
Oid rettype
Definition: fastpath.c:54
Oid GetUserId(void)
Definition: miscinit.c:283
const char * pq_getmsgstring(StringInfo msg)
Definition: pqformat.c:621
bool IsAbortedTransactionBlockState(void)
Definition: xact.c:370
int errcode(int sqlerrcode)
Definition: elog.c:575
static void SendFunctionResult(Datum retval, bool isnull, Oid rettype, int16 format)
Definition: fastpath.c:138
void PopActiveSnapshot(void)
Definition: snapmgr.c:807
#define LOG
Definition: elog.h:26
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_PROTOCOL_MAJOR(v)
Definition: pqcomm.h:104
Snapshot GetTransactionSnapshot(void)
Definition: snapmgr.c:300
AclResult pg_namespace_aclcheck(Oid nsp_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4458
static int16 parse_fcall_arguments_20(StringInfo msgBuf, struct fp_info *fip, FunctionCallInfo fcinfo)
Definition: fastpath.c:558
#define InvokeNamespaceSearchHook(objectId, ereport_on_violation)
Definition: objectaccess.h:174
int check_log_duration(char *msec_str, bool was_logged)
Definition: postgres.c:2102
#define ERROR
Definition: elog.h:43
bool fn_strict
Definition: fmgr.h:58
char * get_func_name(Oid funcid)
Definition: lsyscache.c:1380
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3006
#define FunctionCallInvoke(fcinfo)
Definition: fmgr.h:129
void PushActiveSnapshot(Snapshot snap)
Definition: snapmgr.c:728
void aclcheck_error(AclResult aclerr, AclObjectKind objectkind, const char *objectname)
Definition: aclchk.c:3377
#define ACL_USAGE
Definition: parsenodes.h:73
bool argnull[FUNC_MAX_ARGS]
Definition: fmgr.h:78
#define ereport(elevel, rest)
Definition: elog.h:122
#define InvokeFunctionExecuteHook(objectId)
Definition: objectaccess.h:179
AclResult
Definition: acl.h:170
uintptr_t Datum
Definition: postgres.h:372
static int16 parse_fcall_arguments(StringInfo msgBuf, struct fp_info *fip, FunctionCallInfo fcinfo)
Definition: fastpath.c:420
#define InvalidOid
Definition: postgres_ext.h:36
#define NULL
Definition: c.h:229
#define InitFunctionCallInfoData(Fcinfo, Flinfo, Nargs, Collation, Context, Resultinfo)
Definition: fmgr.h:112
int log_statement
Definition: postgres.c:91
static void fetch_fp_info(Oid func_id, struct fp_info *fip)
Definition: fastpath.c:198
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define ACL_EXECUTE
Definition: parsenodes.h:72
AclResult pg_proc_aclcheck(Oid proc_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4420
int i
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:448
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:97
void pq_getmsgend(StringInfo msg)
Definition: pqformat.c:677
ProtocolVersion FrontendProtocol
Definition: globals.c:27