1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* printtup.c
|
2002-02-27 20:36:13 +01:00
|
|
|
* Routines to print out tuples to the destination (both frontend
|
2003-05-06 22:26:28 +02:00
|
|
|
* clients and standalone backends are supported here).
|
2002-02-27 20:36:13 +01:00
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2014-01-07 22:05:30 +01:00
|
|
|
* Portions Copyright (c) 1996-2014, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/backend/access/common/printtup.c
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
1999-04-25 05:19:27 +02:00
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include "access/printtup.h"
|
2002-02-27 20:36:13 +01:00
|
|
|
#include "libpq/libpq.h"
|
1999-04-25 05:19:27 +02:00
|
|
|
#include "libpq/pqformat.h"
|
2005-06-22 19:45:46 +02:00
|
|
|
#include "tcop/pquery.h"
|
2002-08-22 02:01:51 +02:00
|
|
|
#include "utils/lsyscache.h"
|
2013-06-27 02:00:08 +02:00
|
|
|
#include "utils/memdebug.h"
|
Prevent memory leaks from accumulating across printtup() calls.
Historically, printtup() has assumed that it could prevent memory leakage
by pfree'ing the string result of each output function and manually
managing detoasting of toasted values. This amounts to assuming that
datatype output functions never leak any memory internally; an assumption
we've already decided to be bogus elsewhere, for example in COPY OUT.
range_out in particular is known to leak multiple kilobytes per call, as
noted in bug #8573 from Godfried Vanluffelen. While we could go in and fix
that leak, it wouldn't be very notationally convenient, and in any case
there have been and undoubtedly will again be other leaks in other output
functions. So what seems like the best solution is to run the output
functions in a temporary memory context that can be reset after each row,
as we're doing in COPY OUT. Some quick experimentation suggests this is
actually a tad faster than the retail pfree's anyway.
This patch fixes all the variants of printtup, except for debugtup()
which is used in standalone mode. It doesn't seem worth worrying
about query-lifespan leaks in standalone mode, and fixing that case
would be a bit tedious since debugtup() doesn't currently have any
startup or shutdown functions.
While at it, remove manual detoast management from several other
output-function call sites that had copied it from printtup(). This
doesn't make a lot of difference right now, but in view of recent
discussions about supporting "non-flattened" Datums, we're going to
want that code gone eventually anyway.
Back-patch to 9.2 where range_out was introduced. We might eventually
decide to back-patch this further, but in the absence of known major
leaks in older output functions, I'll refrain for now.
2013-11-03 17:33:05 +01:00
|
|
|
#include "utils/memutils.h"
|
2002-08-22 02:01:51 +02:00
|
|
|
|
1996-10-21 00:04:49 +02:00
|
|
|
|
2003-05-06 22:26:28 +02:00
|
|
|
static void printtup_startup(DestReceiver *self, int operation,
|
2003-08-04 02:43:34 +02:00
|
|
|
TupleDesc typeinfo);
|
2005-03-16 22:38:10 +01:00
|
|
|
static void printtup(TupleTableSlot *slot, DestReceiver *self);
|
|
|
|
static void printtup_20(TupleTableSlot *slot, DestReceiver *self);
|
|
|
|
static void printtup_internal_20(TupleTableSlot *slot, DestReceiver *self);
|
2003-05-06 22:26:28 +02:00
|
|
|
static void printtup_shutdown(DestReceiver *self);
|
|
|
|
static void printtup_destroy(DestReceiver *self);
|
|
|
|
|
1999-01-27 01:36:28 +01:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/* ----------------------------------------------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* printtup / debugtup support
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
1999-01-27 01:36:28 +01:00
|
|
|
/* ----------------
|
|
|
|
* Private state for a printtup destination object
|
2003-05-09 20:08:48 +02:00
|
|
|
*
|
|
|
|
* NOTE: finfo is the lookup info for either typoutput or typsend, whichever
|
|
|
|
* we are using for this column.
|
1999-01-27 01:36:28 +01:00
|
|
|
* ----------------
|
|
|
|
*/
|
1999-05-25 18:15:34 +02:00
|
|
|
typedef struct
|
|
|
|
{ /* Per-attribute information */
|
2003-05-09 20:08:48 +02:00
|
|
|
Oid typoutput; /* Oid for the type's text output fn */
|
|
|
|
Oid typsend; /* Oid for the type's binary output fn */
|
2000-12-01 23:10:31 +01:00
|
|
|
bool typisvarlena; /* is it varlena (ie possibly toastable)? */
|
2003-05-09 20:08:48 +02:00
|
|
|
int16 format; /* format code for this column */
|
|
|
|
FmgrInfo finfo; /* Precomputed call info for output fn */
|
1999-05-26 00:43:53 +02:00
|
|
|
} PrinttupAttrInfo;
|
1999-01-27 01:36:28 +01:00
|
|
|
|
1999-05-25 18:15:34 +02:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
DestReceiver pub; /* publicly-known function pointers */
|
2003-05-08 20:16:37 +02:00
|
|
|
Portal portal; /* the Portal we are printing from */
|
2003-05-05 02:44:56 +02:00
|
|
|
bool sendDescrip; /* send RowDescription at startup? */
|
1999-05-25 18:15:34 +02:00
|
|
|
TupleDesc attrinfo; /* The attr info we are set up for */
|
|
|
|
int nattrs;
|
|
|
|
PrinttupAttrInfo *myinfo; /* Cached info about each attr */
|
Prevent memory leaks from accumulating across printtup() calls.
Historically, printtup() has assumed that it could prevent memory leakage
by pfree'ing the string result of each output function and manually
managing detoasting of toasted values. This amounts to assuming that
datatype output functions never leak any memory internally; an assumption
we've already decided to be bogus elsewhere, for example in COPY OUT.
range_out in particular is known to leak multiple kilobytes per call, as
noted in bug #8573 from Godfried Vanluffelen. While we could go in and fix
that leak, it wouldn't be very notationally convenient, and in any case
there have been and undoubtedly will again be other leaks in other output
functions. So what seems like the best solution is to run the output
functions in a temporary memory context that can be reset after each row,
as we're doing in COPY OUT. Some quick experimentation suggests this is
actually a tad faster than the retail pfree's anyway.
This patch fixes all the variants of printtup, except for debugtup()
which is used in standalone mode. It doesn't seem worth worrying
about query-lifespan leaks in standalone mode, and fixing that case
would be a bit tedious since debugtup() doesn't currently have any
startup or shutdown functions.
While at it, remove manual detoast management from several other
output-function call sites that had copied it from printtup(). This
doesn't make a lot of difference right now, but in view of recent
discussions about supporting "non-flattened" Datums, we're going to
want that code gone eventually anyway.
Back-patch to 9.2 where range_out was introduced. We might eventually
decide to back-patch this further, but in the absence of known major
leaks in older output functions, I'll refrain for now.
2013-11-03 17:33:05 +01:00
|
|
|
MemoryContext tmpcontext; /* Memory context for per-row workspace */
|
1999-05-26 00:43:53 +02:00
|
|
|
} DR_printtup;
|
1999-01-27 01:36:28 +01:00
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* Initialize: create a DestReceiver for printtup
|
|
|
|
* ----------------
|
|
|
|
*/
|
1999-05-25 18:15:34 +02:00
|
|
|
DestReceiver *
|
2008-11-30 21:51:25 +01:00
|
|
|
printtup_create_DR(CommandDest dest)
|
1999-01-27 01:36:28 +01:00
|
|
|
{
|
2008-11-30 21:51:25 +01:00
|
|
|
DR_printtup *self = (DR_printtup *) palloc0(sizeof(DR_printtup));
|
2003-05-06 22:26:28 +02:00
|
|
|
|
2009-06-11 16:49:15 +02:00
|
|
|
self->pub.receiveSlot = printtup; /* might get changed later */
|
2003-08-06 19:46:46 +02:00
|
|
|
self->pub.rStartup = printtup_startup;
|
|
|
|
self->pub.rShutdown = printtup_shutdown;
|
|
|
|
self->pub.rDestroy = printtup_destroy;
|
2003-05-06 22:26:28 +02:00
|
|
|
self->pub.mydest = dest;
|
1999-01-27 01:36:28 +01:00
|
|
|
|
2005-11-03 18:11:40 +01:00
|
|
|
/*
|
|
|
|
* Send T message automatically if DestRemote, but not if
|
|
|
|
* DestRemoteExecute
|
|
|
|
*/
|
|
|
|
self->sendDescrip = (dest == DestRemote);
|
2003-05-05 02:44:56 +02:00
|
|
|
|
1999-01-27 01:36:28 +01:00
|
|
|
self->attrinfo = NULL;
|
|
|
|
self->nattrs = 0;
|
|
|
|
self->myinfo = NULL;
|
Prevent memory leaks from accumulating across printtup() calls.
Historically, printtup() has assumed that it could prevent memory leakage
by pfree'ing the string result of each output function and manually
managing detoasting of toasted values. This amounts to assuming that
datatype output functions never leak any memory internally; an assumption
we've already decided to be bogus elsewhere, for example in COPY OUT.
range_out in particular is known to leak multiple kilobytes per call, as
noted in bug #8573 from Godfried Vanluffelen. While we could go in and fix
that leak, it wouldn't be very notationally convenient, and in any case
there have been and undoubtedly will again be other leaks in other output
functions. So what seems like the best solution is to run the output
functions in a temporary memory context that can be reset after each row,
as we're doing in COPY OUT. Some quick experimentation suggests this is
actually a tad faster than the retail pfree's anyway.
This patch fixes all the variants of printtup, except for debugtup()
which is used in standalone mode. It doesn't seem worth worrying
about query-lifespan leaks in standalone mode, and fixing that case
would be a bit tedious since debugtup() doesn't currently have any
startup or shutdown functions.
While at it, remove manual detoast management from several other
output-function call sites that had copied it from printtup(). This
doesn't make a lot of difference right now, but in view of recent
discussions about supporting "non-flattened" Datums, we're going to
want that code gone eventually anyway.
Back-patch to 9.2 where range_out was introduced. We might eventually
decide to back-patch this further, but in the absence of known major
leaks in older output functions, I'll refrain for now.
2013-11-03 17:33:05 +01:00
|
|
|
self->tmpcontext = NULL;
|
1999-01-27 01:36:28 +01:00
|
|
|
|
1999-05-25 18:15:34 +02:00
|
|
|
return (DestReceiver *) self;
|
1999-01-27 01:36:28 +01:00
|
|
|
}
|
|
|
|
|
2008-11-30 21:51:25 +01:00
|
|
|
/*
|
|
|
|
* Set parameters for a DestRemote (or DestRemoteExecute) receiver
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
SetRemoteDestReceiverParams(DestReceiver *self, Portal portal)
|
|
|
|
{
|
|
|
|
DR_printtup *myState = (DR_printtup *) self;
|
|
|
|
|
|
|
|
Assert(myState->pub.mydest == DestRemote ||
|
|
|
|
myState->pub.mydest == DestRemoteExecute);
|
|
|
|
|
|
|
|
myState->portal = portal;
|
|
|
|
|
|
|
|
if (PG_PROTOCOL_MAJOR(FrontendProtocol) < 3)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* In protocol 2.0 the Bind message does not exist, so there is no way
|
|
|
|
* for the columns to have different print formats; it's sufficient to
|
|
|
|
* look at the first one.
|
|
|
|
*/
|
|
|
|
if (portal->formats && portal->formats[0] != 0)
|
|
|
|
myState->pub.receiveSlot = printtup_internal_20;
|
|
|
|
else
|
|
|
|
myState->pub.receiveSlot = printtup_20;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-01-27 01:36:28 +01:00
|
|
|
static void
|
2003-05-08 20:16:37 +02:00
|
|
|
printtup_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
|
1999-01-27 01:36:28 +01:00
|
|
|
{
|
2003-05-05 02:44:56 +02:00
|
|
|
DR_printtup *myState = (DR_printtup *) self;
|
2003-08-04 02:43:34 +02:00
|
|
|
Portal portal = myState->portal;
|
2003-05-05 02:44:56 +02:00
|
|
|
|
Prevent memory leaks from accumulating across printtup() calls.
Historically, printtup() has assumed that it could prevent memory leakage
by pfree'ing the string result of each output function and manually
managing detoasting of toasted values. This amounts to assuming that
datatype output functions never leak any memory internally; an assumption
we've already decided to be bogus elsewhere, for example in COPY OUT.
range_out in particular is known to leak multiple kilobytes per call, as
noted in bug #8573 from Godfried Vanluffelen. While we could go in and fix
that leak, it wouldn't be very notationally convenient, and in any case
there have been and undoubtedly will again be other leaks in other output
functions. So what seems like the best solution is to run the output
functions in a temporary memory context that can be reset after each row,
as we're doing in COPY OUT. Some quick experimentation suggests this is
actually a tad faster than the retail pfree's anyway.
This patch fixes all the variants of printtup, except for debugtup()
which is used in standalone mode. It doesn't seem worth worrying
about query-lifespan leaks in standalone mode, and fixing that case
would be a bit tedious since debugtup() doesn't currently have any
startup or shutdown functions.
While at it, remove manual detoast management from several other
output-function call sites that had copied it from printtup(). This
doesn't make a lot of difference right now, but in view of recent
discussions about supporting "non-flattened" Datums, we're going to
want that code gone eventually anyway.
Back-patch to 9.2 where range_out was introduced. We might eventually
decide to back-patch this further, but in the absence of known major
leaks in older output functions, I'll refrain for now.
2013-11-03 17:33:05 +01:00
|
|
|
/*
|
|
|
|
* Create a temporary memory context that we can reset once per row to
|
|
|
|
* recover palloc'd memory. This avoids any problems with leaks inside
|
|
|
|
* datatype output routines, and should be faster than retail pfree's
|
|
|
|
* anyway.
|
|
|
|
*/
|
|
|
|
myState->tmpcontext = AllocSetContextCreate(CurrentMemoryContext,
|
|
|
|
"printtup",
|
|
|
|
ALLOCSET_DEFAULT_MINSIZE,
|
|
|
|
ALLOCSET_DEFAULT_INITSIZE,
|
|
|
|
ALLOCSET_DEFAULT_MAXSIZE);
|
|
|
|
|
2003-04-22 02:08:07 +02:00
|
|
|
if (PG_PROTOCOL_MAJOR(FrontendProtocol) < 3)
|
|
|
|
{
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Send portal name to frontend (obsolete cruft, gone in proto 3.0)
|
2003-04-22 02:08:07 +02:00
|
|
|
*
|
|
|
|
* If portal name not specified, use "blank" portal.
|
|
|
|
*/
|
2003-05-08 20:16:37 +02:00
|
|
|
const char *portalName = portal->name;
|
|
|
|
|
|
|
|
if (portalName == NULL || portalName[0] == '\0')
|
2003-04-22 02:08:07 +02:00
|
|
|
portalName = "blank";
|
|
|
|
|
|
|
|
pq_puttextmessage('P', portalName);
|
|
|
|
}
|
2002-02-27 20:36:13 +01:00
|
|
|
|
|
|
|
/*
|
2006-10-04 02:30:14 +02:00
|
|
|
* If we are supposed to emit row descriptions, then send the tuple
|
|
|
|
* descriptor of the tuples.
|
2002-02-27 20:36:13 +01:00
|
|
|
*/
|
2006-08-12 04:52:06 +02:00
|
|
|
if (myState->sendDescrip)
|
2005-06-22 19:45:46 +02:00
|
|
|
SendRowDescriptionMessage(typeinfo,
|
|
|
|
FetchPortalTargetList(portal),
|
|
|
|
portal->formats);
|
2002-02-27 20:36:13 +01:00
|
|
|
|
1999-01-27 01:36:28 +01:00
|
|
|
/* ----------------
|
|
|
|
* We could set up the derived attr info at this time, but we postpone it
|
2002-02-27 20:36:13 +01:00
|
|
|
* until the first call of printtup, for 2 reasons:
|
1999-01-27 01:36:28 +01:00
|
|
|
* 1. We don't waste time (compared to the old way) if there are no
|
1999-05-25 18:15:34 +02:00
|
|
|
* tuples at all to output.
|
1999-01-27 01:36:28 +01:00
|
|
|
* 2. Checking in printtup allows us to handle the case that the tuples
|
1999-05-25 18:15:34 +02:00
|
|
|
* change type midway through (although this probably can't happen in
|
|
|
|
* the current executor).
|
1999-01-27 01:36:28 +01:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
2003-05-05 02:44:56 +02:00
|
|
|
/*
|
|
|
|
* SendRowDescriptionMessage --- send a RowDescription message to the frontend
|
2003-05-06 02:20:33 +02:00
|
|
|
*
|
|
|
|
* Notes: the TupleDesc has typically been manufactured by ExecTypeFromTL()
|
|
|
|
* or some similar function; it does not contain a full set of fields.
|
|
|
|
* The targetlist will be NIL when executing a utility function that does
|
2003-05-08 20:16:37 +02:00
|
|
|
* not have a plan. If the targetlist isn't NIL then it is a Query node's
|
2003-08-04 02:43:34 +02:00
|
|
|
* targetlist; it is up to us to ignore resjunk columns in it. The formats[]
|
2003-05-08 20:16:37 +02:00
|
|
|
* array pointer might be NULL (if we are doing Describe on a prepared stmt);
|
|
|
|
* send zeroes for the format codes in that case.
|
2003-05-05 02:44:56 +02:00
|
|
|
*/
|
|
|
|
void
|
2003-05-08 20:16:37 +02:00
|
|
|
SendRowDescriptionMessage(TupleDesc typeinfo, List *targetlist, int16 *formats)
|
2003-05-05 02:44:56 +02:00
|
|
|
{
|
|
|
|
Form_pg_attribute *attrs = typeinfo->attrs;
|
|
|
|
int natts = typeinfo->natts;
|
|
|
|
int proto = PG_PROTOCOL_MAJOR(FrontendProtocol);
|
|
|
|
int i;
|
|
|
|
StringInfoData buf;
|
2004-05-26 06:41:50 +02:00
|
|
|
ListCell *tlist_item = list_head(targetlist);
|
2003-05-05 02:44:56 +02:00
|
|
|
|
2003-08-04 02:43:34 +02:00
|
|
|
pq_beginmessage(&buf, 'T'); /* tuple descriptor message type */
|
|
|
|
pq_sendint(&buf, natts, 2); /* # of attrs in tuples */
|
2003-05-05 02:44:56 +02:00
|
|
|
|
|
|
|
for (i = 0; i < natts; ++i)
|
|
|
|
{
|
2003-08-04 02:43:34 +02:00
|
|
|
Oid atttypid = attrs[i]->atttypid;
|
|
|
|
int32 atttypmod = attrs[i]->atttypmod;
|
2003-05-13 20:39:50 +02:00
|
|
|
|
2003-05-05 02:44:56 +02:00
|
|
|
pq_sendstring(&buf, NameStr(attrs[i]->attname));
|
|
|
|
/* column ID info appears in protocol 3.0 and up */
|
|
|
|
if (proto >= 3)
|
|
|
|
{
|
2003-05-06 02:20:33 +02:00
|
|
|
/* Do we have a non-resjunk tlist item? */
|
2004-05-26 06:41:50 +02:00
|
|
|
while (tlist_item &&
|
2005-04-06 18:34:07 +02:00
|
|
|
((TargetEntry *) lfirst(tlist_item))->resjunk)
|
2004-05-26 06:41:50 +02:00
|
|
|
tlist_item = lnext(tlist_item);
|
|
|
|
if (tlist_item)
|
2003-05-06 02:20:33 +02:00
|
|
|
{
|
2005-04-06 18:34:07 +02:00
|
|
|
TargetEntry *tle = (TargetEntry *) lfirst(tlist_item);
|
2003-05-06 02:20:33 +02:00
|
|
|
|
2005-04-06 18:34:07 +02:00
|
|
|
pq_sendint(&buf, tle->resorigtbl, 4);
|
|
|
|
pq_sendint(&buf, tle->resorigcol, 2);
|
2004-05-26 06:41:50 +02:00
|
|
|
tlist_item = lnext(tlist_item);
|
2003-05-06 02:20:33 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No info available, so send zeroes */
|
|
|
|
pq_sendint(&buf, 0, 4);
|
|
|
|
pq_sendint(&buf, 0, 2);
|
|
|
|
}
|
2003-05-05 02:44:56 +02:00
|
|
|
}
|
2003-05-13 20:39:50 +02:00
|
|
|
/* If column is a domain, send the base type and typmod instead */
|
2006-04-06 00:11:58 +02:00
|
|
|
atttypid = getBaseTypeAndTypmod(atttypid, &atttypmod);
|
2003-05-13 20:39:50 +02:00
|
|
|
pq_sendint(&buf, (int) atttypid, sizeof(atttypid));
|
|
|
|
pq_sendint(&buf, attrs[i]->attlen, sizeof(attrs[i]->attlen));
|
2003-05-05 02:44:56 +02:00
|
|
|
/* typmod appears in protocol 2.0 and up */
|
|
|
|
if (proto >= 2)
|
2003-05-13 20:39:50 +02:00
|
|
|
pq_sendint(&buf, atttypmod, sizeof(atttypmod));
|
2003-05-08 20:16:37 +02:00
|
|
|
/* format info appears in protocol 3.0 and up */
|
|
|
|
if (proto >= 3)
|
|
|
|
{
|
|
|
|
if (formats)
|
|
|
|
pq_sendint(&buf, formats[i], 2);
|
|
|
|
else
|
|
|
|
pq_sendint(&buf, 0, 2);
|
|
|
|
}
|
2003-05-05 02:44:56 +02:00
|
|
|
}
|
|
|
|
pq_endmessage(&buf);
|
|
|
|
}
|
|
|
|
|
2003-05-09 20:08:48 +02:00
|
|
|
/*
|
|
|
|
* Get the lookup info that printtup() needs
|
|
|
|
*/
|
1999-01-27 01:36:28 +01:00
|
|
|
static void
|
1999-05-26 00:43:53 +02:00
|
|
|
printtup_prepare_info(DR_printtup *myState, TupleDesc typeinfo, int numAttrs)
|
1999-01-27 01:36:28 +01:00
|
|
|
{
|
2003-05-09 20:08:48 +02:00
|
|
|
int16 *formats = myState->portal->formats;
|
1999-05-25 18:15:34 +02:00
|
|
|
int i;
|
1999-01-27 01:36:28 +01:00
|
|
|
|
2004-06-04 22:35:21 +02:00
|
|
|
/* get rid of any old data */
|
1999-01-27 01:36:28 +01:00
|
|
|
if (myState->myinfo)
|
2004-06-04 22:35:21 +02:00
|
|
|
pfree(myState->myinfo);
|
1999-01-27 01:36:28 +01:00
|
|
|
myState->myinfo = NULL;
|
2004-06-04 22:35:21 +02:00
|
|
|
|
1999-01-27 01:36:28 +01:00
|
|
|
myState->attrinfo = typeinfo;
|
|
|
|
myState->nattrs = numAttrs;
|
|
|
|
if (numAttrs <= 0)
|
|
|
|
return;
|
2004-06-04 22:35:21 +02:00
|
|
|
|
1999-05-25 18:15:34 +02:00
|
|
|
myState->myinfo = (PrinttupAttrInfo *)
|
2003-05-09 20:08:48 +02:00
|
|
|
palloc0(numAttrs * sizeof(PrinttupAttrInfo));
|
2004-06-04 22:35:21 +02:00
|
|
|
|
1999-01-27 01:36:28 +01:00
|
|
|
for (i = 0; i < numAttrs; i++)
|
|
|
|
{
|
1999-05-25 18:15:34 +02:00
|
|
|
PrinttupAttrInfo *thisState = myState->myinfo + i;
|
2003-05-09 20:08:48 +02:00
|
|
|
int16 format = (formats ? formats[i] : 0);
|
1999-05-25 18:15:34 +02:00
|
|
|
|
2003-05-09 20:08:48 +02:00
|
|
|
thisState->format = format;
|
|
|
|
if (format == 0)
|
|
|
|
{
|
|
|
|
getTypeOutputInfo(typeinfo->attrs[i]->atttypid,
|
|
|
|
&thisState->typoutput,
|
|
|
|
&thisState->typisvarlena);
|
1999-01-27 02:11:43 +01:00
|
|
|
fmgr_info(thisState->typoutput, &thisState->finfo);
|
2003-05-09 20:08:48 +02:00
|
|
|
}
|
|
|
|
else if (format == 1)
|
|
|
|
{
|
|
|
|
getTypeBinaryOutputInfo(typeinfo->attrs[i]->atttypid,
|
|
|
|
&thisState->typsend,
|
|
|
|
&thisState->typisvarlena);
|
|
|
|
fmgr_info(thisState->typsend, &thisState->finfo);
|
|
|
|
}
|
|
|
|
else
|
2003-07-21 22:29:40 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("unsupported format code: %d", format)));
|
1999-01-27 01:36:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/* ----------------
|
2003-05-08 20:16:37 +02:00
|
|
|
* printtup --- print a tuple in protocol 3.0
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
1999-01-27 01:36:28 +01:00
|
|
|
static void
|
2005-03-16 22:38:10 +01:00
|
|
|
printtup(TupleTableSlot *slot, DestReceiver *self)
|
2003-05-08 20:16:37 +02:00
|
|
|
{
|
2005-10-15 04:49:52 +02:00
|
|
|
TupleDesc typeinfo = slot->tts_tupleDescriptor;
|
2003-05-08 20:16:37 +02:00
|
|
|
DR_printtup *myState = (DR_printtup *) self;
|
Prevent memory leaks from accumulating across printtup() calls.
Historically, printtup() has assumed that it could prevent memory leakage
by pfree'ing the string result of each output function and manually
managing detoasting of toasted values. This amounts to assuming that
datatype output functions never leak any memory internally; an assumption
we've already decided to be bogus elsewhere, for example in COPY OUT.
range_out in particular is known to leak multiple kilobytes per call, as
noted in bug #8573 from Godfried Vanluffelen. While we could go in and fix
that leak, it wouldn't be very notationally convenient, and in any case
there have been and undoubtedly will again be other leaks in other output
functions. So what seems like the best solution is to run the output
functions in a temporary memory context that can be reset after each row,
as we're doing in COPY OUT. Some quick experimentation suggests this is
actually a tad faster than the retail pfree's anyway.
This patch fixes all the variants of printtup, except for debugtup()
which is used in standalone mode. It doesn't seem worth worrying
about query-lifespan leaks in standalone mode, and fixing that case
would be a bit tedious since debugtup() doesn't currently have any
startup or shutdown functions.
While at it, remove manual detoast management from several other
output-function call sites that had copied it from printtup(). This
doesn't make a lot of difference right now, but in view of recent
discussions about supporting "non-flattened" Datums, we're going to
want that code gone eventually anyway.
Back-patch to 9.2 where range_out was introduced. We might eventually
decide to back-patch this further, but in the absence of known major
leaks in older output functions, I'll refrain for now.
2013-11-03 17:33:05 +01:00
|
|
|
MemoryContext oldcontext;
|
2003-05-08 20:16:37 +02:00
|
|
|
StringInfoData buf;
|
2003-05-26 19:51:38 +02:00
|
|
|
int natts = typeinfo->natts;
|
2003-05-08 20:16:37 +02:00
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Set or update my derived attribute info, if needed */
|
|
|
|
if (myState->attrinfo != typeinfo || myState->nattrs != natts)
|
|
|
|
printtup_prepare_info(myState, typeinfo, natts);
|
|
|
|
|
2005-03-16 22:38:10 +01:00
|
|
|
/* Make sure the tuple is fully deconstructed */
|
|
|
|
slot_getallattrs(slot);
|
2004-06-04 22:35:21 +02:00
|
|
|
|
Prevent memory leaks from accumulating across printtup() calls.
Historically, printtup() has assumed that it could prevent memory leakage
by pfree'ing the string result of each output function and manually
managing detoasting of toasted values. This amounts to assuming that
datatype output functions never leak any memory internally; an assumption
we've already decided to be bogus elsewhere, for example in COPY OUT.
range_out in particular is known to leak multiple kilobytes per call, as
noted in bug #8573 from Godfried Vanluffelen. While we could go in and fix
that leak, it wouldn't be very notationally convenient, and in any case
there have been and undoubtedly will again be other leaks in other output
functions. So what seems like the best solution is to run the output
functions in a temporary memory context that can be reset after each row,
as we're doing in COPY OUT. Some quick experimentation suggests this is
actually a tad faster than the retail pfree's anyway.
This patch fixes all the variants of printtup, except for debugtup()
which is used in standalone mode. It doesn't seem worth worrying
about query-lifespan leaks in standalone mode, and fixing that case
would be a bit tedious since debugtup() doesn't currently have any
startup or shutdown functions.
While at it, remove manual detoast management from several other
output-function call sites that had copied it from printtup(). This
doesn't make a lot of difference right now, but in view of recent
discussions about supporting "non-flattened" Datums, we're going to
want that code gone eventually anyway.
Back-patch to 9.2 where range_out was introduced. We might eventually
decide to back-patch this further, but in the absence of known major
leaks in older output functions, I'll refrain for now.
2013-11-03 17:33:05 +01:00
|
|
|
/* Switch into per-row context so we can recover memory below */
|
|
|
|
oldcontext = MemoryContextSwitchTo(myState->tmpcontext);
|
|
|
|
|
2003-05-08 20:16:37 +02:00
|
|
|
/*
|
Prevent memory leaks from accumulating across printtup() calls.
Historically, printtup() has assumed that it could prevent memory leakage
by pfree'ing the string result of each output function and manually
managing detoasting of toasted values. This amounts to assuming that
datatype output functions never leak any memory internally; an assumption
we've already decided to be bogus elsewhere, for example in COPY OUT.
range_out in particular is known to leak multiple kilobytes per call, as
noted in bug #8573 from Godfried Vanluffelen. While we could go in and fix
that leak, it wouldn't be very notationally convenient, and in any case
there have been and undoubtedly will again be other leaks in other output
functions. So what seems like the best solution is to run the output
functions in a temporary memory context that can be reset after each row,
as we're doing in COPY OUT. Some quick experimentation suggests this is
actually a tad faster than the retail pfree's anyway.
This patch fixes all the variants of printtup, except for debugtup()
which is used in standalone mode. It doesn't seem worth worrying
about query-lifespan leaks in standalone mode, and fixing that case
would be a bit tedious since debugtup() doesn't currently have any
startup or shutdown functions.
While at it, remove manual detoast management from several other
output-function call sites that had copied it from printtup(). This
doesn't make a lot of difference right now, but in view of recent
discussions about supporting "non-flattened" Datums, we're going to
want that code gone eventually anyway.
Back-patch to 9.2 where range_out was introduced. We might eventually
decide to back-patch this further, but in the absence of known major
leaks in older output functions, I'll refrain for now.
2013-11-03 17:33:05 +01:00
|
|
|
* Prepare a DataRow message (note buffer is in per-row context)
|
2003-05-08 20:16:37 +02:00
|
|
|
*/
|
|
|
|
pq_beginmessage(&buf, 'D');
|
|
|
|
|
|
|
|
pq_sendint(&buf, natts, 2);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* send the attributes of this tuple
|
|
|
|
*/
|
|
|
|
for (i = 0; i < natts; ++i)
|
|
|
|
{
|
|
|
|
PrinttupAttrInfo *thisState = myState->myinfo + i;
|
Prevent memory leaks from accumulating across printtup() calls.
Historically, printtup() has assumed that it could prevent memory leakage
by pfree'ing the string result of each output function and manually
managing detoasting of toasted values. This amounts to assuming that
datatype output functions never leak any memory internally; an assumption
we've already decided to be bogus elsewhere, for example in COPY OUT.
range_out in particular is known to leak multiple kilobytes per call, as
noted in bug #8573 from Godfried Vanluffelen. While we could go in and fix
that leak, it wouldn't be very notationally convenient, and in any case
there have been and undoubtedly will again be other leaks in other output
functions. So what seems like the best solution is to run the output
functions in a temporary memory context that can be reset after each row,
as we're doing in COPY OUT. Some quick experimentation suggests this is
actually a tad faster than the retail pfree's anyway.
This patch fixes all the variants of printtup, except for debugtup()
which is used in standalone mode. It doesn't seem worth worrying
about query-lifespan leaks in standalone mode, and fixing that case
would be a bit tedious since debugtup() doesn't currently have any
startup or shutdown functions.
While at it, remove manual detoast management from several other
output-function call sites that had copied it from printtup(). This
doesn't make a lot of difference right now, but in view of recent
discussions about supporting "non-flattened" Datums, we're going to
want that code gone eventually anyway.
Back-patch to 9.2 where range_out was introduced. We might eventually
decide to back-patch this further, but in the absence of known major
leaks in older output functions, I'll refrain for now.
2013-11-03 17:33:05 +01:00
|
|
|
Datum attr = slot->tts_values[i];
|
2003-05-08 20:16:37 +02:00
|
|
|
|
2005-03-16 22:38:10 +01:00
|
|
|
if (slot->tts_isnull[i])
|
2003-05-08 20:16:37 +02:00
|
|
|
{
|
|
|
|
pq_sendint(&buf, -1, 4);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2003-05-09 20:08:48 +02:00
|
|
|
/*
|
Prevent memory leaks from accumulating across printtup() calls.
Historically, printtup() has assumed that it could prevent memory leakage
by pfree'ing the string result of each output function and manually
managing detoasting of toasted values. This amounts to assuming that
datatype output functions never leak any memory internally; an assumption
we've already decided to be bogus elsewhere, for example in COPY OUT.
range_out in particular is known to leak multiple kilobytes per call, as
noted in bug #8573 from Godfried Vanluffelen. While we could go in and fix
that leak, it wouldn't be very notationally convenient, and in any case
there have been and undoubtedly will again be other leaks in other output
functions. So what seems like the best solution is to run the output
functions in a temporary memory context that can be reset after each row,
as we're doing in COPY OUT. Some quick experimentation suggests this is
actually a tad faster than the retail pfree's anyway.
This patch fixes all the variants of printtup, except for debugtup()
which is used in standalone mode. It doesn't seem worth worrying
about query-lifespan leaks in standalone mode, and fixing that case
would be a bit tedious since debugtup() doesn't currently have any
startup or shutdown functions.
While at it, remove manual detoast management from several other
output-function call sites that had copied it from printtup(). This
doesn't make a lot of difference right now, but in view of recent
discussions about supporting "non-flattened" Datums, we're going to
want that code gone eventually anyway.
Back-patch to 9.2 where range_out was introduced. We might eventually
decide to back-patch this further, but in the absence of known major
leaks in older output functions, I'll refrain for now.
2013-11-03 17:33:05 +01:00
|
|
|
* Here we catch undefined bytes in datums that are returned to the
|
2013-06-27 02:00:08 +02:00
|
|
|
* client without hitting disk; see comments at the related check in
|
Prevent memory leaks from accumulating across printtup() calls.
Historically, printtup() has assumed that it could prevent memory leakage
by pfree'ing the string result of each output function and manually
managing detoasting of toasted values. This amounts to assuming that
datatype output functions never leak any memory internally; an assumption
we've already decided to be bogus elsewhere, for example in COPY OUT.
range_out in particular is known to leak multiple kilobytes per call, as
noted in bug #8573 from Godfried Vanluffelen. While we could go in and fix
that leak, it wouldn't be very notationally convenient, and in any case
there have been and undoubtedly will again be other leaks in other output
functions. So what seems like the best solution is to run the output
functions in a temporary memory context that can be reset after each row,
as we're doing in COPY OUT. Some quick experimentation suggests this is
actually a tad faster than the retail pfree's anyway.
This patch fixes all the variants of printtup, except for debugtup()
which is used in standalone mode. It doesn't seem worth worrying
about query-lifespan leaks in standalone mode, and fixing that case
would be a bit tedious since debugtup() doesn't currently have any
startup or shutdown functions.
While at it, remove manual detoast management from several other
output-function call sites that had copied it from printtup(). This
doesn't make a lot of difference right now, but in view of recent
discussions about supporting "non-flattened" Datums, we're going to
want that code gone eventually anyway.
Back-patch to 9.2 where range_out was introduced. We might eventually
decide to back-patch this further, but in the absence of known major
leaks in older output functions, I'll refrain for now.
2013-11-03 17:33:05 +01:00
|
|
|
* PageAddItem(). This test is most useful for uncompressed,
|
|
|
|
* non-external datums, but we're quite likely to see such here when
|
|
|
|
* testing new C functions.
|
2003-05-09 20:08:48 +02:00
|
|
|
*/
|
|
|
|
if (thisState->typisvarlena)
|
Prevent memory leaks from accumulating across printtup() calls.
Historically, printtup() has assumed that it could prevent memory leakage
by pfree'ing the string result of each output function and manually
managing detoasting of toasted values. This amounts to assuming that
datatype output functions never leak any memory internally; an assumption
we've already decided to be bogus elsewhere, for example in COPY OUT.
range_out in particular is known to leak multiple kilobytes per call, as
noted in bug #8573 from Godfried Vanluffelen. While we could go in and fix
that leak, it wouldn't be very notationally convenient, and in any case
there have been and undoubtedly will again be other leaks in other output
functions. So what seems like the best solution is to run the output
functions in a temporary memory context that can be reset after each row,
as we're doing in COPY OUT. Some quick experimentation suggests this is
actually a tad faster than the retail pfree's anyway.
This patch fixes all the variants of printtup, except for debugtup()
which is used in standalone mode. It doesn't seem worth worrying
about query-lifespan leaks in standalone mode, and fixing that case
would be a bit tedious since debugtup() doesn't currently have any
startup or shutdown functions.
While at it, remove manual detoast management from several other
output-function call sites that had copied it from printtup(). This
doesn't make a lot of difference right now, but in view of recent
discussions about supporting "non-flattened" Datums, we're going to
want that code gone eventually anyway.
Back-patch to 9.2 where range_out was introduced. We might eventually
decide to back-patch this further, but in the absence of known major
leaks in older output functions, I'll refrain for now.
2013-11-03 17:33:05 +01:00
|
|
|
VALGRIND_CHECK_MEM_IS_DEFINED(DatumGetPointer(attr),
|
|
|
|
VARSIZE_ANY(attr));
|
2003-05-08 20:16:37 +02:00
|
|
|
|
2003-05-09 20:08:48 +02:00
|
|
|
if (thisState->format == 0)
|
2003-05-08 20:16:37 +02:00
|
|
|
{
|
2003-05-09 20:08:48 +02:00
|
|
|
/* Text output */
|
|
|
|
char *outputstr;
|
|
|
|
|
2006-04-04 21:35:37 +02:00
|
|
|
outputstr = OutputFunctionCall(&thisState->finfo, attr);
|
2003-05-09 20:08:48 +02:00
|
|
|
pq_sendcountedtext(&buf, outputstr, strlen(outputstr), false);
|
2003-05-08 20:16:37 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-05-09 20:08:48 +02:00
|
|
|
/* Binary output */
|
|
|
|
bytea *outputbytes;
|
|
|
|
|
2006-04-04 21:35:37 +02:00
|
|
|
outputbytes = SendFunctionCall(&thisState->finfo, attr);
|
2003-05-09 20:08:48 +02:00
|
|
|
pq_sendint(&buf, VARSIZE(outputbytes) - VARHDRSZ, 4);
|
|
|
|
pq_sendbytes(&buf, VARDATA(outputbytes),
|
|
|
|
VARSIZE(outputbytes) - VARHDRSZ);
|
2003-05-08 20:16:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pq_endmessage(&buf);
|
Prevent memory leaks from accumulating across printtup() calls.
Historically, printtup() has assumed that it could prevent memory leakage
by pfree'ing the string result of each output function and manually
managing detoasting of toasted values. This amounts to assuming that
datatype output functions never leak any memory internally; an assumption
we've already decided to be bogus elsewhere, for example in COPY OUT.
range_out in particular is known to leak multiple kilobytes per call, as
noted in bug #8573 from Godfried Vanluffelen. While we could go in and fix
that leak, it wouldn't be very notationally convenient, and in any case
there have been and undoubtedly will again be other leaks in other output
functions. So what seems like the best solution is to run the output
functions in a temporary memory context that can be reset after each row,
as we're doing in COPY OUT. Some quick experimentation suggests this is
actually a tad faster than the retail pfree's anyway.
This patch fixes all the variants of printtup, except for debugtup()
which is used in standalone mode. It doesn't seem worth worrying
about query-lifespan leaks in standalone mode, and fixing that case
would be a bit tedious since debugtup() doesn't currently have any
startup or shutdown functions.
While at it, remove manual detoast management from several other
output-function call sites that had copied it from printtup(). This
doesn't make a lot of difference right now, but in view of recent
discussions about supporting "non-flattened" Datums, we're going to
want that code gone eventually anyway.
Back-patch to 9.2 where range_out was introduced. We might eventually
decide to back-patch this further, but in the absence of known major
leaks in older output functions, I'll refrain for now.
2013-11-03 17:33:05 +01:00
|
|
|
|
|
|
|
/* Return to caller's context, and flush row's temporary memory */
|
|
|
|
MemoryContextSwitchTo(oldcontext);
|
|
|
|
MemoryContextReset(myState->tmpcontext);
|
2003-05-08 20:16:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* printtup_20 --- print a tuple in protocol 2.0
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
static void
|
2005-03-16 22:38:10 +01:00
|
|
|
printtup_20(TupleTableSlot *slot, DestReceiver *self)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2005-10-15 04:49:52 +02:00
|
|
|
TupleDesc typeinfo = slot->tts_tupleDescriptor;
|
1999-05-25 18:15:34 +02:00
|
|
|
DR_printtup *myState = (DR_printtup *) self;
|
Prevent memory leaks from accumulating across printtup() calls.
Historically, printtup() has assumed that it could prevent memory leakage
by pfree'ing the string result of each output function and manually
managing detoasting of toasted values. This amounts to assuming that
datatype output functions never leak any memory internally; an assumption
we've already decided to be bogus elsewhere, for example in COPY OUT.
range_out in particular is known to leak multiple kilobytes per call, as
noted in bug #8573 from Godfried Vanluffelen. While we could go in and fix
that leak, it wouldn't be very notationally convenient, and in any case
there have been and undoubtedly will again be other leaks in other output
functions. So what seems like the best solution is to run the output
functions in a temporary memory context that can be reset after each row,
as we're doing in COPY OUT. Some quick experimentation suggests this is
actually a tad faster than the retail pfree's anyway.
This patch fixes all the variants of printtup, except for debugtup()
which is used in standalone mode. It doesn't seem worth worrying
about query-lifespan leaks in standalone mode, and fixing that case
would be a bit tedious since debugtup() doesn't currently have any
startup or shutdown functions.
While at it, remove manual detoast management from several other
output-function call sites that had copied it from printtup(). This
doesn't make a lot of difference right now, but in view of recent
discussions about supporting "non-flattened" Datums, we're going to
want that code gone eventually anyway.
Back-patch to 9.2 where range_out was introduced. We might eventually
decide to back-patch this further, but in the absence of known major
leaks in older output functions, I'll refrain for now.
2013-11-03 17:33:05 +01:00
|
|
|
MemoryContext oldcontext;
|
1999-04-25 05:19:27 +02:00
|
|
|
StringInfoData buf;
|
2003-05-26 19:51:38 +02:00
|
|
|
int natts = typeinfo->natts;
|
1997-09-08 04:41:22 +02:00
|
|
|
int i,
|
|
|
|
j,
|
1999-04-25 21:27:47 +02:00
|
|
|
k;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-01-27 01:36:28 +01:00
|
|
|
/* Set or update my derived attribute info, if needed */
|
2000-12-01 23:10:31 +01:00
|
|
|
if (myState->attrinfo != typeinfo || myState->nattrs != natts)
|
|
|
|
printtup_prepare_info(myState, typeinfo, natts);
|
1999-01-27 01:36:28 +01:00
|
|
|
|
2005-03-16 22:38:10 +01:00
|
|
|
/* Make sure the tuple is fully deconstructed */
|
|
|
|
slot_getallattrs(slot);
|
2004-06-04 22:35:21 +02:00
|
|
|
|
Prevent memory leaks from accumulating across printtup() calls.
Historically, printtup() has assumed that it could prevent memory leakage
by pfree'ing the string result of each output function and manually
managing detoasting of toasted values. This amounts to assuming that
datatype output functions never leak any memory internally; an assumption
we've already decided to be bogus elsewhere, for example in COPY OUT.
range_out in particular is known to leak multiple kilobytes per call, as
noted in bug #8573 from Godfried Vanluffelen. While we could go in and fix
that leak, it wouldn't be very notationally convenient, and in any case
there have been and undoubtedly will again be other leaks in other output
functions. So what seems like the best solution is to run the output
functions in a temporary memory context that can be reset after each row,
as we're doing in COPY OUT. Some quick experimentation suggests this is
actually a tad faster than the retail pfree's anyway.
This patch fixes all the variants of printtup, except for debugtup()
which is used in standalone mode. It doesn't seem worth worrying
about query-lifespan leaks in standalone mode, and fixing that case
would be a bit tedious since debugtup() doesn't currently have any
startup or shutdown functions.
While at it, remove manual detoast management from several other
output-function call sites that had copied it from printtup(). This
doesn't make a lot of difference right now, but in view of recent
discussions about supporting "non-flattened" Datums, we're going to
want that code gone eventually anyway.
Back-patch to 9.2 where range_out was introduced. We might eventually
decide to back-patch this further, but in the absence of known major
leaks in older output functions, I'll refrain for now.
2013-11-03 17:33:05 +01:00
|
|
|
/* Switch into per-row context so we can recover memory below */
|
|
|
|
oldcontext = MemoryContextSwitchTo(myState->tmpcontext);
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* tell the frontend to expect new tuple data (in ASCII style)
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2003-04-22 02:08:07 +02:00
|
|
|
pq_beginmessage(&buf, 'D');
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* send a bitmap of which attributes are not null
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
|
|
|
j = 0;
|
|
|
|
k = 1 << 7;
|
2000-12-01 23:10:31 +01:00
|
|
|
for (i = 0; i < natts; ++i)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2005-04-23 19:45:35 +02:00
|
|
|
if (!slot->tts_isnull[i])
|
1999-01-24 23:50:58 +01:00
|
|
|
j |= k; /* set bit if not null */
|
1997-09-07 07:04:48 +02:00
|
|
|
k >>= 1;
|
1999-01-24 23:50:58 +01:00
|
|
|
if (k == 0) /* end of byte? */
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1999-04-25 05:19:27 +02:00
|
|
|
pq_sendint(&buf, j, 1);
|
1997-09-07 07:04:48 +02:00
|
|
|
j = 0;
|
|
|
|
k = 1 << 7;
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1999-01-24 23:50:58 +01:00
|
|
|
if (k != (1 << 7)) /* flush last partial byte */
|
1999-04-25 05:19:27 +02:00
|
|
|
pq_sendint(&buf, j, 1);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* send the attributes of this tuple
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2000-12-01 23:10:31 +01:00
|
|
|
for (i = 0; i < natts; ++i)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1999-05-25 18:15:34 +02:00
|
|
|
PrinttupAttrInfo *thisState = myState->myinfo + i;
|
Prevent memory leaks from accumulating across printtup() calls.
Historically, printtup() has assumed that it could prevent memory leakage
by pfree'ing the string result of each output function and manually
managing detoasting of toasted values. This amounts to assuming that
datatype output functions never leak any memory internally; an assumption
we've already decided to be bogus elsewhere, for example in COPY OUT.
range_out in particular is known to leak multiple kilobytes per call, as
noted in bug #8573 from Godfried Vanluffelen. While we could go in and fix
that leak, it wouldn't be very notationally convenient, and in any case
there have been and undoubtedly will again be other leaks in other output
functions. So what seems like the best solution is to run the output
functions in a temporary memory context that can be reset after each row,
as we're doing in COPY OUT. Some quick experimentation suggests this is
actually a tad faster than the retail pfree's anyway.
This patch fixes all the variants of printtup, except for debugtup()
which is used in standalone mode. It doesn't seem worth worrying
about query-lifespan leaks in standalone mode, and fixing that case
would be a bit tedious since debugtup() doesn't currently have any
startup or shutdown functions.
While at it, remove manual detoast management from several other
output-function call sites that had copied it from printtup(). This
doesn't make a lot of difference right now, but in view of recent
discussions about supporting "non-flattened" Datums, we're going to
want that code gone eventually anyway.
Back-patch to 9.2 where range_out was introduced. We might eventually
decide to back-patch this further, but in the absence of known major
leaks in older output functions, I'll refrain for now.
2013-11-03 17:33:05 +01:00
|
|
|
Datum attr = slot->tts_values[i];
|
2000-12-01 23:10:31 +01:00
|
|
|
char *outputstr;
|
1999-05-25 18:15:34 +02:00
|
|
|
|
2005-03-16 22:38:10 +01:00
|
|
|
if (slot->tts_isnull[i])
|
1998-08-30 21:30:38 +02:00
|
|
|
continue;
|
2000-12-01 23:10:31 +01:00
|
|
|
|
2003-05-09 20:08:48 +02:00
|
|
|
Assert(thisState->format == 0);
|
|
|
|
|
2006-04-04 21:35:37 +02:00
|
|
|
outputstr = OutputFunctionCall(&thisState->finfo, attr);
|
2003-05-09 20:08:48 +02:00
|
|
|
pq_sendcountedtext(&buf, outputstr, strlen(outputstr), true);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1999-04-25 05:19:27 +02:00
|
|
|
|
|
|
|
pq_endmessage(&buf);
|
Prevent memory leaks from accumulating across printtup() calls.
Historically, printtup() has assumed that it could prevent memory leakage
by pfree'ing the string result of each output function and manually
managing detoasting of toasted values. This amounts to assuming that
datatype output functions never leak any memory internally; an assumption
we've already decided to be bogus elsewhere, for example in COPY OUT.
range_out in particular is known to leak multiple kilobytes per call, as
noted in bug #8573 from Godfried Vanluffelen. While we could go in and fix
that leak, it wouldn't be very notationally convenient, and in any case
there have been and undoubtedly will again be other leaks in other output
functions. So what seems like the best solution is to run the output
functions in a temporary memory context that can be reset after each row,
as we're doing in COPY OUT. Some quick experimentation suggests this is
actually a tad faster than the retail pfree's anyway.
This patch fixes all the variants of printtup, except for debugtup()
which is used in standalone mode. It doesn't seem worth worrying
about query-lifespan leaks in standalone mode, and fixing that case
would be a bit tedious since debugtup() doesn't currently have any
startup or shutdown functions.
While at it, remove manual detoast management from several other
output-function call sites that had copied it from printtup(). This
doesn't make a lot of difference right now, but in view of recent
discussions about supporting "non-flattened" Datums, we're going to
want that code gone eventually anyway.
Back-patch to 9.2 where range_out was introduced. We might eventually
decide to back-patch this further, but in the absence of known major
leaks in older output functions, I'll refrain for now.
2013-11-03 17:33:05 +01:00
|
|
|
|
|
|
|
/* Return to caller's context, and flush row's temporary memory */
|
|
|
|
MemoryContextSwitchTo(oldcontext);
|
|
|
|
MemoryContextReset(myState->tmpcontext);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1999-01-27 01:36:28 +01:00
|
|
|
/* ----------------
|
2003-05-06 22:26:28 +02:00
|
|
|
* printtup_shutdown
|
1999-01-27 01:36:28 +01:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
static void
|
2003-05-06 22:26:28 +02:00
|
|
|
printtup_shutdown(DestReceiver *self)
|
1999-01-27 01:36:28 +01:00
|
|
|
{
|
1999-05-25 18:15:34 +02:00
|
|
|
DR_printtup *myState = (DR_printtup *) self;
|
|
|
|
|
1999-01-27 01:36:28 +01:00
|
|
|
if (myState->myinfo)
|
|
|
|
pfree(myState->myinfo);
|
2003-05-06 22:26:28 +02:00
|
|
|
myState->myinfo = NULL;
|
2004-06-04 22:35:21 +02:00
|
|
|
|
2003-05-06 22:26:28 +02:00
|
|
|
myState->attrinfo = NULL;
|
Prevent memory leaks from accumulating across printtup() calls.
Historically, printtup() has assumed that it could prevent memory leakage
by pfree'ing the string result of each output function and manually
managing detoasting of toasted values. This amounts to assuming that
datatype output functions never leak any memory internally; an assumption
we've already decided to be bogus elsewhere, for example in COPY OUT.
range_out in particular is known to leak multiple kilobytes per call, as
noted in bug #8573 from Godfried Vanluffelen. While we could go in and fix
that leak, it wouldn't be very notationally convenient, and in any case
there have been and undoubtedly will again be other leaks in other output
functions. So what seems like the best solution is to run the output
functions in a temporary memory context that can be reset after each row,
as we're doing in COPY OUT. Some quick experimentation suggests this is
actually a tad faster than the retail pfree's anyway.
This patch fixes all the variants of printtup, except for debugtup()
which is used in standalone mode. It doesn't seem worth worrying
about query-lifespan leaks in standalone mode, and fixing that case
would be a bit tedious since debugtup() doesn't currently have any
startup or shutdown functions.
While at it, remove manual detoast management from several other
output-function call sites that had copied it from printtup(). This
doesn't make a lot of difference right now, but in view of recent
discussions about supporting "non-flattened" Datums, we're going to
want that code gone eventually anyway.
Back-patch to 9.2 where range_out was introduced. We might eventually
decide to back-patch this further, but in the absence of known major
leaks in older output functions, I'll refrain for now.
2013-11-03 17:33:05 +01:00
|
|
|
|
|
|
|
if (myState->tmpcontext)
|
|
|
|
MemoryContextDelete(myState->tmpcontext);
|
|
|
|
myState->tmpcontext = NULL;
|
2003-05-06 22:26:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* printtup_destroy
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
printtup_destroy(DestReceiver *self)
|
|
|
|
{
|
|
|
|
pfree(self);
|
1999-01-27 01:36:28 +01:00
|
|
|
}
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/* ----------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* printatt
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
printatt(unsigned attributeId,
|
1998-09-01 05:29:17 +02:00
|
|
|
Form_pg_attribute attributeP,
|
1997-09-07 07:04:48 +02:00
|
|
|
char *value)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1998-05-14 19:18:14 +02:00
|
|
|
printf("\t%2d: %s%s%s%s\t(typeid = %u, len = %d, typmod = %d, byval = %c)\n",
|
1997-09-07 07:04:48 +02:00
|
|
|
attributeId,
|
1999-11-08 00:08:36 +01:00
|
|
|
NameStr(attributeP->attname),
|
1997-09-07 07:04:48 +02:00
|
|
|
value != NULL ? " = \"" : "",
|
|
|
|
value != NULL ? value : "",
|
|
|
|
value != NULL ? "\"" : "",
|
|
|
|
(unsigned int) (attributeP->atttypid),
|
|
|
|
attributeP->attlen,
|
1998-05-14 19:18:14 +02:00
|
|
|
attributeP->atttypmod,
|
1997-09-07 07:04:48 +02:00
|
|
|
attributeP->attbyval ? 't' : 'f');
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------
|
2003-05-06 22:26:28 +02:00
|
|
|
* debugStartup - prepare to print tuples for an interactive backend
|
2002-02-27 20:36:13 +01:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
void
|
2003-05-08 20:16:37 +02:00
|
|
|
debugStartup(DestReceiver *self, int operation, TupleDesc typeinfo)
|
2002-02-27 20:36:13 +01:00
|
|
|
{
|
2003-05-08 20:16:37 +02:00
|
|
|
int natts = typeinfo->natts;
|
|
|
|
Form_pg_attribute *attinfo = typeinfo->attrs;
|
|
|
|
int i;
|
|
|
|
|
2002-02-27 20:36:13 +01:00
|
|
|
/*
|
|
|
|
* show the return type of the tuples
|
|
|
|
*/
|
2003-05-08 20:16:37 +02:00
|
|
|
for (i = 0; i < natts; ++i)
|
2004-01-07 19:56:30 +01:00
|
|
|
printatt((unsigned) i + 1, attinfo[i], NULL);
|
2003-05-08 20:16:37 +02:00
|
|
|
printf("\t----\n");
|
2002-02-27 20:36:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* debugtup - print one tuple for an interactive backend
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
void
|
2005-03-16 22:38:10 +01:00
|
|
|
debugtup(TupleTableSlot *slot, DestReceiver *self)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2005-03-16 22:38:10 +01:00
|
|
|
TupleDesc typeinfo = slot->tts_tupleDescriptor;
|
2003-05-26 19:51:38 +02:00
|
|
|
int natts = typeinfo->natts;
|
1998-02-26 05:46:47 +01:00
|
|
|
int i;
|
Prevent memory leaks from accumulating across printtup() calls.
Historically, printtup() has assumed that it could prevent memory leakage
by pfree'ing the string result of each output function and manually
managing detoasting of toasted values. This amounts to assuming that
datatype output functions never leak any memory internally; an assumption
we've already decided to be bogus elsewhere, for example in COPY OUT.
range_out in particular is known to leak multiple kilobytes per call, as
noted in bug #8573 from Godfried Vanluffelen. While we could go in and fix
that leak, it wouldn't be very notationally convenient, and in any case
there have been and undoubtedly will again be other leaks in other output
functions. So what seems like the best solution is to run the output
functions in a temporary memory context that can be reset after each row,
as we're doing in COPY OUT. Some quick experimentation suggests this is
actually a tad faster than the retail pfree's anyway.
This patch fixes all the variants of printtup, except for debugtup()
which is used in standalone mode. It doesn't seem worth worrying
about query-lifespan leaks in standalone mode, and fixing that case
would be a bit tedious since debugtup() doesn't currently have any
startup or shutdown functions.
While at it, remove manual detoast management from several other
output-function call sites that had copied it from printtup(). This
doesn't make a lot of difference right now, but in view of recent
discussions about supporting "non-flattened" Datums, we're going to
want that code gone eventually anyway.
Back-patch to 9.2 where range_out was introduced. We might eventually
decide to back-patch this further, but in the absence of known major
leaks in older output functions, I'll refrain for now.
2013-11-03 17:33:05 +01:00
|
|
|
Datum attr;
|
1997-09-12 06:09:08 +02:00
|
|
|
char *value;
|
1997-09-08 04:41:22 +02:00
|
|
|
bool isnull;
|
2005-05-01 20:56:19 +02:00
|
|
|
Oid typoutput;
|
2000-12-01 23:10:31 +01:00
|
|
|
bool typisvarlena;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-12-01 23:10:31 +01:00
|
|
|
for (i = 0; i < natts; ++i)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
Prevent memory leaks from accumulating across printtup() calls.
Historically, printtup() has assumed that it could prevent memory leakage
by pfree'ing the string result of each output function and manually
managing detoasting of toasted values. This amounts to assuming that
datatype output functions never leak any memory internally; an assumption
we've already decided to be bogus elsewhere, for example in COPY OUT.
range_out in particular is known to leak multiple kilobytes per call, as
noted in bug #8573 from Godfried Vanluffelen. While we could go in and fix
that leak, it wouldn't be very notationally convenient, and in any case
there have been and undoubtedly will again be other leaks in other output
functions. So what seems like the best solution is to run the output
functions in a temporary memory context that can be reset after each row,
as we're doing in COPY OUT. Some quick experimentation suggests this is
actually a tad faster than the retail pfree's anyway.
This patch fixes all the variants of printtup, except for debugtup()
which is used in standalone mode. It doesn't seem worth worrying
about query-lifespan leaks in standalone mode, and fixing that case
would be a bit tedious since debugtup() doesn't currently have any
startup or shutdown functions.
While at it, remove manual detoast management from several other
output-function call sites that had copied it from printtup(). This
doesn't make a lot of difference right now, but in view of recent
discussions about supporting "non-flattened" Datums, we're going to
want that code gone eventually anyway.
Back-patch to 9.2 where range_out was introduced. We might eventually
decide to back-patch this further, but in the absence of known major
leaks in older output functions, I'll refrain for now.
2013-11-03 17:33:05 +01:00
|
|
|
attr = slot_getattr(slot, i + 1, &isnull);
|
1999-01-24 06:40:49 +01:00
|
|
|
if (isnull)
|
|
|
|
continue;
|
2003-05-09 20:08:48 +02:00
|
|
|
getTypeOutputInfo(typeinfo->attrs[i]->atttypid,
|
2005-05-01 20:56:19 +02:00
|
|
|
&typoutput, &typisvarlena);
|
2003-08-04 02:43:34 +02:00
|
|
|
|
2006-04-04 21:35:37 +02:00
|
|
|
value = OidOutputFunctionCall(typoutput, attr);
|
2000-12-01 23:10:31 +01:00
|
|
|
|
2003-05-09 20:08:48 +02:00
|
|
|
printatt((unsigned) i + 1, typeinfo->attrs[i], value);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
printf("\t----\n");
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------
|
2003-05-08 20:16:37 +02:00
|
|
|
* printtup_internal_20 --- print a binary tuple in protocol 2.0
|
|
|
|
*
|
|
|
|
* We use a different message type, i.e. 'B' instead of 'D' to
|
|
|
|
* indicate a tuple in internal (binary) form.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2003-05-09 20:08:48 +02:00
|
|
|
* This is largely same as printtup_20, except we use binary formatting.
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
2000-12-01 23:10:31 +01:00
|
|
|
static void
|
2005-03-16 22:38:10 +01:00
|
|
|
printtup_internal_20(TupleTableSlot *slot, DestReceiver *self)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2005-10-15 04:49:52 +02:00
|
|
|
TupleDesc typeinfo = slot->tts_tupleDescriptor;
|
2000-12-01 23:10:31 +01:00
|
|
|
DR_printtup *myState = (DR_printtup *) self;
|
Prevent memory leaks from accumulating across printtup() calls.
Historically, printtup() has assumed that it could prevent memory leakage
by pfree'ing the string result of each output function and manually
managing detoasting of toasted values. This amounts to assuming that
datatype output functions never leak any memory internally; an assumption
we've already decided to be bogus elsewhere, for example in COPY OUT.
range_out in particular is known to leak multiple kilobytes per call, as
noted in bug #8573 from Godfried Vanluffelen. While we could go in and fix
that leak, it wouldn't be very notationally convenient, and in any case
there have been and undoubtedly will again be other leaks in other output
functions. So what seems like the best solution is to run the output
functions in a temporary memory context that can be reset after each row,
as we're doing in COPY OUT. Some quick experimentation suggests this is
actually a tad faster than the retail pfree's anyway.
This patch fixes all the variants of printtup, except for debugtup()
which is used in standalone mode. It doesn't seem worth worrying
about query-lifespan leaks in standalone mode, and fixing that case
would be a bit tedious since debugtup() doesn't currently have any
startup or shutdown functions.
While at it, remove manual detoast management from several other
output-function call sites that had copied it from printtup(). This
doesn't make a lot of difference right now, but in view of recent
discussions about supporting "non-flattened" Datums, we're going to
want that code gone eventually anyway.
Back-patch to 9.2 where range_out was introduced. We might eventually
decide to back-patch this further, but in the absence of known major
leaks in older output functions, I'll refrain for now.
2013-11-03 17:33:05 +01:00
|
|
|
MemoryContext oldcontext;
|
1999-04-25 05:19:27 +02:00
|
|
|
StringInfoData buf;
|
2003-05-26 19:51:38 +02:00
|
|
|
int natts = typeinfo->natts;
|
1997-09-08 04:41:22 +02:00
|
|
|
int i,
|
|
|
|
j,
|
|
|
|
k;
|
2000-12-01 23:10:31 +01:00
|
|
|
|
|
|
|
/* Set or update my derived attribute info, if needed */
|
|
|
|
if (myState->attrinfo != typeinfo || myState->nattrs != natts)
|
|
|
|
printtup_prepare_info(myState, typeinfo, natts);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2005-03-16 22:38:10 +01:00
|
|
|
/* Make sure the tuple is fully deconstructed */
|
|
|
|
slot_getallattrs(slot);
|
2004-06-04 22:35:21 +02:00
|
|
|
|
Prevent memory leaks from accumulating across printtup() calls.
Historically, printtup() has assumed that it could prevent memory leakage
by pfree'ing the string result of each output function and manually
managing detoasting of toasted values. This amounts to assuming that
datatype output functions never leak any memory internally; an assumption
we've already decided to be bogus elsewhere, for example in COPY OUT.
range_out in particular is known to leak multiple kilobytes per call, as
noted in bug #8573 from Godfried Vanluffelen. While we could go in and fix
that leak, it wouldn't be very notationally convenient, and in any case
there have been and undoubtedly will again be other leaks in other output
functions. So what seems like the best solution is to run the output
functions in a temporary memory context that can be reset after each row,
as we're doing in COPY OUT. Some quick experimentation suggests this is
actually a tad faster than the retail pfree's anyway.
This patch fixes all the variants of printtup, except for debugtup()
which is used in standalone mode. It doesn't seem worth worrying
about query-lifespan leaks in standalone mode, and fixing that case
would be a bit tedious since debugtup() doesn't currently have any
startup or shutdown functions.
While at it, remove manual detoast management from several other
output-function call sites that had copied it from printtup(). This
doesn't make a lot of difference right now, but in view of recent
discussions about supporting "non-flattened" Datums, we're going to
want that code gone eventually anyway.
Back-patch to 9.2 where range_out was introduced. We might eventually
decide to back-patch this further, but in the absence of known major
leaks in older output functions, I'll refrain for now.
2013-11-03 17:33:05 +01:00
|
|
|
/* Switch into per-row context so we can recover memory below */
|
|
|
|
oldcontext = MemoryContextSwitchTo(myState->tmpcontext);
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* tell the frontend to expect new tuple data (in binary style)
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2003-04-22 02:08:07 +02:00
|
|
|
pq_beginmessage(&buf, 'B');
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* send a bitmap of which attributes are not null
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
|
|
|
j = 0;
|
|
|
|
k = 1 << 7;
|
2000-12-01 23:10:31 +01:00
|
|
|
for (i = 0; i < natts; ++i)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2005-04-23 19:45:35 +02:00
|
|
|
if (!slot->tts_isnull[i])
|
1999-01-24 23:50:58 +01:00
|
|
|
j |= k; /* set bit if not null */
|
1997-09-07 07:04:48 +02:00
|
|
|
k >>= 1;
|
1999-01-24 23:50:58 +01:00
|
|
|
if (k == 0) /* end of byte? */
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1999-04-25 05:19:27 +02:00
|
|
|
pq_sendint(&buf, j, 1);
|
1997-09-07 07:04:48 +02:00
|
|
|
j = 0;
|
|
|
|
k = 1 << 7;
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1999-01-24 23:50:58 +01:00
|
|
|
if (k != (1 << 7)) /* flush last partial byte */
|
1999-04-25 05:19:27 +02:00
|
|
|
pq_sendint(&buf, j, 1);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* send the attributes of this tuple
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2000-12-01 23:10:31 +01:00
|
|
|
for (i = 0; i < natts; ++i)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2000-12-01 23:10:31 +01:00
|
|
|
PrinttupAttrInfo *thisState = myState->myinfo + i;
|
Prevent memory leaks from accumulating across printtup() calls.
Historically, printtup() has assumed that it could prevent memory leakage
by pfree'ing the string result of each output function and manually
managing detoasting of toasted values. This amounts to assuming that
datatype output functions never leak any memory internally; an assumption
we've already decided to be bogus elsewhere, for example in COPY OUT.
range_out in particular is known to leak multiple kilobytes per call, as
noted in bug #8573 from Godfried Vanluffelen. While we could go in and fix
that leak, it wouldn't be very notationally convenient, and in any case
there have been and undoubtedly will again be other leaks in other output
functions. So what seems like the best solution is to run the output
functions in a temporary memory context that can be reset after each row,
as we're doing in COPY OUT. Some quick experimentation suggests this is
actually a tad faster than the retail pfree's anyway.
This patch fixes all the variants of printtup, except for debugtup()
which is used in standalone mode. It doesn't seem worth worrying
about query-lifespan leaks in standalone mode, and fixing that case
would be a bit tedious since debugtup() doesn't currently have any
startup or shutdown functions.
While at it, remove manual detoast management from several other
output-function call sites that had copied it from printtup(). This
doesn't make a lot of difference right now, but in view of recent
discussions about supporting "non-flattened" Datums, we're going to
want that code gone eventually anyway.
Back-patch to 9.2 where range_out was introduced. We might eventually
decide to back-patch this further, but in the absence of known major
leaks in older output functions, I'll refrain for now.
2013-11-03 17:33:05 +01:00
|
|
|
Datum attr = slot->tts_values[i];
|
2003-05-09 20:08:48 +02:00
|
|
|
bytea *outputbytes;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2005-03-16 22:38:10 +01:00
|
|
|
if (slot->tts_isnull[i])
|
2000-12-01 23:10:31 +01:00
|
|
|
continue;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2003-05-09 20:08:48 +02:00
|
|
|
Assert(thisState->format == 1);
|
1998-12-12 23:04:09 +01:00
|
|
|
|
2006-04-04 21:35:37 +02:00
|
|
|
outputbytes = SendFunctionCall(&thisState->finfo, attr);
|
2003-05-09 20:08:48 +02:00
|
|
|
pq_sendint(&buf, VARSIZE(outputbytes) - VARHDRSZ, 4);
|
|
|
|
pq_sendbytes(&buf, VARDATA(outputbytes),
|
|
|
|
VARSIZE(outputbytes) - VARHDRSZ);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1999-04-25 05:19:27 +02:00
|
|
|
|
|
|
|
pq_endmessage(&buf);
|
Prevent memory leaks from accumulating across printtup() calls.
Historically, printtup() has assumed that it could prevent memory leakage
by pfree'ing the string result of each output function and manually
managing detoasting of toasted values. This amounts to assuming that
datatype output functions never leak any memory internally; an assumption
we've already decided to be bogus elsewhere, for example in COPY OUT.
range_out in particular is known to leak multiple kilobytes per call, as
noted in bug #8573 from Godfried Vanluffelen. While we could go in and fix
that leak, it wouldn't be very notationally convenient, and in any case
there have been and undoubtedly will again be other leaks in other output
functions. So what seems like the best solution is to run the output
functions in a temporary memory context that can be reset after each row,
as we're doing in COPY OUT. Some quick experimentation suggests this is
actually a tad faster than the retail pfree's anyway.
This patch fixes all the variants of printtup, except for debugtup()
which is used in standalone mode. It doesn't seem worth worrying
about query-lifespan leaks in standalone mode, and fixing that case
would be a bit tedious since debugtup() doesn't currently have any
startup or shutdown functions.
While at it, remove manual detoast management from several other
output-function call sites that had copied it from printtup(). This
doesn't make a lot of difference right now, but in view of recent
discussions about supporting "non-flattened" Datums, we're going to
want that code gone eventually anyway.
Back-patch to 9.2 where range_out was introduced. We might eventually
decide to back-patch this further, but in the absence of known major
leaks in older output functions, I'll refrain for now.
2013-11-03 17:33:05 +01:00
|
|
|
|
|
|
|
/* Return to caller's context, and flush row's temporary memory */
|
|
|
|
MemoryContextSwitchTo(oldcontext);
|
|
|
|
MemoryContextReset(myState->tmpcontext);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|