Commit 0ac6298b authored by Tom Lane's avatar Tom Lane

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).
parent d85a0a6b
......@@ -9,7 +9,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* 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
*
* NOTE: finfo is the lookup info for either typoutput or typsend, whichever
* we are using for this column.
* ----------------
*/
typedef struct
{ /* 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 */
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;
typedef struct
......@@ -219,9 +224,13 @@ SendRowDescriptionMessage(TupleDesc typeinfo, List *targetlist, int16 *formats)
pq_endmessage(&buf);
}
/*
* Get the lookup info that printtup() needs
*/
static void
printtup_prepare_info(DR_printtup *myState, TupleDesc typeinfo, int numAttrs)
{
int16 *formats = myState->portal->formats;
int i;
if (myState->myinfo)
......@@ -232,16 +241,32 @@ printtup_prepare_info(DR_printtup *myState, TupleDesc typeinfo, int numAttrs)
if (numAttrs <= 0)
return;
myState->myinfo = (PrinttupAttrInfo *)
palloc(numAttrs * sizeof(PrinttupAttrInfo));
palloc0(numAttrs * sizeof(PrinttupAttrInfo));
for (i = 0; i < numAttrs; i++)
{
PrinttupAttrInfo *thisState = myState->myinfo + i;
int16 format = (formats ? formats[i] : 0);
if (getTypeOutputInfo(typeinfo->attrs[i]->atttypid,
&thisState->typoutput, &thisState->typelem,
&thisState->typisvarlena))
thisState->format = format;
if (format == 0)
{
getTypeOutputInfo(typeinfo->attrs[i]->atttypid,
&thisState->typoutput,
&thisState->typelem,
&thisState->typisvarlena);
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)
{
DR_printtup *myState = (DR_printtup *) self;
int16 *formats = myState->portal->formats;
StringInfoData buf;
int natts = tuple->t_data->t_natts;
int i;
......@@ -274,11 +298,9 @@ printtup(HeapTuple tuple, TupleDesc typeinfo, DestReceiver *self)
for (i = 0; i < natts; ++i)
{
PrinttupAttrInfo *thisState = myState->myinfo + i;
int16 format = (formats ? formats[i] : 0);
Datum origattr,
attr;
bool isnull;
char *outputstr;
origattr = heap_getattr(tuple, i + 1, typeinfo, &isnull);
if (isnull)
......@@ -286,10 +308,7 @@ printtup(HeapTuple tuple, TupleDesc typeinfo, DestReceiver *self)
pq_sendint(&buf, -1, 4);
continue;
}
if (format == 0)
{
if (OidIsValid(thisState->typoutput))
{
/*
* If we have a toasted datum, forcibly detoast it here to
* avoid memory leakage inside the type's output routine.
......@@ -299,33 +318,36 @@ printtup(HeapTuple tuple, TupleDesc typeinfo, DestReceiver *self)
else
attr = origattr;
if (thisState->format == 0)
{
/* Text output */
char *outputstr;
outputstr = DatumGetCString(FunctionCall3(&thisState->finfo,
attr,
ObjectIdGetDatum(thisState->typelem),
Int32GetDatum(typeinfo->attrs[i]->atttypmod)));
pq_sendcountedtext(&buf, outputstr, strlen(outputstr), false);
/* Clean up detoasted copy, if any */
if (attr != origattr)
pfree(DatumGetPointer(attr));
pfree(outputstr);
}
else
{
outputstr = "<unprintable>";
pq_sendcountedtext(&buf, outputstr, strlen(outputstr), false);
}
}
else if (format == 1)
{
/* XXX something similar to above */
elog(ERROR, "Binary transmission not implemented yet");
}
else
{
elog(ERROR, "Invalid format code %d", format);
/* Binary output */
bytea *outputbytes;
outputbytes = DatumGetByteaP(FunctionCall2(&thisState->finfo,
attr,
ObjectIdGetDatum(thisState->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);
}
/* Clean up detoasted copy, if any */
if (attr != origattr)
pfree(DatumGetPointer(attr));
}
pq_endmessage(&buf);
......@@ -388,8 +410,9 @@ printtup_20(HeapTuple tuple, TupleDesc typeinfo, DestReceiver *self)
origattr = heap_getattr(tuple, i + 1, typeinfo, &isnull);
if (isnull)
continue;
if (OidIsValid(thisState->typoutput))
{
Assert(thisState->format == 0);
/*
* If we have a toasted datum, forcibly detoast it here to
* avoid memory leakage inside the type's output routine.
......@@ -403,19 +426,12 @@ printtup_20(HeapTuple tuple, TupleDesc typeinfo, DestReceiver *self)
attr,
ObjectIdGetDatum(thisState->typelem),
Int32GetDatum(typeinfo->attrs[i]->atttypmod)));
pq_sendcountedtext(&buf, outputstr, strlen(outputstr), true);
pfree(outputstr);
/* Clean up detoasted copy, if any */
if (attr != origattr)
pfree(DatumGetPointer(attr));
pfree(outputstr);
}
else
{
outputstr = "<unprintable>";
pq_sendcountedtext(&buf, outputstr, strlen(outputstr), true);
}
}
pq_endmessage(&buf);
......@@ -508,9 +524,8 @@ debugtup(HeapTuple tuple, TupleDesc typeinfo, DestReceiver *self)
origattr = heap_getattr(tuple, i + 1, typeinfo, &isnull);
if (isnull)
continue;
if (getTypeOutputInfo(typeinfo->attrs[i]->atttypid,
&typoutput, &typelem, &typisvarlena))
{
getTypeOutputInfo(typeinfo->attrs[i]->atttypid,
&typoutput, &typelem, &typisvarlena);
/*
* If we have a toasted datum, forcibly detoast it here to
* 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);
pfree(value);
/* Clean up detoasted copy, if any */
if (attr != origattr)
pfree(DatumGetPointer(attr));
pfree(value);
}
}
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
* 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
......@@ -587,84 +602,42 @@ printtup_internal_20(HeapTuple tuple, TupleDesc typeinfo, DestReceiver *self)
/*
* 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)
{
PrinttupAttrInfo *thisState = myState->myinfo + i;
Datum origattr,
attr;
bool isnull;
int32 len;
bytea *outputbytes;
origattr = heap_getattr(tuple, i + 1, typeinfo, &isnull);
if (isnull)
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));
else
attr = origattr;
len = VARSIZE(attr) - VARHDRSZ;
pq_sendint(&buf, len, VARHDRSZ);
pq_sendbytes(&buf, VARDATA(attr), len);
#ifdef IPORTAL_DEBUG
{
char *d = VARDATA(attr);
fprintf(stderr, "length %d data %x %x %x %x\n",
len, *d, *(d + 1), *(d + 2), *(d + 3));
}
#endif
outputbytes = DatumGetByteaP(FunctionCall2(&thisState->finfo,
attr,
ObjectIdGetDatum(thisState->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);
/* Clean up detoasted copy, if any */
if (attr != origattr)
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);
}
......@@ -8,7 +8,7 @@
*
*
* 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);
Oid out_func_oid;
if (!getTypeOutputInfo(attr[attnum - 1]->atttypid,
getTypeOutputInfo(attr[attnum - 1]->atttypid,
&out_func_oid, &elements[attnum - 1],
&isvarlena[attnum - 1]))
elog(ERROR, "COPY: couldn't lookup info for type %u",
attr[attnum - 1]->atttypid);
&isvarlena[attnum - 1]);
fmgr_info(out_func_oid, &out_functions[attnum - 1]);
if (binary && attr[attnum - 1]->attlen == -2)
elog(ERROR, "COPY BINARY: cstring not supported");
......
......@@ -8,7 +8,7 @@
*
*
* 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;
}
if (!getTypeOutputInfo(typoid, &foutoid, &typelem, &typisvarlena))
{
SPI_result = SPI_ERROR_NOOUTFUNC;
return NULL;
}
getTypeOutputInfo(typoid, &foutoid, &typelem, &typisvarlena);
/*
* If we have a toasted datum, forcibly detoast it here to avoid
......
This diff is collapsed.
......@@ -8,7 +8,7 @@
*
*
* 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
* this is the "main" module of the postgres backend and
......@@ -1342,8 +1342,21 @@ exec_bind_message(StringInfo input_message)
}
else if (pformat == 1)
{
/* XXX something similar to above */
elog(ERROR, "Binary BIND not implemented yet");
Oid typReceive;
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
{
......@@ -2511,7 +2524,7 @@ PostgresMain(int argc, char *argv[], const char *username)
if (!IsUnderPostmaster)
{
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_xact_command();
/* switch back to message context */
MemoryContextSwitchTo(MessageContext);
if (HandleFunctionRequest(input_message) == EOF)
{
/* lost frontend connection during F message input */
......
......@@ -7,7 +7,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* 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
* Eventually, the index information should go through here, too.
......@@ -1361,11 +1361,15 @@ getTypeInputInfo(Oid type, Oid *typInput, Oid *typElem)
ObjectIdGetDatum(type),
0, 0, 0);
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);
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;
*typElem = pt->typelem;
......@@ -1377,10 +1381,8 @@ getTypeInputInfo(Oid type, Oid *typInput, Oid *typElem)
* getTypeOutputInfo
*
* 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,
bool *typIsVarlena)
{
......@@ -1391,14 +1393,85 @@ getTypeOutputInfo(Oid type, Oid *typOutput, Oid *typElem,
ObjectIdGetDatum(type),
0, 0, 0);
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);
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;
*typElem = pt->typelem;
*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);
return OidIsValid(*typOutput);
}
......
......@@ -6,7 +6,7 @@
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* 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_array_type(Oid typid);
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);
extern Oid getBaseType(Oid typid);
extern int32 get_typavgwidth(Oid typid, int32 typmod);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment