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  *
8  * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
9  * Portions Copyright (c) 1994, Regents of the University of California
10  *
11  * IDENTIFICATION
12  *	  src/backend/access/common/printtup.c
13  *
14  *-------------------------------------------------------------------------
15  */
16 #include "postgres.h"
17 
18 #include "access/printtup.h"
19 #include "libpq/libpq.h"
20 #include "libpq/pqformat.h"
21 #include "tcop/pquery.h"
22 #include "utils/lsyscache.h"
23 #include "utils/memdebug.h"
24 #include "utils/memutils.h"
25 
26 
27 static void printtup_startup(DestReceiver *self, int operation,
28 				 TupleDesc typeinfo);
29 static bool printtup(TupleTableSlot *slot, DestReceiver *self);
30 static bool printtup_20(TupleTableSlot *slot, DestReceiver *self);
31 static bool printtup_internal_20(TupleTableSlot *slot, DestReceiver *self);
32 static void printtup_shutdown(DestReceiver *self);
33 static void printtup_destroy(DestReceiver *self);
34 
35 
36 /* ----------------------------------------------------------------
37  *		printtup / debugtup support
38  * ----------------------------------------------------------------
39  */
40 
41 /* ----------------
42  *		Private state for a printtup destination object
43  *
44  * NOTE: finfo is the lookup info for either typoutput or typsend, whichever
45  * we are using for this column.
46  * ----------------
47  */
48 typedef struct
49 {								/* Per-attribute information */
50 	Oid			typoutput;		/* Oid for the type's text output fn */
51 	Oid			typsend;		/* Oid for the type's binary output fn */
52 	bool		typisvarlena;	/* is it varlena (ie possibly toastable)? */
53 	int16		format;			/* format code for this column */
54 	FmgrInfo	finfo;			/* Precomputed call info for output fn */
55 } PrinttupAttrInfo;
56 
57 typedef struct
58 {
59 	DestReceiver pub;			/* publicly-known function pointers */
60 	Portal		portal;			/* the Portal we are printing from */
61 	bool		sendDescrip;	/* send RowDescription at startup? */
62 	TupleDesc	attrinfo;		/* The attr info we are set up for */
63 	int			nattrs;
64 	PrinttupAttrInfo *myinfo;	/* Cached info about each attr */
65 	MemoryContext tmpcontext;	/* Memory context for per-row workspace */
66 } DR_printtup;
67 
68 /* ----------------
69  *		Initialize: create a DestReceiver for printtup
70  * ----------------
71  */
72 DestReceiver *
printtup_create_DR(CommandDest dest)73 printtup_create_DR(CommandDest dest)
74 {
75 	DR_printtup *self = (DR_printtup *) palloc0(sizeof(DR_printtup));
76 
77 	self->pub.receiveSlot = printtup;	/* might get changed later */
78 	self->pub.rStartup = printtup_startup;
79 	self->pub.rShutdown = printtup_shutdown;
80 	self->pub.rDestroy = printtup_destroy;
81 	self->pub.mydest = dest;
82 
83 	/*
84 	 * Send T message automatically if DestRemote, but not if
85 	 * DestRemoteExecute
86 	 */
87 	self->sendDescrip = (dest == DestRemote);
88 
89 	self->attrinfo = NULL;
90 	self->nattrs = 0;
91 	self->myinfo = NULL;
92 	self->tmpcontext = NULL;
93 
94 	return (DestReceiver *) self;
95 }
96 
97 /*
98  * Set parameters for a DestRemote (or DestRemoteExecute) receiver
99  */
100 void
SetRemoteDestReceiverParams(DestReceiver * self,Portal portal)101 SetRemoteDestReceiverParams(DestReceiver *self, Portal portal)
102 {
103 	DR_printtup *myState = (DR_printtup *) self;
104 
105 	Assert(myState->pub.mydest == DestRemote ||
106 		   myState->pub.mydest == DestRemoteExecute);
107 
108 	myState->portal = portal;
109 
110 	if (PG_PROTOCOL_MAJOR(FrontendProtocol) < 3)
111 	{
112 		/*
113 		 * In protocol 2.0 the Bind message does not exist, so there is no way
114 		 * for the columns to have different print formats; it's sufficient to
115 		 * look at the first one.
116 		 */
117 		if (portal->formats && portal->formats[0] != 0)
118 			myState->pub.receiveSlot = printtup_internal_20;
119 		else
120 			myState->pub.receiveSlot = printtup_20;
121 	}
122 }
123 
124 static void
printtup_startup(DestReceiver * self,int operation,TupleDesc typeinfo)125 printtup_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
126 {
127 	DR_printtup *myState = (DR_printtup *) self;
128 	Portal		portal = myState->portal;
129 
130 	/*
131 	 * Create a temporary memory context that we can reset once per row to
132 	 * recover palloc'd memory.  This avoids any problems with leaks inside
133 	 * datatype output routines, and should be faster than retail pfree's
134 	 * anyway.
135 	 */
136 	myState->tmpcontext = AllocSetContextCreate(CurrentMemoryContext,
137 												"printtup",
138 												ALLOCSET_DEFAULT_SIZES);
139 
140 	if (PG_PROTOCOL_MAJOR(FrontendProtocol) < 3)
141 	{
142 		/*
143 		 * Send portal name to frontend (obsolete cruft, gone in proto 3.0)
144 		 *
145 		 * If portal name not specified, use "blank" portal.
146 		 */
147 		const char *portalName = portal->name;
148 
149 		if (portalName == NULL || portalName[0] == '\0')
150 			portalName = "blank";
151 
152 		pq_puttextmessage('P', portalName);
153 	}
154 
155 	/*
156 	 * If we are supposed to emit row descriptions, then send the tuple
157 	 * descriptor of the tuples.
158 	 */
159 	if (myState->sendDescrip)
160 		SendRowDescriptionMessage(typeinfo,
161 								  FetchPortalTargetList(portal),
162 								  portal->formats);
163 
164 	/* ----------------
165 	 * We could set up the derived attr info at this time, but we postpone it
166 	 * until the first call of printtup, for 2 reasons:
167 	 * 1. We don't waste time (compared to the old way) if there are no
168 	 *	  tuples at all to output.
169 	 * 2. Checking in printtup allows us to handle the case that the tuples
170 	 *	  change type midway through (although this probably can't happen in
171 	 *	  the current executor).
172 	 * ----------------
173 	 */
174 }
175 
176 /*
177  * SendRowDescriptionMessage --- send a RowDescription message to the frontend
178  *
179  * Notes: the TupleDesc has typically been manufactured by ExecTypeFromTL()
180  * or some similar function; it does not contain a full set of fields.
181  * The targetlist will be NIL when executing a utility function that does
182  * not have a plan.  If the targetlist isn't NIL then it is a Query node's
183  * targetlist; it is up to us to ignore resjunk columns in it.  The formats[]
184  * array pointer might be NULL (if we are doing Describe on a prepared stmt);
185  * send zeroes for the format codes in that case.
186  */
187 void
SendRowDescriptionMessage(TupleDesc typeinfo,List * targetlist,int16 * formats)188 SendRowDescriptionMessage(TupleDesc typeinfo, List *targetlist, int16 *formats)
189 {
190 	Form_pg_attribute *attrs = typeinfo->attrs;
191 	int			natts = typeinfo->natts;
192 	int			proto = PG_PROTOCOL_MAJOR(FrontendProtocol);
193 	int			i;
194 	StringInfoData buf;
195 	ListCell   *tlist_item = list_head(targetlist);
196 
197 	pq_beginmessage(&buf, 'T'); /* tuple descriptor message type */
198 	pq_sendint(&buf, natts, 2); /* # of attrs in tuples */
199 
200 	for (i = 0; i < natts; ++i)
201 	{
202 		Oid			atttypid = attrs[i]->atttypid;
203 		int32		atttypmod = attrs[i]->atttypmod;
204 
205 		pq_sendstring(&buf, NameStr(attrs[i]->attname));
206 		/* column ID info appears in protocol 3.0 and up */
207 		if (proto >= 3)
208 		{
209 			/* Do we have a non-resjunk tlist item? */
210 			while (tlist_item &&
211 				   ((TargetEntry *) lfirst(tlist_item))->resjunk)
212 				tlist_item = lnext(tlist_item);
213 			if (tlist_item)
214 			{
215 				TargetEntry *tle = (TargetEntry *) lfirst(tlist_item);
216 
217 				pq_sendint(&buf, tle->resorigtbl, 4);
218 				pq_sendint(&buf, tle->resorigcol, 2);
219 				tlist_item = lnext(tlist_item);
220 			}
221 			else
222 			{
223 				/* No info available, so send zeroes */
224 				pq_sendint(&buf, 0, 4);
225 				pq_sendint(&buf, 0, 2);
226 			}
227 		}
228 		/* If column is a domain, send the base type and typmod instead */
229 		atttypid = getBaseTypeAndTypmod(atttypid, &atttypmod);
230 		pq_sendint(&buf, (int) atttypid, sizeof(atttypid));
231 		pq_sendint(&buf, attrs[i]->attlen, sizeof(attrs[i]->attlen));
232 		/* typmod appears in protocol 2.0 and up */
233 		if (proto >= 2)
234 			pq_sendint(&buf, atttypmod, sizeof(atttypmod));
235 		/* format info appears in protocol 3.0 and up */
236 		if (proto >= 3)
237 		{
238 			if (formats)
239 				pq_sendint(&buf, formats[i], 2);
240 			else
241 				pq_sendint(&buf, 0, 2);
242 		}
243 	}
244 	pq_endmessage(&buf);
245 }
246 
247 /*
248  * Get the lookup info that printtup() needs
249  */
250 static void
printtup_prepare_info(DR_printtup * myState,TupleDesc typeinfo,int numAttrs)251 printtup_prepare_info(DR_printtup *myState, TupleDesc typeinfo, int numAttrs)
252 {
253 	int16	   *formats = myState->portal->formats;
254 	int			i;
255 
256 	/* get rid of any old data */
257 	if (myState->myinfo)
258 		pfree(myState->myinfo);
259 	myState->myinfo = NULL;
260 
261 	myState->attrinfo = typeinfo;
262 	myState->nattrs = numAttrs;
263 	if (numAttrs <= 0)
264 		return;
265 
266 	myState->myinfo = (PrinttupAttrInfo *)
267 		palloc0(numAttrs * sizeof(PrinttupAttrInfo));
268 
269 	for (i = 0; i < numAttrs; i++)
270 	{
271 		PrinttupAttrInfo *thisState = myState->myinfo + i;
272 		int16		format = (formats ? formats[i] : 0);
273 
274 		thisState->format = format;
275 		if (format == 0)
276 		{
277 			getTypeOutputInfo(typeinfo->attrs[i]->atttypid,
278 							  &thisState->typoutput,
279 							  &thisState->typisvarlena);
280 			fmgr_info(thisState->typoutput, &thisState->finfo);
281 		}
282 		else if (format == 1)
283 		{
284 			getTypeBinaryOutputInfo(typeinfo->attrs[i]->atttypid,
285 									&thisState->typsend,
286 									&thisState->typisvarlena);
287 			fmgr_info(thisState->typsend, &thisState->finfo);
288 		}
289 		else
290 			ereport(ERROR,
291 					(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
292 					 errmsg("unsupported format code: %d", format)));
293 	}
294 }
295 
296 /* ----------------
297  *		printtup --- print a tuple in protocol 3.0
298  * ----------------
299  */
300 static bool
printtup(TupleTableSlot * slot,DestReceiver * self)301 printtup(TupleTableSlot *slot, DestReceiver *self)
302 {
303 	TupleDesc	typeinfo = slot->tts_tupleDescriptor;
304 	DR_printtup *myState = (DR_printtup *) self;
305 	MemoryContext oldcontext;
306 	StringInfoData buf;
307 	int			natts = typeinfo->natts;
308 	int			i;
309 
310 	/* Set or update my derived attribute info, if needed */
311 	if (myState->attrinfo != typeinfo || myState->nattrs != natts)
312 		printtup_prepare_info(myState, typeinfo, natts);
313 
314 	/* Make sure the tuple is fully deconstructed */
315 	slot_getallattrs(slot);
316 
317 	/* Switch into per-row context so we can recover memory below */
318 	oldcontext = MemoryContextSwitchTo(myState->tmpcontext);
319 
320 	/*
321 	 * Prepare a DataRow message (note buffer is in per-row context)
322 	 */
323 	pq_beginmessage(&buf, 'D');
324 
325 	pq_sendint(&buf, natts, 2);
326 
327 	/*
328 	 * send the attributes of this tuple
329 	 */
330 	for (i = 0; i < natts; ++i)
331 	{
332 		PrinttupAttrInfo *thisState = myState->myinfo + i;
333 		Datum		attr = slot->tts_values[i];
334 
335 		if (slot->tts_isnull[i])
336 		{
337 			pq_sendint(&buf, -1, 4);
338 			continue;
339 		}
340 
341 		/*
342 		 * Here we catch undefined bytes in datums that are returned to the
343 		 * client without hitting disk; see comments at the related check in
344 		 * PageAddItem().  This test is most useful for uncompressed,
345 		 * non-external datums, but we're quite likely to see such here when
346 		 * testing new C functions.
347 		 */
348 		if (thisState->typisvarlena)
349 			VALGRIND_CHECK_MEM_IS_DEFINED(DatumGetPointer(attr),
350 										  VARSIZE_ANY(attr));
351 
352 		if (thisState->format == 0)
353 		{
354 			/* Text output */
355 			char	   *outputstr;
356 
357 			outputstr = OutputFunctionCall(&thisState->finfo, attr);
358 			pq_sendcountedtext(&buf, outputstr, strlen(outputstr), false);
359 		}
360 		else
361 		{
362 			/* Binary output */
363 			bytea	   *outputbytes;
364 
365 			outputbytes = SendFunctionCall(&thisState->finfo, attr);
366 			pq_sendint(&buf, VARSIZE(outputbytes) - VARHDRSZ, 4);
367 			pq_sendbytes(&buf, VARDATA(outputbytes),
368 						 VARSIZE(outputbytes) - VARHDRSZ);
369 		}
370 	}
371 
372 	pq_endmessage(&buf);
373 
374 	/* Return to caller's context, and flush row's temporary memory */
375 	MemoryContextSwitchTo(oldcontext);
376 	MemoryContextReset(myState->tmpcontext);
377 
378 	return true;
379 }
380 
381 /* ----------------
382  *		printtup_20 --- print a tuple in protocol 2.0
383  * ----------------
384  */
385 static bool
printtup_20(TupleTableSlot * slot,DestReceiver * self)386 printtup_20(TupleTableSlot *slot, DestReceiver *self)
387 {
388 	TupleDesc	typeinfo = slot->tts_tupleDescriptor;
389 	DR_printtup *myState = (DR_printtup *) self;
390 	MemoryContext oldcontext;
391 	StringInfoData buf;
392 	int			natts = typeinfo->natts;
393 	int			i,
394 				j,
395 				k;
396 
397 	/* Set or update my derived attribute info, if needed */
398 	if (myState->attrinfo != typeinfo || myState->nattrs != natts)
399 		printtup_prepare_info(myState, typeinfo, natts);
400 
401 	/* Make sure the tuple is fully deconstructed */
402 	slot_getallattrs(slot);
403 
404 	/* Switch into per-row context so we can recover memory below */
405 	oldcontext = MemoryContextSwitchTo(myState->tmpcontext);
406 
407 	/*
408 	 * tell the frontend to expect new tuple data (in ASCII style)
409 	 */
410 	pq_beginmessage(&buf, 'D');
411 
412 	/*
413 	 * send a bitmap of which attributes are not null
414 	 */
415 	j = 0;
416 	k = 1 << 7;
417 	for (i = 0; i < natts; ++i)
418 	{
419 		if (!slot->tts_isnull[i])
420 			j |= k;				/* set bit if not null */
421 		k >>= 1;
422 		if (k == 0)				/* end of byte? */
423 		{
424 			pq_sendint(&buf, j, 1);
425 			j = 0;
426 			k = 1 << 7;
427 		}
428 	}
429 	if (k != (1 << 7))			/* flush last partial byte */
430 		pq_sendint(&buf, j, 1);
431 
432 	/*
433 	 * send the attributes of this tuple
434 	 */
435 	for (i = 0; i < natts; ++i)
436 	{
437 		PrinttupAttrInfo *thisState = myState->myinfo + i;
438 		Datum		attr = slot->tts_values[i];
439 		char	   *outputstr;
440 
441 		if (slot->tts_isnull[i])
442 			continue;
443 
444 		Assert(thisState->format == 0);
445 
446 		outputstr = OutputFunctionCall(&thisState->finfo, attr);
447 		pq_sendcountedtext(&buf, outputstr, strlen(outputstr), true);
448 	}
449 
450 	pq_endmessage(&buf);
451 
452 	/* Return to caller's context, and flush row's temporary memory */
453 	MemoryContextSwitchTo(oldcontext);
454 	MemoryContextReset(myState->tmpcontext);
455 
456 	return true;
457 }
458 
459 /* ----------------
460  *		printtup_shutdown
461  * ----------------
462  */
463 static void
printtup_shutdown(DestReceiver * self)464 printtup_shutdown(DestReceiver *self)
465 {
466 	DR_printtup *myState = (DR_printtup *) self;
467 
468 	if (myState->myinfo)
469 		pfree(myState->myinfo);
470 	myState->myinfo = NULL;
471 
472 	myState->attrinfo = NULL;
473 
474 	if (myState->tmpcontext)
475 		MemoryContextDelete(myState->tmpcontext);
476 	myState->tmpcontext = NULL;
477 }
478 
479 /* ----------------
480  *		printtup_destroy
481  * ----------------
482  */
483 static void
printtup_destroy(DestReceiver * self)484 printtup_destroy(DestReceiver *self)
485 {
486 	pfree(self);
487 }
488 
489 /* ----------------
490  *		printatt
491  * ----------------
492  */
493 static void
printatt(unsigned attributeId,Form_pg_attribute attributeP,char * value)494 printatt(unsigned attributeId,
495 		 Form_pg_attribute attributeP,
496 		 char *value)
497 {
498 	printf("\t%2d: %s%s%s%s\t(typeid = %u, len = %d, typmod = %d, byval = %c)\n",
499 		   attributeId,
500 		   NameStr(attributeP->attname),
501 		   value != NULL ? " = \"" : "",
502 		   value != NULL ? value : "",
503 		   value != NULL ? "\"" : "",
504 		   (unsigned int) (attributeP->atttypid),
505 		   attributeP->attlen,
506 		   attributeP->atttypmod,
507 		   attributeP->attbyval ? 't' : 'f');
508 }
509 
510 /* ----------------
511  *		debugStartup - prepare to print tuples for an interactive backend
512  * ----------------
513  */
514 void
debugStartup(DestReceiver * self,int operation,TupleDesc typeinfo)515 debugStartup(DestReceiver *self, int operation, TupleDesc typeinfo)
516 {
517 	int			natts = typeinfo->natts;
518 	Form_pg_attribute *attinfo = typeinfo->attrs;
519 	int			i;
520 
521 	/*
522 	 * show the return type of the tuples
523 	 */
524 	for (i = 0; i < natts; ++i)
525 		printatt((unsigned) i + 1, attinfo[i], NULL);
526 	printf("\t----\n");
527 }
528 
529 /* ----------------
530  *		debugtup - print one tuple for an interactive backend
531  * ----------------
532  */
533 bool
debugtup(TupleTableSlot * slot,DestReceiver * self)534 debugtup(TupleTableSlot *slot, DestReceiver *self)
535 {
536 	TupleDesc	typeinfo = slot->tts_tupleDescriptor;
537 	int			natts = typeinfo->natts;
538 	int			i;
539 	Datum		attr;
540 	char	   *value;
541 	bool		isnull;
542 	Oid			typoutput;
543 	bool		typisvarlena;
544 
545 	for (i = 0; i < natts; ++i)
546 	{
547 		attr = slot_getattr(slot, i + 1, &isnull);
548 		if (isnull)
549 			continue;
550 		getTypeOutputInfo(typeinfo->attrs[i]->atttypid,
551 						  &typoutput, &typisvarlena);
552 
553 		value = OidOutputFunctionCall(typoutput, attr);
554 
555 		printatt((unsigned) i + 1, typeinfo->attrs[i], value);
556 	}
557 	printf("\t----\n");
558 
559 	return true;
560 }
561 
562 /* ----------------
563  *		printtup_internal_20 --- print a binary tuple in protocol 2.0
564  *
565  * We use a different message type, i.e. 'B' instead of 'D' to
566  * indicate a tuple in internal (binary) form.
567  *
568  * This is largely same as printtup_20, except we use binary formatting.
569  * ----------------
570  */
571 static bool
printtup_internal_20(TupleTableSlot * slot,DestReceiver * self)572 printtup_internal_20(TupleTableSlot *slot, DestReceiver *self)
573 {
574 	TupleDesc	typeinfo = slot->tts_tupleDescriptor;
575 	DR_printtup *myState = (DR_printtup *) self;
576 	MemoryContext oldcontext;
577 	StringInfoData buf;
578 	int			natts = typeinfo->natts;
579 	int			i,
580 				j,
581 				k;
582 
583 	/* Set or update my derived attribute info, if needed */
584 	if (myState->attrinfo != typeinfo || myState->nattrs != natts)
585 		printtup_prepare_info(myState, typeinfo, natts);
586 
587 	/* Make sure the tuple is fully deconstructed */
588 	slot_getallattrs(slot);
589 
590 	/* Switch into per-row context so we can recover memory below */
591 	oldcontext = MemoryContextSwitchTo(myState->tmpcontext);
592 
593 	/*
594 	 * tell the frontend to expect new tuple data (in binary style)
595 	 */
596 	pq_beginmessage(&buf, 'B');
597 
598 	/*
599 	 * send a bitmap of which attributes are not null
600 	 */
601 	j = 0;
602 	k = 1 << 7;
603 	for (i = 0; i < natts; ++i)
604 	{
605 		if (!slot->tts_isnull[i])
606 			j |= k;				/* set bit if not null */
607 		k >>= 1;
608 		if (k == 0)				/* end of byte? */
609 		{
610 			pq_sendint(&buf, j, 1);
611 			j = 0;
612 			k = 1 << 7;
613 		}
614 	}
615 	if (k != (1 << 7))			/* flush last partial byte */
616 		pq_sendint(&buf, j, 1);
617 
618 	/*
619 	 * send the attributes of this tuple
620 	 */
621 	for (i = 0; i < natts; ++i)
622 	{
623 		PrinttupAttrInfo *thisState = myState->myinfo + i;
624 		Datum		attr = slot->tts_values[i];
625 		bytea	   *outputbytes;
626 
627 		if (slot->tts_isnull[i])
628 			continue;
629 
630 		Assert(thisState->format == 1);
631 
632 		outputbytes = SendFunctionCall(&thisState->finfo, attr);
633 		pq_sendint(&buf, VARSIZE(outputbytes) - VARHDRSZ, 4);
634 		pq_sendbytes(&buf, VARDATA(outputbytes),
635 					 VARSIZE(outputbytes) - VARHDRSZ);
636 	}
637 
638 	pq_endmessage(&buf);
639 
640 	/* Return to caller's context, and flush row's temporary memory */
641 	MemoryContextSwitchTo(oldcontext);
642 	MemoryContextReset(myState->tmpcontext);
643 
644 	return true;
645 }
646