Implement new-protocol binary I/O support in DataRow, Bind, and FunctionCall

messages.  Binary I/O is now up and working, but only for a small set
of datatypes (integers, text, bytea).
This commit is contained in:
Tom Lane 2003-05-09 18:08:48 +00:00
parent d85a0a6bef
commit 0ac6298bb8
7 changed files with 434 additions and 344 deletions

View File

@ -9,7 +9,7 @@
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/access/common/printtup.c,v 1.71 2003/05/08 18:16:36 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/access/common/printtup.c,v 1.72 2003/05/09 18:08:48 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -42,14 +42,19 @@ static void printtup_destroy(DestReceiver *self);
/* ---------------- /* ----------------
* Private state for a printtup destination object * Private state for a printtup destination object
*
* NOTE: finfo is the lookup info for either typoutput or typsend, whichever
* we are using for this column.
* ---------------- * ----------------
*/ */
typedef struct typedef struct
{ /* Per-attribute information */ { /* Per-attribute information */
Oid typoutput; /* Oid for the attribute's type output fn */ Oid typoutput; /* Oid for the type's text output fn */
Oid typsend; /* Oid for the type's binary output fn */
Oid typelem; /* typelem value to pass to the output fn */ Oid typelem; /* typelem value to pass to the output fn */
bool typisvarlena; /* is it varlena (ie possibly toastable)? */ bool typisvarlena; /* is it varlena (ie possibly toastable)? */
FmgrInfo finfo; /* Precomputed call info for typoutput */ int16 format; /* format code for this column */
FmgrInfo finfo; /* Precomputed call info for output fn */
} PrinttupAttrInfo; } PrinttupAttrInfo;
typedef struct typedef struct
@ -219,9 +224,13 @@ SendRowDescriptionMessage(TupleDesc typeinfo, List *targetlist, int16 *formats)
pq_endmessage(&buf); pq_endmessage(&buf);
} }
/*
* Get the lookup info that printtup() needs
*/
static void static void
printtup_prepare_info(DR_printtup *myState, TupleDesc typeinfo, int numAttrs) printtup_prepare_info(DR_printtup *myState, TupleDesc typeinfo, int numAttrs)
{ {
int16 *formats = myState->portal->formats;
int i; int i;
if (myState->myinfo) if (myState->myinfo)
@ -232,16 +241,32 @@ printtup_prepare_info(DR_printtup *myState, TupleDesc typeinfo, int numAttrs)
if (numAttrs <= 0) if (numAttrs <= 0)
return; return;
myState->myinfo = (PrinttupAttrInfo *) myState->myinfo = (PrinttupAttrInfo *)
palloc(numAttrs * sizeof(PrinttupAttrInfo)); palloc0(numAttrs * sizeof(PrinttupAttrInfo));
for (i = 0; i < numAttrs; i++) for (i = 0; i < numAttrs; i++)
{ {
PrinttupAttrInfo *thisState = myState->myinfo + i; PrinttupAttrInfo *thisState = myState->myinfo + i;
int16 format = (formats ? formats[i] : 0);
if (getTypeOutputInfo(typeinfo->attrs[i]->atttypid, thisState->format = format;
&thisState->typoutput, &thisState->typelem, if (format == 0)
&thisState->typisvarlena)) {
getTypeOutputInfo(typeinfo->attrs[i]->atttypid,
&thisState->typoutput,
&thisState->typelem,
&thisState->typisvarlena);
fmgr_info(thisState->typoutput, &thisState->finfo); fmgr_info(thisState->typoutput, &thisState->finfo);
} }
else if (format == 1)
{
getTypeBinaryOutputInfo(typeinfo->attrs[i]->atttypid,
&thisState->typsend,
&thisState->typelem,
&thisState->typisvarlena);
fmgr_info(thisState->typsend, &thisState->finfo);
}
else
elog(ERROR, "Unsupported format code %d", format);
}
} }
/* ---------------- /* ----------------
@ -252,7 +277,6 @@ static void
printtup(HeapTuple tuple, TupleDesc typeinfo, DestReceiver *self) printtup(HeapTuple tuple, TupleDesc typeinfo, DestReceiver *self)
{ {
DR_printtup *myState = (DR_printtup *) self; DR_printtup *myState = (DR_printtup *) self;
int16 *formats = myState->portal->formats;
StringInfoData buf; StringInfoData buf;
int natts = tuple->t_data->t_natts; int natts = tuple->t_data->t_natts;
int i; int i;
@ -274,11 +298,9 @@ printtup(HeapTuple tuple, TupleDesc typeinfo, DestReceiver *self)
for (i = 0; i < natts; ++i) for (i = 0; i < natts; ++i)
{ {
PrinttupAttrInfo *thisState = myState->myinfo + i; PrinttupAttrInfo *thisState = myState->myinfo + i;
int16 format = (formats ? formats[i] : 0);
Datum origattr, Datum origattr,
attr; attr;
bool isnull; bool isnull;
char *outputstr;
origattr = heap_getattr(tuple, i + 1, typeinfo, &isnull); origattr = heap_getattr(tuple, i + 1, typeinfo, &isnull);
if (isnull) if (isnull)
@ -286,10 +308,7 @@ printtup(HeapTuple tuple, TupleDesc typeinfo, DestReceiver *self)
pq_sendint(&buf, -1, 4); pq_sendint(&buf, -1, 4);
continue; continue;
} }
if (format == 0)
{
if (OidIsValid(thisState->typoutput))
{
/* /*
* If we have a toasted datum, forcibly detoast it here to * If we have a toasted datum, forcibly detoast it here to
* avoid memory leakage inside the type's output routine. * avoid memory leakage inside the type's output routine.
@ -299,33 +318,36 @@ printtup(HeapTuple tuple, TupleDesc typeinfo, DestReceiver *self)
else else
attr = origattr; attr = origattr;
if (thisState->format == 0)
{
/* Text output */
char *outputstr;
outputstr = DatumGetCString(FunctionCall3(&thisState->finfo, outputstr = DatumGetCString(FunctionCall3(&thisState->finfo,
attr, attr,
ObjectIdGetDatum(thisState->typelem), ObjectIdGetDatum(thisState->typelem),
Int32GetDatum(typeinfo->attrs[i]->atttypmod))); Int32GetDatum(typeinfo->attrs[i]->atttypmod)));
pq_sendcountedtext(&buf, outputstr, strlen(outputstr), false); pq_sendcountedtext(&buf, outputstr, strlen(outputstr), false);
/* Clean up detoasted copy, if any */
if (attr != origattr)
pfree(DatumGetPointer(attr));
pfree(outputstr); pfree(outputstr);
} }
else else
{ {
outputstr = "<unprintable>"; /* Binary output */
pq_sendcountedtext(&buf, outputstr, strlen(outputstr), false); bytea *outputbytes;
}
} outputbytes = DatumGetByteaP(FunctionCall2(&thisState->finfo,
else if (format == 1) attr,
{ ObjectIdGetDatum(thisState->typelem)));
/* XXX something similar to above */ /* We assume the result will not have been toasted */
elog(ERROR, "Binary transmission not implemented yet"); pq_sendint(&buf, VARSIZE(outputbytes) - VARHDRSZ, 4);
} pq_sendbytes(&buf, VARDATA(outputbytes),
else VARSIZE(outputbytes) - VARHDRSZ);
{ pfree(outputbytes);
elog(ERROR, "Invalid format code %d", format);
} }
/* Clean up detoasted copy, if any */
if (attr != origattr)
pfree(DatumGetPointer(attr));
} }
pq_endmessage(&buf); pq_endmessage(&buf);
@ -388,8 +410,9 @@ printtup_20(HeapTuple tuple, TupleDesc typeinfo, DestReceiver *self)
origattr = heap_getattr(tuple, i + 1, typeinfo, &isnull); origattr = heap_getattr(tuple, i + 1, typeinfo, &isnull);
if (isnull) if (isnull)
continue; continue;
if (OidIsValid(thisState->typoutput))
{ Assert(thisState->format == 0);
/* /*
* If we have a toasted datum, forcibly detoast it here to * If we have a toasted datum, forcibly detoast it here to
* avoid memory leakage inside the type's output routine. * avoid memory leakage inside the type's output routine.
@ -403,19 +426,12 @@ printtup_20(HeapTuple tuple, TupleDesc typeinfo, DestReceiver *self)
attr, attr,
ObjectIdGetDatum(thisState->typelem), ObjectIdGetDatum(thisState->typelem),
Int32GetDatum(typeinfo->attrs[i]->atttypmod))); Int32GetDatum(typeinfo->attrs[i]->atttypmod)));
pq_sendcountedtext(&buf, outputstr, strlen(outputstr), true); pq_sendcountedtext(&buf, outputstr, strlen(outputstr), true);
pfree(outputstr);
/* Clean up detoasted copy, if any */ /* Clean up detoasted copy, if any */
if (attr != origattr) if (attr != origattr)
pfree(DatumGetPointer(attr)); pfree(DatumGetPointer(attr));
pfree(outputstr);
}
else
{
outputstr = "<unprintable>";
pq_sendcountedtext(&buf, outputstr, strlen(outputstr), true);
}
} }
pq_endmessage(&buf); pq_endmessage(&buf);
@ -508,9 +524,8 @@ debugtup(HeapTuple tuple, TupleDesc typeinfo, DestReceiver *self)
origattr = heap_getattr(tuple, i + 1, typeinfo, &isnull); origattr = heap_getattr(tuple, i + 1, typeinfo, &isnull);
if (isnull) if (isnull)
continue; continue;
if (getTypeOutputInfo(typeinfo->attrs[i]->atttypid, getTypeOutputInfo(typeinfo->attrs[i]->atttypid,
&typoutput, &typelem, &typisvarlena)) &typoutput, &typelem, &typisvarlena);
{
/* /*
* If we have a toasted datum, forcibly detoast it here to * If we have a toasted datum, forcibly detoast it here to
* avoid memory leakage inside the type's output routine. * avoid memory leakage inside the type's output routine.
@ -527,11 +542,11 @@ debugtup(HeapTuple tuple, TupleDesc typeinfo, DestReceiver *self)
printatt((unsigned) i + 1, typeinfo->attrs[i], value); printatt((unsigned) i + 1, typeinfo->attrs[i], value);
pfree(value);
/* Clean up detoasted copy, if any */ /* Clean up detoasted copy, if any */
if (attr != origattr) if (attr != origattr)
pfree(DatumGetPointer(attr)); pfree(DatumGetPointer(attr));
pfree(value);
}
} }
printf("\t----\n"); printf("\t----\n");
} }
@ -542,7 +557,7 @@ debugtup(HeapTuple tuple, TupleDesc typeinfo, DestReceiver *self)
* We use a different message type, i.e. 'B' instead of 'D' to * We use a different message type, i.e. 'B' instead of 'D' to
* indicate a tuple in internal (binary) form. * indicate a tuple in internal (binary) form.
* *
* This is largely same as printtup_20, except we don't use the typout func. * This is largely same as printtup_20, except we use binary formatting.
* ---------------- * ----------------
*/ */
static void static void
@ -587,84 +602,42 @@ printtup_internal_20(HeapTuple tuple, TupleDesc typeinfo, DestReceiver *self)
/* /*
* send the attributes of this tuple * send the attributes of this tuple
*/ */
#ifdef IPORTAL_DEBUG
fprintf(stderr, "sending tuple with %d atts\n", natts);
#endif
for (i = 0; i < natts; ++i) for (i = 0; i < natts; ++i)
{ {
PrinttupAttrInfo *thisState = myState->myinfo + i; PrinttupAttrInfo *thisState = myState->myinfo + i;
Datum origattr, Datum origattr,
attr; attr;
bool isnull; bool isnull;
int32 len; bytea *outputbytes;
origattr = heap_getattr(tuple, i + 1, typeinfo, &isnull); origattr = heap_getattr(tuple, i + 1, typeinfo, &isnull);
if (isnull) if (isnull)
continue; continue;
/* send # of bytes, and opaque data */
if (thisState->typisvarlena) Assert(thisState->format == 1);
{
/* /*
* If we have a toasted datum, must detoast before sending. * If we have a toasted datum, forcibly detoast it here to
* avoid memory leakage inside the type's output routine.
*/ */
if (thisState->typisvarlena)
attr = PointerGetDatum(PG_DETOAST_DATUM(origattr)); attr = PointerGetDatum(PG_DETOAST_DATUM(origattr));
else
attr = origattr;
len = VARSIZE(attr) - VARHDRSZ; outputbytes = DatumGetByteaP(FunctionCall2(&thisState->finfo,
attr,
pq_sendint(&buf, len, VARHDRSZ); ObjectIdGetDatum(thisState->typelem)));
pq_sendbytes(&buf, VARDATA(attr), len); /* We assume the result will not have been toasted */
pq_sendint(&buf, VARSIZE(outputbytes) - VARHDRSZ, 4);
#ifdef IPORTAL_DEBUG pq_sendbytes(&buf, VARDATA(outputbytes),
{ VARSIZE(outputbytes) - VARHDRSZ);
char *d = VARDATA(attr); pfree(outputbytes);
fprintf(stderr, "length %d data %x %x %x %x\n",
len, *d, *(d + 1), *(d + 2), *(d + 3));
}
#endif
/* Clean up detoasted copy, if any */ /* Clean up detoasted copy, if any */
if (attr != origattr) if (attr != origattr)
pfree(DatumGetPointer(attr)); pfree(DatumGetPointer(attr));
} }
else
{
/* fixed size or cstring */
attr = origattr;
len = typeinfo->attrs[i]->attlen;
if (len <= 0)
{
/* it's a cstring */
Assert(len == -2 && !typeinfo->attrs[i]->attbyval);
len = strlen(DatumGetCString(attr)) + 1;
}
pq_sendint(&buf, len, sizeof(int32));
if (typeinfo->attrs[i]->attbyval)
{
Datum datumBuf;
/*
* We need this horsing around because we don't know how
* shorter data values are aligned within a Datum.
*/
store_att_byval(&datumBuf, attr, len);
pq_sendbytes(&buf, (char *) &datumBuf, len);
#ifdef IPORTAL_DEBUG
fprintf(stderr, "byval length %d data %ld\n", len,
(long) attr);
#endif
}
else
{
pq_sendbytes(&buf, DatumGetPointer(attr), len);
#ifdef IPORTAL_DEBUG
fprintf(stderr, "byref length %d data %p\n", len,
DatumGetPointer(attr));
#endif
}
}
}
pq_endmessage(&buf); pq_endmessage(&buf);
} }

View File

@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/commands/copy.c,v 1.198 2003/05/08 18:16:36 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/commands/copy.c,v 1.199 2003/05/09 18:08:48 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -828,11 +828,9 @@ CopyTo(Relation rel, List *attnumlist, bool binary, bool oids,
int attnum = lfirsti(cur); int attnum = lfirsti(cur);
Oid out_func_oid; Oid out_func_oid;
if (!getTypeOutputInfo(attr[attnum - 1]->atttypid, getTypeOutputInfo(attr[attnum - 1]->atttypid,
&out_func_oid, &elements[attnum - 1], &out_func_oid, &elements[attnum - 1],
&isvarlena[attnum - 1])) &isvarlena[attnum - 1]);
elog(ERROR, "COPY: couldn't lookup info for type %u",
attr[attnum - 1]->atttypid);
fmgr_info(out_func_oid, &out_functions[attnum - 1]); fmgr_info(out_func_oid, &out_functions[attnum - 1]);
if (binary && attr[attnum - 1]->attlen == -2) if (binary && attr[attnum - 1]->attlen == -2)
elog(ERROR, "COPY BINARY: cstring not supported"); elog(ERROR, "COPY BINARY: cstring not supported");

View File

@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/executor/spi.c,v 1.97 2003/05/08 18:16:36 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/executor/spi.c,v 1.98 2003/05/09 18:08:48 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -546,11 +546,7 @@ SPI_getvalue(HeapTuple tuple, TupleDesc tupdesc, int fnumber)
typmod = -1; typmod = -1;
} }
if (!getTypeOutputInfo(typoid, &foutoid, &typelem, &typisvarlena)) getTypeOutputInfo(typoid, &foutoid, &typelem, &typisvarlena);
{
SPI_result = SPI_ERROR_NOOUTFUNC;
return NULL;
}
/* /*
* If we have a toasted datum, forcibly detoast it here to avoid * If we have a toasted datum, forcibly detoast it here to avoid

View File

@ -8,26 +8,11 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/tcop/fastpath.c,v 1.62 2003/05/08 18:16:36 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/tcop/fastpath.c,v 1.63 2003/05/09 18:08:48 tgl Exp $
* *
* NOTES * NOTES
* This cruft is the server side of PQfn. * This cruft is the server side of PQfn.
* *
* - jolly 07/11/95:
*
* no longer rely on return sizes provided by the frontend. Always
* use the true lengths for the catalogs. Assume that the frontend
* has allocated enough space to handle the result value returned.
*
* trust that the user knows what he is doing with the args. If the
* sys catalog says it is a varlena, assume that the user is only sending
* down VARDATA and that the argsize is the VARSIZE. If the arg is
* fixed len, assume that the argsize given by the user is correct.
*
* if the function returns by value, then only send 4 bytes value
* back to the frontend. If the return returns by reference,
* send down only the data portion and set the return size appropriately.
*
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
#include "postgres.h" #include "postgres.h"
@ -35,11 +20,11 @@
#include <netinet/in.h> #include <netinet/in.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include "access/xact.h"
#include "catalog/pg_proc.h" #include "catalog/pg_proc.h"
#include "libpq/libpq.h" #include "libpq/libpq.h"
#include "libpq/pqformat.h" #include "libpq/pqformat.h"
#include "miscadmin.h" #include "miscadmin.h"
#include "mb/pg_wchar.h"
#include "tcop/fastpath.h" #include "tcop/fastpath.h"
#include "utils/acl.h" #include "utils/acl.h"
#include "utils/lsyscache.h" #include "utils/lsyscache.h"
@ -62,16 +47,15 @@ struct fp_info
{ {
Oid funcid; Oid funcid;
FmgrInfo flinfo; /* function lookup info for funcid */ FmgrInfo flinfo; /* function lookup info for funcid */
int16 arglen[FUNC_MAX_ARGS]; Oid namespace; /* other stuff from pg_proc */
bool argbyval[FUNC_MAX_ARGS]; Oid rettype;
int16 retlen; Oid argtypes[FUNC_MAX_ARGS];
bool retbyval;
}; };
static void parse_fcall_arguments(StringInfo msgBuf, struct fp_info *fip, static int16 parse_fcall_arguments(StringInfo msgBuf, struct fp_info *fip,
FunctionCallInfo fcinfo); FunctionCallInfo fcinfo);
static void parse_fcall_arguments_20(StringInfo msgBuf, struct fp_info *fip, static int16 parse_fcall_arguments_20(StringInfo msgBuf, struct fp_info *fip,
FunctionCallInfo fcinfo); FunctionCallInfo fcinfo);
@ -114,7 +98,7 @@ GetOldFunctionMessage(StringInfo buf)
return EOF; return EOF;
appendBinaryStringInfo(buf, (char *) &ibuf, 4); appendBinaryStringInfo(buf, (char *) &ibuf, 4);
argsize = ntohl(ibuf); argsize = ntohl(ibuf);
if (argsize < 0) if (argsize < -1)
{ {
/* FATAL here since no hope of regaining message sync */ /* FATAL here since no hope of regaining message sync */
elog(FATAL, "HandleFunctionRequest: bogus argsize %d", elog(FATAL, "HandleFunctionRequest: bogus argsize %d",
@ -139,78 +123,68 @@ GetOldFunctionMessage(StringInfo buf)
/* ---------------- /* ----------------
* SendFunctionResult * SendFunctionResult
* *
* retlen is 0 if returning NULL, else the typlen according to the catalogs * Note: although this routine doesn't check, the format had better be 1
* (binary) when talking to a pre-3.0 client.
* ---------------- * ----------------
*/ */
static void static void
SendFunctionResult(Datum retval, bool retbyval, int retlen) SendFunctionResult(Datum retval, bool isnull, Oid rettype, int16 format)
{ {
bool newstyle = (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 3);
StringInfoData buf; StringInfoData buf;
pq_beginmessage(&buf, 'V'); pq_beginmessage(&buf, 'V');
if (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 3) if (isnull)
{ {
/* New-style message */ if (newstyle)
/* XXX replace this with standard binary (or text!) output */
if (retlen != 0)
{
if (retbyval)
{ /* by-value */
pq_sendint(&buf, retlen, 4);
pq_sendint(&buf, DatumGetInt32(retval), retlen);
}
else
{ /* by-reference ... */
if (retlen == -1)
{ /* ... varlena */
struct varlena *v = PG_DETOAST_DATUM(retval);
pq_sendint(&buf, VARSIZE(v) - VARHDRSZ, VARHDRSZ);
pq_sendbytes(&buf, VARDATA(v), VARSIZE(v) - VARHDRSZ);
}
else
{ /* ... fixed */
pq_sendint(&buf, retlen, 4);
pq_sendbytes(&buf, DatumGetPointer(retval), retlen);
}
}
}
else
{
/* NULL marker */
pq_sendint(&buf, -1, 4); pq_sendint(&buf, -1, 4);
} }
}
else else
{ {
/* Old-style message */ if (!newstyle)
if (retlen != 0)
{
pq_sendbyte(&buf, 'G'); pq_sendbyte(&buf, 'G');
if (retbyval)
{ /* by-value */
pq_sendint(&buf, retlen, 4);
pq_sendint(&buf, DatumGetInt32(retval), retlen);
}
else
{ /* by-reference ... */
if (retlen == -1)
{ /* ... varlena */
struct varlena *v = PG_DETOAST_DATUM(retval);
pq_sendint(&buf, VARSIZE(v) - VARHDRSZ, VARHDRSZ); if (format == 0)
pq_sendbytes(&buf, VARDATA(v), VARSIZE(v) - VARHDRSZ); {
Oid typoutput,
typelem;
bool typisvarlena;
char *outputstr;
getTypeOutputInfo(rettype,
&typoutput, &typelem, &typisvarlena);
outputstr = DatumGetCString(OidFunctionCall3(typoutput,
retval,
ObjectIdGetDatum(typelem),
Int32GetDatum(-1)));
pq_sendcountedtext(&buf, outputstr, strlen(outputstr), false);
pfree(outputstr);
}
else if (format == 1)
{
Oid typsend,
typelem;
bool typisvarlena;
bytea *outputbytes;
getTypeBinaryOutputInfo(rettype,
&typsend, &typelem, &typisvarlena);
outputbytes = DatumGetByteaP(OidFunctionCall2(typsend,
retval,
ObjectIdGetDatum(typelem)));
/* 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 else
{ /* ... fixed */ elog(ERROR, "Invalid format code %d", format);
pq_sendint(&buf, retlen, 4);
pq_sendbytes(&buf, DatumGetPointer(retval), retlen);
}
}
} }
if (!newstyle)
pq_sendbyte(&buf, '0'); pq_sendbyte(&buf, '0');
}
pq_endmessage(&buf); pq_endmessage(&buf);
} }
@ -224,11 +198,8 @@ SendFunctionResult(Datum retval, bool retbyval, int retlen)
static void static void
fetch_fp_info(Oid func_id, struct fp_info * fip) fetch_fp_info(Oid func_id, struct fp_info * fip)
{ {
Oid *argtypes; /* an oidvector */
Oid rettype;
HeapTuple func_htp; HeapTuple func_htp;
Form_pg_proc pp; Form_pg_proc pp;
int i;
Assert(OidIsValid(func_id)); Assert(OidIsValid(func_id));
Assert(fip != (struct fp_info *) NULL); Assert(fip != (struct fp_info *) NULL);
@ -254,20 +225,10 @@ fetch_fp_info(Oid func_id, struct fp_info * fip)
elog(ERROR, "fetch_fp_info: cache lookup for function %u failed", elog(ERROR, "fetch_fp_info: cache lookup for function %u failed",
func_id); func_id);
pp = (Form_pg_proc) GETSTRUCT(func_htp); pp = (Form_pg_proc) GETSTRUCT(func_htp);
rettype = pp->prorettype;
argtypes = pp->proargtypes;
for (i = 0; i < pp->pronargs; ++i) fip->namespace = pp->pronamespace;
{ fip->rettype = pp->prorettype;
get_typlenbyval(argtypes[i], &fip->arglen[i], &fip->argbyval[i]); memcpy(fip->argtypes, pp->proargtypes, FUNC_MAX_ARGS * sizeof(Oid));
/* We don't support cstring in fastpath protocol */
if (fip->arglen[i] == -2)
elog(ERROR, "CSTRING not supported in fastpath protocol");
}
get_typlenbyval(rettype, &fip->retlen, &fip->retbyval);
if (fip->retlen == -2)
elog(ERROR, "CSTRING not supported in fastpath protocol");
ReleaseSysCache(func_htp); ReleaseSysCache(func_htp);
@ -308,6 +269,7 @@ HandleFunctionRequest(StringInfo msgBuf)
Oid fid; Oid fid;
AclResult aclresult; AclResult aclresult;
FunctionCallInfoData fcinfo; FunctionCallInfoData fcinfo;
int16 rformat;
Datum retval; Datum retval;
struct fp_info my_fp; struct fp_info my_fp;
struct fp_info *fip; struct fp_info *fip;
@ -334,7 +296,7 @@ HandleFunctionRequest(StringInfo msgBuf)
"queries ignored until end of transaction block"); "queries ignored until end of transaction block");
/* /*
* Parse the buffer contents. * Begin parsing the buffer contents.
*/ */
if (PG_PROTOCOL_MAJOR(FrontendProtocol) < 3) if (PG_PROTOCOL_MAJOR(FrontendProtocol) < 3)
(void) pq_getmsgstring(msgBuf); /* dummy string */ (void) pq_getmsgstring(msgBuf); /* dummy string */
@ -348,7 +310,14 @@ HandleFunctionRequest(StringInfo msgBuf)
fip = &my_fp; fip = &my_fp;
fetch_fp_info(fid, fip); fetch_fp_info(fid, fip);
/* Check permission to call function */ /*
* Check permission to access and call function. Since we didn't go
* through a normal name lookup, we need to check schema usage too.
*/
aclresult = pg_namespace_aclcheck(fip->namespace, GetUserId(), ACL_USAGE);
if (aclresult != ACLCHECK_OK)
aclcheck_error(aclresult, get_namespace_name(fip->namespace));
aclresult = pg_proc_aclcheck(fid, GetUserId(), ACL_EXECUTE); aclresult = pg_proc_aclcheck(fid, GetUserId(), ACL_EXECUTE);
if (aclresult != ACLCHECK_OK) if (aclresult != ACLCHECK_OK)
aclcheck_error(aclresult, get_func_name(fid)); aclcheck_error(aclresult, get_func_name(fid));
@ -365,9 +334,9 @@ HandleFunctionRequest(StringInfo msgBuf)
fcinfo.flinfo = &fip->flinfo; fcinfo.flinfo = &fip->flinfo;
if (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 3) if (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 3)
parse_fcall_arguments(msgBuf, fip, &fcinfo); rformat = parse_fcall_arguments(msgBuf, fip, &fcinfo);
else else
parse_fcall_arguments_20(msgBuf, fip, &fcinfo); rformat = parse_fcall_arguments_20(msgBuf, fip, &fcinfo);
/* Verify we reached the end of the message where expected. */ /* Verify we reached the end of the message where expected. */
pq_getmsgend(msgBuf); pq_getmsgend(msgBuf);
@ -375,18 +344,18 @@ HandleFunctionRequest(StringInfo msgBuf)
/* Okay, do it ... */ /* Okay, do it ... */
retval = FunctionCallInvoke(&fcinfo); retval = FunctionCallInvoke(&fcinfo);
if (fcinfo.isnull) SendFunctionResult(retval, fcinfo.isnull, fip->rettype, rformat);
SendFunctionResult(retval, fip->retbyval, 0);
else
SendFunctionResult(retval, fip->retbyval, fip->retlen);
return 0; return 0;
} }
/* /*
* Parse function arguments in a 3.0 protocol message * Parse function arguments in a 3.0 protocol message
*
* Argument values are loaded into *fcinfo, and the desired result format
* is returned.
*/ */
static void static int16
parse_fcall_arguments(StringInfo msgBuf, struct fp_info *fip, parse_fcall_arguments(StringInfo msgBuf, struct fp_info *fip,
FunctionCallInfo fcinfo) FunctionCallInfo fcinfo)
{ {
@ -394,6 +363,7 @@ parse_fcall_arguments(StringInfo msgBuf, struct fp_info *fip,
int i; int i;
int numAFormats; int numAFormats;
int16 *aformats = NULL; int16 *aformats = NULL;
StringInfoData abuf;
/* Get the argument format codes */ /* Get the argument format codes */
numAFormats = pq_getmsgint(msgBuf, 2); numAFormats = pq_getmsgint(msgBuf, 2);
@ -412,59 +382,110 @@ parse_fcall_arguments(StringInfo msgBuf, struct fp_info *fip,
fcinfo->nargs = nargs; fcinfo->nargs = nargs;
if (numAFormats > 1 && numAFormats != nargs)
elog(ERROR, "Function Call message has %d argument formats but %d arguments",
numAFormats, nargs);
initStringInfo(&abuf);
/* /*
* Copy supplied arguments into arg vector. * Copy supplied arguments into arg vector.
*/ */
for (i = 0; i < nargs; ++i) for (i = 0; i < nargs; ++i)
{ {
int argsize; int argsize;
char *p; int16 aformat;
argsize = pq_getmsgint(msgBuf, 4); argsize = pq_getmsgint(msgBuf, 4);
if (fip->argbyval[i]) if (argsize == -1)
{ /* by-value */ {
if (argsize < 1 || argsize > 4) fcinfo->argnull[i] = true;
elog(ERROR, "HandleFunctionRequest: bogus argsize %d", continue;
argsize);
/* XXX should we demand argsize == fip->arglen[i] ? */
fcinfo->arg[i] = (Datum) pq_getmsgint(msgBuf, argsize);
} }
else
{ /* by-reference ... */
if (fip->arglen[i] == -1)
{ /* ... varlena */
if (argsize < 0) if (argsize < 0)
elog(ERROR, "HandleFunctionRequest: bogus argsize %d", elog(ERROR, "HandleFunctionRequest: bogus argsize %d",
argsize); argsize);
p = palloc(argsize + VARHDRSZ);
VARATT_SIZEP(p) = argsize + VARHDRSZ; /* Reset abuf to empty, and insert raw data into it */
pq_copymsgbytes(msgBuf, VARDATA(p), argsize); abuf.len = 0;
abuf.data[0] = '\0';
abuf.cursor = 0;
appendBinaryStringInfo(&abuf,
pq_getmsgbytes(msgBuf, argsize),
argsize);
if (numAFormats > 1)
aformat = aformats[i];
else if (numAFormats > 0)
aformat = aformats[0];
else
aformat = 0; /* default = text */
if (aformat == 0)
{
Oid typInput;
Oid typElem;
char *pstring;
getTypeInputInfo(fip->argtypes[i], &typInput, &typElem);
/*
* Since stringinfo.c keeps a trailing null in
* place even for binary data, the contents of
* abuf are a valid C string. We have to do
* encoding conversion before calling the typinput
* routine, though.
*/
pstring = (char *)
pg_client_to_server((unsigned char *) abuf.data,
argsize);
fcinfo->arg[i] =
OidFunctionCall3(typInput,
CStringGetDatum(pstring),
ObjectIdGetDatum(typElem),
Int32GetDatum(-1));
/* Free result of encoding conversion, if any */
if (pstring != abuf.data)
pfree(pstring);
}
else if (aformat == 1)
{
Oid typReceive;
Oid typElem;
/* Call the argument type's binary input converter */
getTypeBinaryInputInfo(fip->argtypes[i], &typReceive, &typElem);
fcinfo->arg[i] = OidFunctionCall2(typReceive,
PointerGetDatum(&abuf),
ObjectIdGetDatum(typElem));
/* Trouble if it didn't eat the whole buffer */
if (abuf.cursor != abuf.len)
elog(ERROR, "Improper binary format in function argument %d",
i + 1);
} }
else else
{ /* ... fixed */ elog(ERROR, "Invalid format code %d", aformat);
if (argsize != fip->arglen[i])
elog(ERROR, "HandleFunctionRequest: bogus argsize %d, should be %d",
argsize, fip->arglen[i]);
p = palloc(argsize + 1); /* +1 in case argsize is 0 */
pq_copymsgbytes(msgBuf, p, argsize);
}
fcinfo->arg[i] = PointerGetDatum(p);
}
} }
/* XXX for the moment, ignore result format code */ /* Return result format code */
(void) pq_getmsgint(msgBuf, 2); return (int16) pq_getmsgint(msgBuf, 2);
} }
/* /*
* Parse function arguments in a 2.0 protocol message * Parse function arguments in a 2.0 protocol message
*
* Argument values are loaded into *fcinfo, and the desired result format
* is returned.
*/ */
static void static int16
parse_fcall_arguments_20(StringInfo msgBuf, struct fp_info *fip, parse_fcall_arguments_20(StringInfo msgBuf, struct fp_info *fip,
FunctionCallInfo fcinfo) FunctionCallInfo fcinfo)
{ {
int nargs; int nargs;
int i; int i;
StringInfoData abuf;
nargs = pq_getmsgint(msgBuf, 4); /* # of arguments */ nargs = pq_getmsgint(msgBuf, 4); /* # of arguments */
@ -474,44 +495,54 @@ parse_fcall_arguments_20(StringInfo msgBuf, struct fp_info *fip,
fcinfo->nargs = nargs; fcinfo->nargs = nargs;
initStringInfo(&abuf);
/* /*
* Copy supplied arguments into arg vector. Note there is no way for * Copy supplied arguments into arg vector. In protocol 2.0 these are
* frontend to specify a NULL argument --- this protocol is misdesigned. * always assumed to be supplied in binary format.
*
* Note: although the original protocol 2.0 code did not have any way for
* the frontend to specify a NULL argument, we now choose to interpret
* length == -1 as meaning a NULL.
*/ */
for (i = 0; i < nargs; ++i) for (i = 0; i < nargs; ++i)
{ {
int argsize; int argsize;
char *p; Oid typReceive;
Oid typElem;
argsize = pq_getmsgint(msgBuf, 4); argsize = pq_getmsgint(msgBuf, 4);
if (fip->argbyval[i]) if (argsize == -1)
{ /* by-value */ {
if (argsize < 1 || argsize > 4) fcinfo->argnull[i] = true;
elog(ERROR, "HandleFunctionRequest: bogus argsize %d", continue;
argsize);
/* XXX should we demand argsize == fip->arglen[i] ? */
fcinfo->arg[i] = (Datum) pq_getmsgint(msgBuf, argsize);
} }
else
{ /* by-reference ... */
if (fip->arglen[i] == -1)
{ /* ... varlena */
if (argsize < 0) if (argsize < 0)
elog(ERROR, "HandleFunctionRequest: bogus argsize %d", elog(ERROR, "HandleFunctionRequest: bogus argsize %d",
argsize); argsize);
p = palloc(argsize + VARHDRSZ);
VARATT_SIZEP(p) = argsize + VARHDRSZ; /* Reset abuf to empty, and insert raw data into it */
pq_copymsgbytes(msgBuf, VARDATA(p), argsize); abuf.len = 0;
} abuf.data[0] = '\0';
else abuf.cursor = 0;
{ /* ... fixed */
if (argsize != fip->arglen[i]) appendBinaryStringInfo(&abuf,
elog(ERROR, "HandleFunctionRequest: bogus argsize %d, should be %d", pq_getmsgbytes(msgBuf, argsize),
argsize, fip->arglen[i]); argsize);
p = palloc(argsize + 1); /* +1 in case argsize is 0 */
pq_copymsgbytes(msgBuf, p, argsize); /* Call the argument type's binary input converter */
} getTypeBinaryInputInfo(fip->argtypes[i], &typReceive, &typElem);
fcinfo->arg[i] = PointerGetDatum(p);
} fcinfo->arg[i] = OidFunctionCall2(typReceive,
PointerGetDatum(&abuf),
ObjectIdGetDatum(typElem));
/* Trouble if it didn't eat the whole buffer */
if (abuf.cursor != abuf.len)
elog(ERROR, "Improper binary format in function argument %d",
i + 1);
} }
/* Desired result format is always binary in protocol 2.0 */
return 1;
} }

View File

@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/tcop/postgres.c,v 1.341 2003/05/09 15:57:24 momjian Exp $ * $Header: /cvsroot/pgsql/src/backend/tcop/postgres.c,v 1.342 2003/05/09 18:08:48 tgl Exp $
* *
* NOTES * NOTES
* this is the "main" module of the postgres backend and * this is the "main" module of the postgres backend and
@ -1342,8 +1342,21 @@ exec_bind_message(StringInfo input_message)
} }
else if (pformat == 1) else if (pformat == 1)
{ {
/* XXX something similar to above */ Oid typReceive;
elog(ERROR, "Binary BIND not implemented yet"); Oid typElem;
/* Call the parameter type's binary input converter */
getTypeBinaryInputInfo(ptype, &typReceive, &typElem);
params[i].value =
OidFunctionCall2(typReceive,
PointerGetDatum(&pbuf),
ObjectIdGetDatum(typElem));
/* Trouble if it didn't eat the whole buffer */
if (pbuf.cursor != pbuf.len)
elog(ERROR, "Improper binary format in BIND parameter %d",
i + 1);
} }
else else
{ {
@ -2511,7 +2524,7 @@ PostgresMain(int argc, char *argv[], const char *username)
if (!IsUnderPostmaster) if (!IsUnderPostmaster)
{ {
puts("\nPOSTGRES backend interactive interface "); puts("\nPOSTGRES backend interactive interface ");
puts("$Revision: 1.341 $ $Date: 2003/05/09 15:57:24 $\n"); puts("$Revision: 1.342 $ $Date: 2003/05/09 18:08:48 $\n");
} }
/* /*
@ -2771,6 +2784,9 @@ PostgresMain(int argc, char *argv[], const char *username)
/* start an xact for this function invocation */ /* start an xact for this function invocation */
start_xact_command(); start_xact_command();
/* switch back to message context */
MemoryContextSwitchTo(MessageContext);
if (HandleFunctionRequest(input_message) == EOF) if (HandleFunctionRequest(input_message) == EOF)
{ {
/* lost frontend connection during F message input */ /* lost frontend connection during F message input */

View File

@ -7,7 +7,7 @@
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/cache/lsyscache.c,v 1.92 2003/04/08 23:20:02 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/utils/cache/lsyscache.c,v 1.93 2003/05/09 18:08:48 tgl Exp $
* *
* NOTES * NOTES
* Eventually, the index information should go through here, too. * Eventually, the index information should go through here, too.
@ -1361,11 +1361,15 @@ getTypeInputInfo(Oid type, Oid *typInput, Oid *typElem)
ObjectIdGetDatum(type), ObjectIdGetDatum(type),
0, 0, 0); 0, 0, 0);
if (!HeapTupleIsValid(typeTuple)) if (!HeapTupleIsValid(typeTuple))
elog(ERROR, "getTypeInputInfo: Cache lookup of type %u failed", type); elog(ERROR, "Cache lookup of type %u failed", type);
pt = (Form_pg_type) GETSTRUCT(typeTuple); pt = (Form_pg_type) GETSTRUCT(typeTuple);
if (!pt->typisdefined) if (!pt->typisdefined)
elog(ERROR, "Type \"%s\" is only a shell", NameStr(pt->typname)); elog(ERROR, "Type %s is only a shell",
format_type_be(type));
if (!OidIsValid(pt->typinput))
elog(ERROR, "No input function available for type %s",
format_type_be(type));
*typInput = pt->typinput; *typInput = pt->typinput;
*typElem = pt->typelem; *typElem = pt->typelem;
@ -1377,10 +1381,8 @@ getTypeInputInfo(Oid type, Oid *typInput, Oid *typElem)
* getTypeOutputInfo * getTypeOutputInfo
* *
* Get info needed for printing values of a type * Get info needed for printing values of a type
*
* Returns true if data valid (a false result probably means it's a shell type)
*/ */
bool void
getTypeOutputInfo(Oid type, Oid *typOutput, Oid *typElem, getTypeOutputInfo(Oid type, Oid *typOutput, Oid *typElem,
bool *typIsVarlena) bool *typIsVarlena)
{ {
@ -1391,14 +1393,85 @@ getTypeOutputInfo(Oid type, Oid *typOutput, Oid *typElem,
ObjectIdGetDatum(type), ObjectIdGetDatum(type),
0, 0, 0); 0, 0, 0);
if (!HeapTupleIsValid(typeTuple)) if (!HeapTupleIsValid(typeTuple))
elog(ERROR, "getTypeOutputInfo: Cache lookup of type %u failed", type); elog(ERROR, "Cache lookup of type %u failed", type);
pt = (Form_pg_type) GETSTRUCT(typeTuple); pt = (Form_pg_type) GETSTRUCT(typeTuple);
if (!pt->typisdefined)
elog(ERROR, "Type %s is only a shell",
format_type_be(type));
if (!OidIsValid(pt->typoutput))
elog(ERROR, "No output function available for type %s",
format_type_be(type));
*typOutput = pt->typoutput; *typOutput = pt->typoutput;
*typElem = pt->typelem; *typElem = pt->typelem;
*typIsVarlena = (!pt->typbyval) && (pt->typlen == -1); *typIsVarlena = (!pt->typbyval) && (pt->typlen == -1);
ReleaseSysCache(typeTuple);
}
/*
* getTypeBinaryInputInfo
*
* Get info needed for binary input of values of a type
*/
void
getTypeBinaryInputInfo(Oid type, Oid *typReceive, Oid *typElem)
{
HeapTuple typeTuple;
Form_pg_type pt;
typeTuple = SearchSysCache(TYPEOID,
ObjectIdGetDatum(type),
0, 0, 0);
if (!HeapTupleIsValid(typeTuple))
elog(ERROR, "Cache lookup of type %u failed", type);
pt = (Form_pg_type) GETSTRUCT(typeTuple);
if (!pt->typisdefined)
elog(ERROR, "Type %s is only a shell",
format_type_be(type));
if (!OidIsValid(pt->typreceive))
elog(ERROR, "No binary input function available for type %s",
format_type_be(type));
*typReceive = pt->typreceive;
*typElem = pt->typelem;
ReleaseSysCache(typeTuple);
}
/*
* getTypeBinaryOutputInfo
*
* Get info needed for binary output of values of a type
*/
void
getTypeBinaryOutputInfo(Oid type, Oid *typSend, Oid *typElem,
bool *typIsVarlena)
{
HeapTuple typeTuple;
Form_pg_type pt;
typeTuple = SearchSysCache(TYPEOID,
ObjectIdGetDatum(type),
0, 0, 0);
if (!HeapTupleIsValid(typeTuple))
elog(ERROR, "Cache lookup of type %u failed", type);
pt = (Form_pg_type) GETSTRUCT(typeTuple);
if (!pt->typisdefined)
elog(ERROR, "Type %s is only a shell",
format_type_be(type));
if (!OidIsValid(pt->typsend))
elog(ERROR, "No binary output function available for type %s",
format_type_be(type));
*typSend = pt->typsend;
*typElem = pt->typelem;
*typIsVarlena = (!pt->typbyval) && (pt->typlen == -1);
ReleaseSysCache(typeTuple); ReleaseSysCache(typeTuple);
return OidIsValid(*typOutput);
} }

View File

@ -6,7 +6,7 @@
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $Id: lsyscache.h,v 1.68 2003/04/08 23:20:04 tgl Exp $ * $Id: lsyscache.h,v 1.69 2003/05/09 18:08:48 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -61,7 +61,10 @@ extern Oid get_typ_typrelid(Oid typid);
extern Oid get_element_type(Oid typid); extern Oid get_element_type(Oid typid);
extern Oid get_array_type(Oid typid); extern Oid get_array_type(Oid typid);
extern void getTypeInputInfo(Oid type, Oid *typInput, Oid *typElem); extern void getTypeInputInfo(Oid type, Oid *typInput, Oid *typElem);
extern bool getTypeOutputInfo(Oid type, Oid *typOutput, Oid *typElem, extern void getTypeOutputInfo(Oid type, Oid *typOutput, Oid *typElem,
bool *typIsVarlena);
extern void getTypeBinaryInputInfo(Oid type, Oid *typReceive, Oid *typElem);
extern void getTypeBinaryOutputInfo(Oid type, Oid *typSend, Oid *typElem,
bool *typIsVarlena); bool *typIsVarlena);
extern Oid getBaseType(Oid typid); extern Oid getBaseType(Oid typid);
extern int32 get_typavgwidth(Oid typid, int32 typmod); extern int32 get_typavgwidth(Oid typid, int32 typmod);