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)
 
void 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:1094
void enlargeStringInfo(StringInfo str, int needed)
Definition: stringinfo.c:245
#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:1166
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:208
void HandleFunctionRequest ( StringInfo  msgBuf)

Definition at line 261 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().

262 {
263  Oid fid;
264  AclResult aclresult;
265  FunctionCallInfoData fcinfo;
266  int16 rformat;
267  Datum retval;
268  struct fp_info my_fp;
269  struct fp_info *fip;
270  bool callit;
271  bool was_logged = false;
272  char msec_str[32];
273 
274  /*
275  * We only accept COMMIT/ABORT if we are in an aborted transaction, and
276  * COMMIT/ABORT cannot be executed through the fastpath interface.
277  */
279  ereport(ERROR,
280  (errcode(ERRCODE_IN_FAILED_SQL_TRANSACTION),
281  errmsg("current transaction is aborted, "
282  "commands ignored until end of transaction block")));
283 
284  /*
285  * Now that we know we are in a valid transaction, set snapshot in case
286  * needed by function itself or one of the datatype I/O routines.
287  */
289 
290  /*
291  * Begin parsing the buffer contents.
292  */
294  (void) pq_getmsgstring(msgBuf); /* dummy string */
295 
296  fid = (Oid) pq_getmsgint(msgBuf, 4); /* function oid */
297 
298  /*
299  * There used to be a lame attempt at caching lookup info here. Now we
300  * just do the lookups on every call.
301  */
302  fip = &my_fp;
303  fetch_fp_info(fid, fip);
304 
305  /* Log as soon as we have the function OID and name */
306  if (log_statement == LOGSTMT_ALL)
307  {
308  ereport(LOG,
309  (errmsg("fastpath function call: \"%s\" (OID %u)",
310  fip->fname, fid)));
311  was_logged = true;
312  }
313 
314  /*
315  * Check permission to access and call function. Since we didn't go
316  * through a normal name lookup, we need to check schema usage too.
317  */
318  aclresult = pg_namespace_aclcheck(fip->namespace, GetUserId(), ACL_USAGE);
319  if (aclresult != ACLCHECK_OK)
321  get_namespace_name(fip->namespace));
322  InvokeNamespaceSearchHook(fip->namespace, true);
323 
324  aclresult = pg_proc_aclcheck(fid, GetUserId(), ACL_EXECUTE);
325  if (aclresult != ACLCHECK_OK)
326  aclcheck_error(aclresult, ACL_KIND_PROC,
327  get_func_name(fid));
329 
330  /*
331  * Prepare function call info block and insert arguments.
332  *
333  * Note: for now we pass collation = InvalidOid, so collation-sensitive
334  * functions can't be called this way. Perhaps we should pass
335  * DEFAULT_COLLATION_OID, instead?
336  */
337  InitFunctionCallInfoData(fcinfo, &fip->flinfo, 0, InvalidOid, NULL, NULL);
338 
340  rformat = parse_fcall_arguments(msgBuf, fip, &fcinfo);
341  else
342  rformat = parse_fcall_arguments_20(msgBuf, fip, &fcinfo);
343 
344  /* Verify we reached the end of the message where expected. */
345  pq_getmsgend(msgBuf);
346 
347  /*
348  * If func is strict, must not call it for null args.
349  */
350  callit = true;
351  if (fip->flinfo.fn_strict)
352  {
353  int i;
354 
355  for (i = 0; i < fcinfo.nargs; i++)
356  {
357  if (fcinfo.argnull[i])
358  {
359  callit = false;
360  break;
361  }
362  }
363  }
364 
365  if (callit)
366  {
367  /* Okay, do it ... */
368  retval = FunctionCallInvoke(&fcinfo);
369  }
370  else
371  {
372  fcinfo.isnull = true;
373  retval = (Datum) 0;
374  }
375 
376  /* ensure we do at least one CHECK_FOR_INTERRUPTS per function call */
378 
379  SendFunctionResult(retval, fcinfo.isnull, fip->rettype, rformat);
380 
381  /* We no longer need the snapshot */
383 
384  /*
385  * Emit duration logging if appropriate.
386  */
387  switch (check_log_duration(msec_str, was_logged))
388  {
389  case 1:
390  ereport(LOG,
391  (errmsg("duration: %s ms", msec_str)));
392  break;
393  case 2:
394  ereport(LOG,
395  (errmsg("duration: %s ms fastpath function call: \"%s\" (OID %u)",
396  msec_str, fip->fname, fid)));
397  break;
398  }
399 }
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:284
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:812
#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:304
AclResult pg_namespace_aclcheck(Oid nsp_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4484
static int16 parse_fcall_arguments_20(StringInfo msgBuf, struct fp_info *fip, FunctionCallInfo fcinfo)
Definition: fastpath.c:546
#define InvokeNamespaceSearchHook(objectId, ereport_on_violation)
Definition: objectaccess.h:174
int check_log_duration(char *msec_str, bool was_logged)
Definition: postgres.c:2100
#define ERROR
Definition: elog.h:43
bool fn_strict
Definition: fmgr.h:61
char * get_func_name(Oid funcid)
Definition: lsyscache.c:1412
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3033
#define FunctionCallInvoke(fcinfo)
Definition: fmgr.h:137
void PushActiveSnapshot(Snapshot snap)
Definition: snapmgr.c:733
void aclcheck_error(AclResult aclerr, AclObjectKind objectkind, const char *objectname)
Definition: aclchk.c:3399
#define ACL_USAGE
Definition: parsenodes.h:80
bool argnull[FUNC_MAX_ARGS]
Definition: fmgr.h:86
#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:408
#define InvalidOid
Definition: postgres_ext.h:36
#define NULL
Definition: c.h:229
#define InitFunctionCallInfoData(Fcinfo, Flinfo, Nargs, Collation, Context, Resultinfo)
Definition: fmgr.h:120
int log_statement
Definition: postgres.c:93
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:79
AclResult pg_proc_aclcheck(Oid proc_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4446
int i
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:448
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98
void pq_getmsgend(StringInfo msg)
Definition: pqformat.c:677
ProtocolVersion FrontendProtocol
Definition: globals.c:27