printtup.c 17.6 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * printtup.c
4
 *	  Routines to print out tuples to the destination (both frontend
5
 *	  clients and standalone backends are supported here).
6
 *
7
 *
B
Bruce Momjian 已提交
8
 * Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group
B
Add:  
Bruce Momjian 已提交
9
 * Portions Copyright (c) 1994, Regents of the University of California
10 11
 *
 * IDENTIFICATION
B
Bruce Momjian 已提交
12
 *	  $PostgreSQL: pgsql/src/backend/access/common/printtup.c,v 1.85 2004/08/29 05:06:39 momjian Exp $
13 14 15
 *
 *-------------------------------------------------------------------------
 */
16 17 18 19
#include "postgres.h"

#include "access/heapam.h"
#include "access/printtup.h"
20
#include "libpq/libpq.h"
21
#include "libpq/pqformat.h"
22
#include "utils/lsyscache.h"
23
#include "utils/portal.h"
24

M
Marc G. Fournier 已提交
25

26
static void printtup_startup(DestReceiver *self, int operation,
B
Bruce Momjian 已提交
27
				 TupleDesc typeinfo);
28
static void printtup(HeapTuple tuple, TupleDesc typeinfo,
B
Bruce Momjian 已提交
29
		 DestReceiver *self);
30
static void printtup_20(HeapTuple tuple, TupleDesc typeinfo,
B
Bruce Momjian 已提交
31
			DestReceiver *self);
32
static void printtup_internal_20(HeapTuple tuple, TupleDesc typeinfo,
B
Bruce Momjian 已提交
33
					 DestReceiver *self);
34 35 36
static void printtup_shutdown(DestReceiver *self);
static void printtup_destroy(DestReceiver *self);

37

38
/* ----------------------------------------------------------------
39
 *		printtup / debugtup support
40 41 42
 * ----------------------------------------------------------------
 */

43 44
/* ----------------
 *		Private state for a printtup destination object
45 46 47
 *
 * NOTE: finfo is the lookup info for either typoutput or typsend, whichever
 * we are using for this column.
48 49
 * ----------------
 */
B
Bruce Momjian 已提交
50 51
typedef struct
{								/* Per-attribute information */
52 53
	Oid			typoutput;		/* Oid for the type's text output fn */
	Oid			typsend;		/* Oid for the type's binary output fn */
54
	Oid			typioparam;		/* param to pass to the output fn */
55
	bool		typisvarlena;	/* is it varlena (ie possibly toastable)? */
56 57
	int16		format;			/* format code for this column */
	FmgrInfo	finfo;			/* Precomputed call info for output fn */
58
} PrinttupAttrInfo;
59

B
Bruce Momjian 已提交
60 61 62
typedef struct
{
	DestReceiver pub;			/* publicly-known function pointers */
63
	Portal		portal;			/* the Portal we are printing from */
64
	bool		sendDescrip;	/* send RowDescription at startup? */
B
Bruce Momjian 已提交
65 66 67
	TupleDesc	attrinfo;		/* The attr info we are set up for */
	int			nattrs;
	PrinttupAttrInfo *myinfo;	/* Cached info about each attr */
68 69
	Datum	   *values;			/* preallocated space for deformtuple */
	char	   *nulls;
70
} DR_printtup;
71 72 73 74 75

/* ----------------
 *		Initialize: create a DestReceiver for printtup
 * ----------------
 */
B
Bruce Momjian 已提交
76
DestReceiver *
77
printtup_create_DR(CommandDest dest, Portal portal)
78
{
B
Bruce Momjian 已提交
79
	DR_printtup *self = (DR_printtup *) palloc(sizeof(DR_printtup));
80

81 82 83
	if (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 3)
		self->pub.receiveTuple = printtup;
	else
84
	{
85
		/*
B
Bruce Momjian 已提交
86 87
		 * 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
88 89 90 91 92 93
		 * sufficient to look at the first one.
		 */
		if (portal->formats && portal->formats[0] != 0)
			self->pub.receiveTuple = printtup_internal_20;
		else
			self->pub.receiveTuple = printtup_20;
94
	}
95 96 97
	self->pub.rStartup = printtup_startup;
	self->pub.rShutdown = printtup_shutdown;
	self->pub.rDestroy = printtup_destroy;
98
	self->pub.mydest = dest;
99

100 101 102 103
	self->portal = portal;

	/* Send T message automatically if Remote, but not if RemoteExecute */
	self->sendDescrip = (dest == Remote);
104

105 106 107
	self->attrinfo = NULL;
	self->nattrs = 0;
	self->myinfo = NULL;
108 109
	self->values = NULL;
	self->nulls = NULL;
110

B
Bruce Momjian 已提交
111
	return (DestReceiver *) self;
112 113 114
}

static void
115
printtup_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
116
{
117
	DR_printtup *myState = (DR_printtup *) self;
B
Bruce Momjian 已提交
118
	Portal		portal = myState->portal;
119

120 121 122
	if (PG_PROTOCOL_MAJOR(FrontendProtocol) < 3)
	{
		/*
B
Bruce Momjian 已提交
123 124
		 * Send portal name to frontend (obsolete cruft, gone in proto
		 * 3.0)
125 126 127
		 *
		 * If portal name not specified, use "blank" portal.
		 */
128 129 130
		const char *portalName = portal->name;

		if (portalName == NULL || portalName[0] == '\0')
131 132 133 134
			portalName = "blank";

		pq_puttextmessage('P', portalName);
	}
135 136

	/*
B
Bruce Momjian 已提交
137 138
	 * If this is a retrieve, and we are supposed to emit row
	 * descriptions, then we send back the tuple descriptor of the tuples.
139
	 */
140
	if (operation == CMD_SELECT && myState->sendDescrip)
141 142 143 144
	{
		List	   *targetlist;

		if (portal->strategy == PORTAL_ONE_SELECT)
145
			targetlist = ((Query *) linitial(portal->parseTrees))->targetList;
146 147 148 149 150
		else
			targetlist = NIL;

		SendRowDescriptionMessage(typeinfo, targetlist, portal->formats);
	}
151

152 153
	/* ----------------
	 * We could set up the derived attr info at this time, but we postpone it
154
	 * until the first call of printtup, for 2 reasons:
155
	 * 1. We don't waste time (compared to the old way) if there are no
B
Bruce Momjian 已提交
156
	 *	  tuples at all to output.
157
	 * 2. Checking in printtup allows us to handle the case that the tuples
B
Bruce Momjian 已提交
158 159
	 *	  change type midway through (although this probably can't happen in
	 *	  the current executor).
160 161 162 163
	 * ----------------
	 */
}

164 165
/*
 * SendRowDescriptionMessage --- send a RowDescription message to the frontend
166 167 168 169
 *
 * 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
170
 * not have a plan.  If the targetlist isn't NIL then it is a Query node's
B
Bruce Momjian 已提交
171
 * targetlist; it is up to us to ignore resjunk columns in it.	The formats[]
172 173
 * array pointer might be NULL (if we are doing Describe on a prepared stmt);
 * send zeroes for the format codes in that case.
174 175
 */
void
176
SendRowDescriptionMessage(TupleDesc typeinfo, List *targetlist, int16 *formats)
177 178 179 180 181 182
{
	Form_pg_attribute *attrs = typeinfo->attrs;
	int			natts = typeinfo->natts;
	int			proto = PG_PROTOCOL_MAJOR(FrontendProtocol);
	int			i;
	StringInfoData buf;
183
	ListCell   *tlist_item = list_head(targetlist);
184

B
Bruce Momjian 已提交
185 186
	pq_beginmessage(&buf, 'T'); /* tuple descriptor message type */
	pq_sendint(&buf, natts, 2); /* # of attrs in tuples */
187 188 189

	for (i = 0; i < natts; ++i)
	{
B
Bruce Momjian 已提交
190 191 192
		Oid			atttypid = attrs[i]->atttypid;
		int32		atttypmod = attrs[i]->atttypmod;
		Oid			basetype;
193

194 195 196 197
		pq_sendstring(&buf, NameStr(attrs[i]->attname));
		/* column ID info appears in protocol 3.0 and up */
		if (proto >= 3)
		{
198
			/* Do we have a non-resjunk tlist item? */
199 200 201 202
			while (tlist_item &&
				   ((TargetEntry *) lfirst(tlist_item))->resdom->resjunk)
				tlist_item = lnext(tlist_item);
			if (tlist_item)
203
			{
204
				Resdom	   *res = ((TargetEntry *) lfirst(tlist_item))->resdom;
205 206 207

				pq_sendint(&buf, res->resorigtbl, 4);
				pq_sendint(&buf, res->resorigcol, 2);
208
				tlist_item = lnext(tlist_item);
209 210 211 212 213 214 215
			}
			else
			{
				/* No info available, so send zeroes */
				pq_sendint(&buf, 0, 4);
				pq_sendint(&buf, 0, 2);
			}
216
		}
217 218 219 220 221 222 223 224 225
		/* If column is a domain, send the base type and typmod instead */
		basetype = getBaseType(atttypid);
		if (basetype != atttypid)
		{
			atttypmod = get_typtypmod(atttypid);
			atttypid = basetype;
		}
		pq_sendint(&buf, (int) atttypid, sizeof(atttypid));
		pq_sendint(&buf, attrs[i]->attlen, sizeof(attrs[i]->attlen));
226 227
		/* typmod appears in protocol 2.0 and up */
		if (proto >= 2)
228
			pq_sendint(&buf, atttypmod, sizeof(atttypmod));
229 230 231 232 233 234 235 236
		/* 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);
		}
237 238 239 240
	}
	pq_endmessage(&buf);
}

241 242 243
/*
 * Get the lookup info that printtup() needs
 */
244
static void
245
printtup_prepare_info(DR_printtup *myState, TupleDesc typeinfo, int numAttrs)
246
{
247
	int16	   *formats = myState->portal->formats;
B
Bruce Momjian 已提交
248
	int			i;
249

250
	/* get rid of any old data */
251
	if (myState->myinfo)
252
		pfree(myState->myinfo);
253
	myState->myinfo = NULL;
254 255 256 257 258 259 260
	if (myState->values)
		pfree(myState->values);
	myState->values = NULL;
	if (myState->nulls)
		pfree(myState->nulls);
	myState->nulls = NULL;

261 262 263 264
	myState->attrinfo = typeinfo;
	myState->nattrs = numAttrs;
	if (numAttrs <= 0)
		return;
265

B
Bruce Momjian 已提交
266
	myState->myinfo = (PrinttupAttrInfo *)
267
		palloc0(numAttrs * sizeof(PrinttupAttrInfo));
268 269 270
	myState->values = (Datum *) palloc(numAttrs * sizeof(Datum));
	myState->nulls = (char *) palloc(numAttrs * sizeof(char));

271 272
	for (i = 0; i < numAttrs; i++)
	{
B
Bruce Momjian 已提交
273
		PrinttupAttrInfo *thisState = myState->myinfo + i;
274
		int16		format = (formats ? formats[i] : 0);
B
Bruce Momjian 已提交
275

276 277 278 279 280
		thisState->format = format;
		if (format == 0)
		{
			getTypeOutputInfo(typeinfo->attrs[i]->atttypid,
							  &thisState->typoutput,
281
							  &thisState->typioparam,
282
							  &thisState->typisvarlena);
283
			fmgr_info(thisState->typoutput, &thisState->finfo);
284 285 286 287 288
		}
		else if (format == 1)
		{
			getTypeBinaryOutputInfo(typeinfo->attrs[i]->atttypid,
									&thisState->typsend,
289
									&thisState->typioparam,
290 291 292 293
									&thisState->typisvarlena);
			fmgr_info(thisState->typsend, &thisState->finfo);
		}
		else
294 295 296
			ereport(ERROR,
					(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
					 errmsg("unsupported format code: %d", format)));
297 298 299
	}
}

300
/* ----------------
301
 *		printtup --- print a tuple in protocol 3.0
302 303
 * ----------------
 */
304
static void
305
printtup(HeapTuple tuple, TupleDesc typeinfo, DestReceiver *self)
306 307 308
{
	DR_printtup *myState = (DR_printtup *) self;
	StringInfoData buf;
309
	int			natts = typeinfo->natts;
310 311 312 313 314 315
	int			i;

	/* Set or update my derived attribute info, if needed */
	if (myState->attrinfo != typeinfo || myState->nattrs != natts)
		printtup_prepare_info(myState, typeinfo, natts);

316 317 318 319 320
	/*
	 * deconstruct the tuple (faster than a heap_getattr loop)
	 */
	heap_deformtuple(tuple, typeinfo, myState->values, myState->nulls);

321 322 323 324 325 326 327 328 329 330 331 332 333
	/*
	 * Prepare a DataRow message
	 */
	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;
334
		Datum		origattr = myState->values[i],
335 336
					attr;

337
		if (myState->nulls[i] == 'n')
338 339 340 341 342
		{
			pq_sendint(&buf, -1, 4);
			continue;
		}

343
		/*
B
Bruce Momjian 已提交
344 345
		 * If we have a toasted datum, forcibly detoast it here to avoid
		 * memory leakage inside the type's output routine.
346 347 348 349 350
		 */
		if (thisState->typisvarlena)
			attr = PointerGetDatum(PG_DETOAST_DATUM(origattr));
		else
			attr = origattr;
351

352
		if (thisState->format == 0)
353
		{
354 355 356 357 358
			/* Text output */
			char	   *outputstr;

			outputstr = DatumGetCString(FunctionCall3(&thisState->finfo,
													  attr,
B
Bruce Momjian 已提交
359
								 ObjectIdGetDatum(thisState->typioparam),
360 361 362
						  Int32GetDatum(typeinfo->attrs[i]->atttypmod)));
			pq_sendcountedtext(&buf, outputstr, strlen(outputstr), false);
			pfree(outputstr);
363 364 365
		}
		else
		{
366 367 368 369 370
			/* Binary output */
			bytea	   *outputbytes;

			outputbytes = DatumGetByteaP(FunctionCall2(&thisState->finfo,
													   attr,
B
Bruce Momjian 已提交
371
							   ObjectIdGetDatum(thisState->typioparam)));
372 373 374 375 376
			/* 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);
377
		}
378 379 380 381

		/* Clean up detoasted copy, if any */
		if (attr != origattr)
			pfree(DatumGetPointer(attr));
382 383 384 385 386 387 388 389 390 391 392
	}

	pq_endmessage(&buf);
}

/* ----------------
 *		printtup_20 --- print a tuple in protocol 2.0
 * ----------------
 */
static void
printtup_20(HeapTuple tuple, TupleDesc typeinfo, DestReceiver *self)
393
{
B
Bruce Momjian 已提交
394
	DR_printtup *myState = (DR_printtup *) self;
395
	StringInfoData buf;
396
	int			natts = typeinfo->natts;
397 398
	int			i,
				j,
399
				k;
400

401
	/* Set or update my derived attribute info, if needed */
402 403
	if (myState->attrinfo != typeinfo || myState->nattrs != natts)
		printtup_prepare_info(myState, typeinfo, natts);
404

405 406 407 408 409
	/*
	 * deconstruct the tuple (faster than a heap_getattr loop)
	 */
	heap_deformtuple(tuple, typeinfo, myState->values, myState->nulls);

410 411
	/*
	 * tell the frontend to expect new tuple data (in ASCII style)
412
	 */
413
	pq_beginmessage(&buf, 'D');
414

415 416
	/*
	 * send a bitmap of which attributes are not null
417 418 419
	 */
	j = 0;
	k = 1 << 7;
420
	for (i = 0; i < natts; ++i)
421
	{
422
		if (myState->nulls[i] != 'n')
423
			j |= k;				/* set bit if not null */
424
		k >>= 1;
425
		if (k == 0)				/* end of byte? */
426
		{
427
			pq_sendint(&buf, j, 1);
428 429 430
			j = 0;
			k = 1 << 7;
		}
431
	}
432
	if (k != (1 << 7))			/* flush last partial byte */
433
		pq_sendint(&buf, j, 1);
434

435 436
	/*
	 * send the attributes of this tuple
437
	 */
438
	for (i = 0; i < natts; ++i)
439
	{
B
Bruce Momjian 已提交
440
		PrinttupAttrInfo *thisState = myState->myinfo + i;
441
		Datum		origattr = myState->values[i],
442 443
					attr;
		char	   *outputstr;
B
Bruce Momjian 已提交
444

445
		if (myState->nulls[i] == 'n')
M
 
Marc G. Fournier 已提交
446
			continue;
447

448 449 450
		Assert(thisState->format == 0);

		/*
B
Bruce Momjian 已提交
451 452
		 * If we have a toasted datum, forcibly detoast it here to avoid
		 * memory leakage inside the type's output routine.
453 454 455 456 457 458 459 460
		 */
		if (thisState->typisvarlena)
			attr = PointerGetDatum(PG_DETOAST_DATUM(origattr));
		else
			attr = origattr;

		outputstr = DatumGetCString(FunctionCall3(&thisState->finfo,
												  attr,
B
Bruce Momjian 已提交
461
								 ObjectIdGetDatum(thisState->typioparam),
B
Bruce Momjian 已提交
462
						  Int32GetDatum(typeinfo->attrs[i]->atttypmod)));
463 464
		pq_sendcountedtext(&buf, outputstr, strlen(outputstr), true);
		pfree(outputstr);
465

466 467 468
		/* Clean up detoasted copy, if any */
		if (attr != origattr)
			pfree(DatumGetPointer(attr));
469
	}
470 471

	pq_endmessage(&buf);
472 473
}

474
/* ----------------
475
 *		printtup_shutdown
476 477 478
 * ----------------
 */
static void
479
printtup_shutdown(DestReceiver *self)
480
{
B
Bruce Momjian 已提交
481 482
	DR_printtup *myState = (DR_printtup *) self;

483 484
	if (myState->myinfo)
		pfree(myState->myinfo);
485
	myState->myinfo = NULL;
486 487 488 489 490 491 492
	if (myState->values)
		pfree(myState->values);
	myState->values = NULL;
	if (myState->nulls)
		pfree(myState->nulls);
	myState->nulls = NULL;

493 494 495 496 497 498 499 500 501 502 503
	myState->attrinfo = NULL;
}

/* ----------------
 *		printtup_destroy
 * ----------------
 */
static void
printtup_destroy(DestReceiver *self)
{
	pfree(self);
504 505
}

506
/* ----------------
507
 *		printatt
508 509 510 511
 * ----------------
 */
static void
printatt(unsigned attributeId,
512
		 Form_pg_attribute attributeP,
513
		 char *value)
514
{
B
Bruce Momjian 已提交
515
	printf("\t%2d: %s%s%s%s\t(typeid = %u, len = %d, typmod = %d, byval = %c)\n",
516
		   attributeId,
517
		   NameStr(attributeP->attname),
518 519 520 521 522
		   value != NULL ? " = \"" : "",
		   value != NULL ? value : "",
		   value != NULL ? "\"" : "",
		   (unsigned int) (attributeP->atttypid),
		   attributeP->attlen,
B
Bruce Momjian 已提交
523
		   attributeP->atttypmod,
524
		   attributeP->attbyval ? 't' : 'f');
525 526 527
}

/* ----------------
528
 *		debugStartup - prepare to print tuples for an interactive backend
529 530 531
 * ----------------
 */
void
532
debugStartup(DestReceiver *self, int operation, TupleDesc typeinfo)
533
{
534 535 536 537
	int			natts = typeinfo->natts;
	Form_pg_attribute *attinfo = typeinfo->attrs;
	int			i;

538 539 540
	/*
	 * show the return type of the tuples
	 */
541
	for (i = 0; i < natts; ++i)
542
		printatt((unsigned) i + 1, attinfo[i], NULL);
543
	printf("\t----\n");
544 545 546 547
}

/* ----------------
 *		debugtup - print one tuple for an interactive backend
548 549 550
 * ----------------
 */
void
551
debugtup(HeapTuple tuple, TupleDesc typeinfo, DestReceiver *self)
552
{
553
	int			natts = typeinfo->natts;
554
	int			i;
555 556
	Datum		origattr,
				attr;
557
	char	   *value;
558
	bool		isnull;
559
	Oid			typoutput,
560
				typioparam;
561
	bool		typisvarlena;
562

563
	for (i = 0; i < natts; ++i)
564
	{
565
		origattr = heap_getattr(tuple, i + 1, typeinfo, &isnull);
566 567
		if (isnull)
			continue;
568
		getTypeOutputInfo(typeinfo->attrs[i]->atttypid,
569
						  &typoutput, &typioparam, &typisvarlena);
B
Bruce Momjian 已提交
570

571
		/*
B
Bruce Momjian 已提交
572 573
		 * If we have a toasted datum, forcibly detoast it here to avoid
		 * memory leakage inside the type's output routine.
574 575 576 577 578
		 */
		if (typisvarlena)
			attr = PointerGetDatum(PG_DETOAST_DATUM(origattr));
		else
			attr = origattr;
579

580 581
		value = DatumGetCString(OidFunctionCall3(typoutput,
												 attr,
B
Bruce Momjian 已提交
582
											ObjectIdGetDatum(typioparam),
B
Bruce Momjian 已提交
583
						  Int32GetDatum(typeinfo->attrs[i]->atttypmod)));
584

585
		printatt((unsigned) i + 1, typeinfo->attrs[i], value);
586

587 588 589 590 591
		pfree(value);

		/* Clean up detoasted copy, if any */
		if (attr != origattr)
			pfree(DatumGetPointer(attr));
592
	}
593
	printf("\t----\n");
594 595 596
}

/* ----------------
597 598 599 600
 *		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.
601
 *
602
 * This is largely same as printtup_20, except we use binary formatting.
603 604
 * ----------------
 */
605
static void
606
printtup_internal_20(HeapTuple tuple, TupleDesc typeinfo, DestReceiver *self)
607
{
608
	DR_printtup *myState = (DR_printtup *) self;
609
	StringInfoData buf;
610
	int			natts = typeinfo->natts;
611 612 613
	int			i,
				j,
				k;
614 615 616 617

	/* Set or update my derived attribute info, if needed */
	if (myState->attrinfo != typeinfo || myState->nattrs != natts)
		printtup_prepare_info(myState, typeinfo, natts);
618

619 620 621 622 623
	/*
	 * deconstruct the tuple (faster than a heap_getattr loop)
	 */
	heap_deformtuple(tuple, typeinfo, myState->values, myState->nulls);

624 625
	/*
	 * tell the frontend to expect new tuple data (in binary style)
626
	 */
627
	pq_beginmessage(&buf, 'B');
628

629 630
	/*
	 * send a bitmap of which attributes are not null
631 632 633
	 */
	j = 0;
	k = 1 << 7;
634
	for (i = 0; i < natts; ++i)
635
	{
636
		if (myState->nulls[i] != 'n')
637
			j |= k;				/* set bit if not null */
638
		k >>= 1;
639
		if (k == 0)				/* end of byte? */
640
		{
641
			pq_sendint(&buf, j, 1);
642 643 644
			j = 0;
			k = 1 << 7;
		}
645
	}
646
	if (k != (1 << 7))			/* flush last partial byte */
647
		pq_sendint(&buf, j, 1);
648

649 650
	/*
	 * send the attributes of this tuple
651
	 */
652
	for (i = 0; i < natts; ++i)
653
	{
654
		PrinttupAttrInfo *thisState = myState->myinfo + i;
655
		Datum		origattr = myState->values[i],
656
					attr;
657
		bytea	   *outputbytes;
658

659
		if (myState->nulls[i] == 'n')
660
			continue;
661

662
		Assert(thisState->format == 1);
663

664
		/*
B
Bruce Momjian 已提交
665 666
		 * If we have a toasted datum, forcibly detoast it here to avoid
		 * memory leakage inside the type's output routine.
667 668 669
		 */
		if (thisState->typisvarlena)
			attr = PointerGetDatum(PG_DETOAST_DATUM(origattr));
670 671
		else
			attr = origattr;
672 673 674

		outputbytes = DatumGetByteaP(FunctionCall2(&thisState->finfo,
												   attr,
B
Bruce Momjian 已提交
675
							   ObjectIdGetDatum(thisState->typioparam)));
676 677 678 679 680 681 682 683 684
		/* 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));
685
	}
686 687

	pq_endmessage(&buf);
688
}