⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fastpath.c

📁 PostgreSQL 8.1.4的源码 适用于Linux下的开源数据库系统
💻 C
📖 第 1 页 / 共 2 页
字号:
/*------------------------------------------------------------------------- * * fastpath.c *	  routines to handle function requests from the frontend * * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * * * IDENTIFICATION *	  $PostgreSQL: pgsql/src/backend/tcop/fastpath.c,v 1.83.2.1 2005/11/22 18:23:20 momjian Exp $ * * NOTES *	  This cruft is the server side of PQfn. * *------------------------------------------------------------------------- */#include "postgres.h"#include <netinet/in.h>#include <arpa/inet.h>#include "catalog/pg_proc.h"#include "libpq/libpq.h"#include "libpq/pqformat.h"#include "miscadmin.h"#include "mb/pg_wchar.h"#include "tcop/fastpath.h"#include "utils/acl.h"#include "utils/lsyscache.h"#include "utils/syscache.h"#include "utils/tqual.h"/* * Formerly, this code attempted to cache the function and type info * looked up by fetch_fp_info, but only for the duration of a single * transaction command (since in theory the info could change between * commands).  This was utterly useless, because postgres.c executes * each fastpath call as a separate transaction command, and so the * cached data could never actually have been reused.  If it had worked * as intended, it would have had problems anyway with dangling references * in the FmgrInfo struct.	So, forget about caching and just repeat the * syscache fetches on each usage.	They're not *that* expensive. */struct fp_info{	Oid			funcid;	FmgrInfo	flinfo;			/* function lookup info for funcid */	Oid			namespace;		/* other stuff from pg_proc */	Oid			rettype;	Oid			argtypes[FUNC_MAX_ARGS];};static int16 parse_fcall_arguments(StringInfo msgBuf, struct fp_info * fip,					  FunctionCallInfo fcinfo);static int16 parse_fcall_arguments_20(StringInfo msgBuf, struct fp_info * fip,						 FunctionCallInfo fcinfo);/* ---------------- *		GetOldFunctionMessage * * In pre-3.0 protocol, there is no length word on the message, so we have * to have code that understands the message layout to absorb the message * into a buffer.  We want to do this before we start execution, so that * we do not lose sync with the frontend if there's an error. * * The caller should already have initialized buf to empty. * ---------------- */static intGetOldFunctionMessage(StringInfo buf){	int32		ibuf;	int			nargs;	/* Dummy string argument */	if (pq_getstring(buf))		return EOF;	/* Function OID */	if (pq_getbytes((char *) &ibuf, 4))		return EOF;	appendBinaryStringInfo(buf, (char *) &ibuf, 4);	/* Number of arguments */	if (pq_getbytes((char *) &ibuf, 4))		return EOF;	appendBinaryStringInfo(buf, (char *) &ibuf, 4);	nargs = ntohl(ibuf);	/* For each argument ... */	while (nargs-- > 0)	{		int			argsize;		/* argsize */		if (pq_getbytes((char *) &ibuf, 4))			return EOF;		appendBinaryStringInfo(buf, (char *) &ibuf, 4);		argsize = ntohl(ibuf);		if (argsize < -1)		{			/* FATAL here since no hope of regaining message sync */			ereport(FATAL,					(errcode(ERRCODE_PROTOCOL_VIOLATION),				  errmsg("invalid argument size %d in function call message",						 argsize)));		}		/* and arg contents */		if (argsize > 0)		{			/* Allocate space for arg */			enlargeStringInfo(buf, argsize);			/* And grab it */			if (pq_getbytes(buf->data + buf->len, argsize))				return EOF;			buf->len += argsize;			/* Place a trailing null per StringInfo convention */			buf->data[buf->len] = '\0';		}	}	return 0;}/* ---------------- *		SendFunctionResult * * Note: although this routine doesn't check, the format had better be 1 * (binary) when talking to a pre-3.0 client. * ---------------- */static voidSendFunctionResult(Datum retval, bool isnull, Oid rettype, int16 format){	bool		newstyle = (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 3);	StringInfoData buf;	pq_beginmessage(&buf, 'V');	if (isnull)	{		if (newstyle)			pq_sendint(&buf, -1, 4);	}	else	{		if (!newstyle)			pq_sendbyte(&buf, 'G');		if (format == 0)		{			Oid			typoutput;			bool		typisvarlena;			char	   *outputstr;			getTypeOutputInfo(rettype, &typoutput, &typisvarlena);			outputstr = DatumGetCString(OidFunctionCall1(typoutput,														 retval));			pq_sendcountedtext(&buf, outputstr, strlen(outputstr), false);			pfree(outputstr);		}		else if (format == 1)		{			Oid			typsend;			bool		typisvarlena;			bytea	   *outputbytes;			getTypeBinaryOutputInfo(rettype, &typsend, &typisvarlena);			outputbytes = DatumGetByteaP(OidFunctionCall1(typsend,														  retval));			/* We assume the result will not have been toasted */			pq_sendint(&buf, VARSIZE(outputbytes) - VARHDRSZ, 4);			pq_sendbytes(&buf, VARDATA(outputbytes),						 VARSIZE(outputbytes) - VARHDRSZ);			pfree(outputbytes);		}		else			ereport(ERROR,					(errcode(ERRCODE_INVALID_PARAMETER_VALUE),					 errmsg("unsupported format code: %d", format)));	}	if (!newstyle)		pq_sendbyte(&buf, '0');	pq_endmessage(&buf);}/* * fetch_fp_info * * Performs catalog lookups to load a struct fp_info 'fip' for the * function 'func_id'. */static voidfetch_fp_info(Oid func_id, struct fp_info * fip){	HeapTuple	func_htp;	Form_pg_proc pp;	Assert(OidIsValid(func_id));	Assert(fip != NULL);	/*	 * Since the validity of this structure is determined by whether the	 * funcid is OK, we clear the funcid here.	It must not be set to the	 * correct value until we are about to return with a good struct fp_info,	 * since we can be interrupted (i.e., with an ereport(ERROR, ...)) at any	 * time.  [No longer really an issue since we don't save the struct	 * fp_info across transactions anymore, but keep it anyway.]	 */	MemSet(fip, 0, sizeof(struct fp_info));	fip->funcid = InvalidOid;	fmgr_info(func_id, &fip->flinfo);	func_htp = SearchSysCache(PROCOID,							  ObjectIdGetDatum(func_id),							  0, 0, 0);	if (!HeapTupleIsValid(func_htp))		ereport(ERROR,				(errcode(ERRCODE_UNDEFINED_FUNCTION),				 errmsg("function with OID %u does not exist", func_id)));	pp = (Form_pg_proc) GETSTRUCT(func_htp);	/* watch out for catalog entries with more than FUNC_MAX_ARGS args */	if (pp->pronargs > FUNC_MAX_ARGS)		elog(ERROR, "function %s has more than %d arguments",			 NameStr(pp->proname), FUNC_MAX_ARGS);	fip->namespace = pp->pronamespace;	fip->rettype = pp->prorettype;	memcpy(fip->argtypes, pp->proargtypes.values, pp->pronargs * sizeof(Oid));	ReleaseSysCache(func_htp);	/*	 * This must be last!	 */	fip->funcid = func_id;}/* * HandleFunctionRequest * * Server side of PQfn (fastpath function calls from the frontend). * This corresponds to the libpq protocol symbol "F". * * INPUT: *		In protocol version 3, postgres.c has already read the message body *		and will pass it in msgBuf. *		In old protocol, the passed msgBuf is empty and we must read the *		message here. * * RETURNS: *		0 if successful completion, EOF if frontend connection lost. * * Note: All ordinary errors result in ereport(ERROR,...).	However, * if we lose the frontend connection there is no one to ereport to, * and no use in proceeding... * * Note: palloc()s done here and in the called function do not need to be * cleaned up explicitly.  We are called from PostgresMain() in the * MessageContext memory context, which will be automatically reset when * control returns to PostgresMain. */intHandleFunctionRequest(StringInfo msgBuf){	Oid			fid;	AclResult	aclresult;	FunctionCallInfoData fcinfo;	int16		rformat;	Datum		retval;	struct fp_info my_fp;	struct fp_info *fip;	bool		callit;	/*	 * Read message contents if not already done.	 */	if (PG_PROTOCOL_MAJOR(FrontendProtocol) < 3)	{		if (GetOldFunctionMessage(msgBuf))		{			ereport(COMMERROR,					(errcode(ERRCODE_PROTOCOL_VIOLATION),					 errmsg("unexpected EOF on client connection")));			return EOF;		}	}	/*	 * Now that we've eaten the input message, check to see if we actually	 * want to do the function call or not.  It's now safe to ereport(); we	 * won't lose sync with the frontend.	 */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -