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 77 of file fastpath.c.

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

Referenced by SocketBackend().

78 {
79  int32 ibuf;
80  int nargs;
81 
82  /* Dummy string argument */
83  if (pq_getstring(buf))
84  return EOF;
85  /* Function OID */
86  if (pq_getbytes((char *) &ibuf, 4))
87  return EOF;
88  appendBinaryStringInfo(buf, (char *) &ibuf, 4);
89  /* Number of arguments */
90  if (pq_getbytes((char *) &ibuf, 4))
91  return EOF;
92  appendBinaryStringInfo(buf, (char *) &ibuf, 4);
93  nargs = pg_ntoh32(ibuf);
94  /* For each argument ... */
95  while (nargs-- > 0)
96  {
97  int argsize;
98 
99  /* argsize */
100  if (pq_getbytes((char *) &ibuf, 4))
101  return EOF;
102  appendBinaryStringInfo(buf, (char *) &ibuf, 4);
103  argsize = pg_ntoh32(ibuf);
104  if (argsize < -1)
105  {
106  /* FATAL here since no hope of regaining message sync */
107  ereport(FATAL,
108  (errcode(ERRCODE_PROTOCOL_VIOLATION),
109  errmsg("invalid argument size %d in function call message",
110  argsize)));
111  }
112  /* and arg contents */
113  if (argsize > 0)
114  {
115  /* Allocate space for arg */
116  enlargeStringInfo(buf, argsize);
117  /* And grab it */
118  if (pq_getbytes(buf->data + buf->len, argsize))
119  return EOF;
120  buf->len += argsize;
121  /* Place a trailing null per StringInfo convention */
122  buf->data[buf->len] = '\0';
123  }
124  }
125  return 0;
126 }
int errcode(int sqlerrcode)
Definition: elog.c:575
signed int int32
Definition: c.h:246
#define pg_ntoh32(x)
Definition: pg_bswap.h:125
#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:264
#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 259 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, 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().

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