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-2017, 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 		pq_sendint(&buf, atttypmod, sizeof(atttypmod));
233 		/* format info appears in protocol 3.0 and up */
234 		if (proto >= 3)
235 		{
236 			if (formats)
237 				pq_sendint(&buf, formats[i], 2);
238 			else
239 				pq_sendint(&buf, 0, 2);
240 		}
241 	}
242 	pq_endmessage(&buf);
243 }
244 
245 /*
246  * Get the lookup info that printtup() needs
247  */
248 static void
printtup_prepare_info(DR_printtup * myState,TupleDesc typeinfo,int numAttrs)249 printtup_prepare_info(DR_printtup *myState, TupleDesc typeinfo, int numAttrs)
250 {
251 	int16	   *formats = myState->portal->formats;
252 	int			i;
253 
254 	/* get rid of any old data */
255 	if (myState->myinfo)
256 		pfree(myState->myinfo);
257 	myState->myinfo = NULL;
258 
259 	myState->attrinfo = typeinfo;
260 	myState->nattrs = numAttrs;
261 	if (numAttrs <= 0)
262 		return;
263 
264 	myState->myinfo = (PrinttupAttrInfo *)
265 		palloc0(numAttrs * sizeof(PrinttupAttrInfo));
266 
267 	for (i = 0; i < numAttrs; i++)
268 	{
269 		PrinttupAttrInfo *thisState = myState->myinfo + i;
270 		int16		format = (formats ? formats[i] : 0);
271 
272 		thisState->format = format;
273 		if (format == 0)
274 		{
275 			getTypeOutputInfo(typeinfo->attrs[i]->atttypid,
276 							  &thisState->typoutput,
277 							  &thisState->typisvarlena);
278 			fmgr_info(thisState->typoutput, &thisState->finfo);
279 		}
280 		else if (format == 1)
281 		{
282 			getTypeBinaryOutputInfo(typeinfo->attrs[i]->atttypid,
283 									&thisState->typsend,
284 									&thisState->typisvarlena);
285 			fmgr_info(thisState->typsend, &thisState->finfo);
286 		}
287 		else
288 			ereport(ERROR,
289 					(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
290 					 errmsg("unsupported format code: %d", format)));
291 	}
292 }
293 
294 /* ----------------
295  *		printtup --- print a tuple in protocol 3.0
296  * ----------------
297  */
298 static bool
printtup(TupleTableSlot * slot,DestReceiver * self)299 printtup(TupleTableSlot *slot, DestReceiver *self)
300 {
301 	TupleDesc	typeinfo = slot->tts_tupleDescriptor;
302 	DR_printtup *myState = (DR_printtup *) self;
303 	MemoryContext oldcontext;
304 	StringInfoData buf;
305 	int			natts = typeinfo->natts;
306 	int			i;
307 
308 	/* Set or update my derived attribute info, if needed */
309 	if (myState->attrinfo != typeinfo || myState->nattrs != natts)
310 		printtup_prepare_info(myState, typeinfo, natts);
311 
312 	/* Make sure the tuple is fully deconstructed */
313 	slot_getallattrs(slot);
314 
315 	/* Switch into per-row context so we can recover memory below */
316 	oldcontext = MemoryContextSwitchTo(myState->tmpcontext);
317 
318 	/*
319 	 * Prepare a DataRow message (note buffer is in per-row context)
320 	 */
321 	pq_beginmessage(&buf, 'D');
322 
323 	pq_sendint(&buf, natts, 2);
324 
325 	/*
326 	 * send the attributes of this tuple
327 	 */
328 	for (i = 0; i < natts; ++i)
329 	{
330 		PrinttupAttrInfo *thisState = myState->myinfo + i;
331 		Datum		attr = slot->tts_values[i];
332 
333 		if (slot->tts_isnull[i])
334 		{
335 			pq_sendint(&buf, -1, 4);
336 			continue;
337 		}
338 
339 		/*
340 		 * Here we catch undefined bytes in datums that are returned to the
341 		 * client without hitting disk; see comments at the related check in
342 		 * PageAddItem().  This test is most useful for uncompressed,
343 		 * non-external datums, but we're quite likely to see such here when
344 		 * testing new C functions.
345 		 */
346 		if (thisState->typisvarlena)
347 			VALGRIND_CHECK_MEM_IS_DEFINED(DatumGetPointer(attr),
348 										  VARSIZE_ANY(attr));
349 
350 		if (thisState->format == 0)
351 		{
352 			/* Text output */
353 			char	   *outputstr;
354 
355 			outputstr = OutputFunctionCall(&thisState->finfo, attr);
356 			pq_sendcountedtext(&buf, outputstr, strlen(outputstr), false);
357 		}
358 		else
359 		{
360 			/* Binary output */
361 			bytea	   *outputbytes;
362 
363 			outputbytes = SendFunctionCall(&thisState->finfo, attr);
364 			pq_sendint(&buf, VARSIZE(outputbytes) - VARHDRSZ, 4);
365 			pq_sendbytes(&buf, VARDATA(outputbytes),
366 						 VARSIZE(outputbytes) - VARHDRSZ);
367 		}
368 	}
369 
370 	pq_endmessage(&buf);
371 
372 	/* Return to caller's context, and flush row's temporary memory */
373 	MemoryContextSwitchTo(oldcontext);
374 	MemoryContextReset(myState->tmpcontext);
375 
376 	return true;
377 }
378 
379 /* ----------------
380  *		printtup_20 --- print a tuple in protocol 2.0
381  * ----------------
382  */
383 static bool
printtup_20(TupleTableSlot * slot,DestReceiver * self)384 printtup_20(TupleTableSlot *slot, DestReceiver *self)
385 {
386 	TupleDesc	typeinfo = slot->tts_tupleDescriptor;
387 	DR_printtup *myState = (DR_printtup *) self;
388 	MemoryContext oldcontext;
389 	StringInfoData buf;
390 	int			natts = typeinfo->natts;
391 	int			i,
392 				j,
393 				k;
394 
395 	/* Set or update my derived attribute info, if needed */
396 	if (myState->attrinfo != typeinfo || myState->nattrs != natts)
397 		printtup_prepare_info(myState, typeinfo, natts);
398 
399 	/* Make sure the tuple is fully deconstructed */
400 	slot_getallattrs(slot);
401 
402 	/* Switch into per-row context so we can recover memory below */
403 	oldcontext = MemoryContextSwitchTo(myState->tmpcontext);
404 
405 	/*
406 	 * tell the frontend to expect new tuple data (in ASCII style)
407 	 */
408 	pq_beginmessage(&buf, 'D');
409 
410 	/*
411 	 * send a bitmap of which attributes are not null
412 	 */
413 	j = 0;
414 	k = 1 << 7;
415 	for (i = 0; i < natts; ++i)
416 	{
417 		if (!slot->tts_isnull[i])
418 			j |= k;				/* set bit if not null */
419 		k >>= 1;
420 		if (k == 0)				/* end of byte? */
421 		{
422 			pq_sendint(&buf, j, 1);
423 			j = 0;
424 			k = 1 << 7;
425 		}
426 	}
427 	if (k != (1 << 7))			/* flush last partial byte */
428 		pq_sendint(&buf, j, 1);
429 
430 	/*
431 	 * send the attributes of this tuple
432 	 */
433 	for (i = 0; i < natts; ++i)
434 	{
435 		PrinttupAttrInfo *thisState = myState->myinfo + i;
436 		Datum		attr = slot->tts_values[i];
437 		char	   *outputstr;
438 
439 		if (slot->tts_isnull[i])
440 			continue;
441 
442 		Assert(thisState->format == 0);
443 
444 		outputstr = OutputFunctionCall(&thisState->finfo, attr);
445 		pq_sendcountedtext(&buf, outputstr, strlen(outputstr), true);
446 	}
447 
448 	pq_endmessage(&buf);
449 
450 	/* Return to caller's context, and flush row's temporary memory */
451 	MemoryContextSwitchTo(oldcontext);
452 	MemoryContextReset(myState->tmpcontext);
453 
454 	return true;
455 }
456 
457 /* ----------------
458  *		printtup_shutdown
459  * ----------------
460  */
461 static void
printtup_shutdown(DestReceiver * self)462 printtup_shutdown(DestReceiver *self)
463 {
464 	DR_printtup *myState = (DR_printtup *) self;
465 
466 	if (myState->myinfo)
467 		pfree(myState->myinfo);
468 	myState->myinfo = NULL;
469 
470 	myState->attrinfo = NULL;
471 
472 	if (myState->tmpcontext)
473 		MemoryContextDelete(myState->tmpcontext);
474 	myState->tmpcontext = NULL;
475 }
476 
477 /* ----------------
478  *		printtup_destroy
479  * ----------------
480  */
481 static void
printtup_destroy(DestReceiver * self)482 printtup_destroy(DestReceiver *self)
483 {
484 	pfree(self);
485 }
486 
487 /* ----------------
488  *		printatt
489  * ----------------
490  */
491 static void
printatt(unsigned attributeId,Form_pg_attribute attributeP,char * value)492 printatt(unsigned attributeId,
493 		 Form_pg_attribute attributeP,
494 		 char *value)
495 {
496 	printf("\t%2d: %s%s%s%s\t(typeid = %u, len = %d, typmod = %d, byval = %c)\n",
497 		   attributeId,
498 		   NameStr(attributeP->attname),
499 		   value != NULL ? " = \"" : "",
500 		   value != NULL ? value : "",
501 		   value != NULL ? "\"" : "",
502 		   (unsigned int) (attributeP->atttypid),
503 		   attributeP->attlen,
504 		   attributeP->atttypmod,
505 		   attributeP->attbyval ? 't' : 'f');
506 }
507 
508 /* ----------------
509  *		debugStartup - prepare to print tuples for an interactive backend
510  * ----------------
511  */
512 void
debugStartup(DestReceiver * self,int operation,TupleDesc typeinfo)513 debugStartup(DestReceiver *self, int operation, TupleDesc typeinfo)
514 {
515 	int			natts = typeinfo->natts;
516 	Form_pg_attribute *attinfo = typeinfo->attrs;
517 	int			i;
518 
519 	/*
520 	 * show the return type of the tuples
521 	 */
522 	for (i = 0; i < natts; ++i)
523 		printatt((unsigned) i + 1, attinfo[i], NULL);
524 	printf("\t----\n");
525 }
526 
527 /* ----------------
528  *		debugtup - print one tuple for an interactive backend
529  * ----------------
530  */
531 bool
debugtup(TupleTableSlot * slot,DestReceiver * self)532 debugtup(TupleTableSlot *slot, DestReceiver *self)
533 {
534 	TupleDesc	typeinfo = slot->tts_tupleDescriptor;
535 	int			natts = typeinfo->natts;
536 	int			i;
537 	Datum		attr;
538 	char	   *value;
539 	bool		isnull;
540 	Oid			typoutput;
541 	bool		typisvarlena;
542 
543 	for (i = 0; i < natts; ++i)
544 	{
545 		attr = slot_getattr(slot, i + 1, &isnull);
546 		if (isnull)
547 			continue;
548 		getTypeOutputInfo(typeinfo->attrs[i]->atttypid,
549 						  &typoutput, &typisvarlena);
550 
551 		value = OidOutputFunctionCall(typoutput, attr);
552 
553 		printatt((unsigned) i + 1, typeinfo->attrs[i], value);
554 	}
555 	printf("\t----\n");
556 
557 	return true;
558 }
559 
560 /* ----------------
561  *		printtup_internal_20 --- print a binary tuple in protocol 2.0
562  *
563  * We use a different message type, i.e. 'B' instead of 'D' to
564  * indicate a tuple in internal (binary) form.
565  *
566  * This is largely same as printtup_20, except we use binary formatting.
567  * ----------------
568  */
569 static bool
printtup_internal_20(TupleTableSlot * slot,DestReceiver * self)570 printtup_internal_20(TupleTableSlot *slot, DestReceiver *self)
571 {
572 	TupleDesc	typeinfo = slot->tts_tupleDescriptor;
573 	DR_printtup *myState = (DR_printtup *) self;
574 	MemoryContext oldcontext;
575 	StringInfoData buf;
576 	int			natts = typeinfo->natts;
577 	int			i,
578 				j,
579 				k;
580 
581 	/* Set or update my derived attribute info, if needed */
582 	if (myState->attrinfo != typeinfo || myState->nattrs != natts)
583 		printtup_prepare_info(myState, typeinfo, natts);
584 
585 	/* Make sure the tuple is fully deconstructed */
586 	slot_getallattrs(slot);
587 
588 	/* Switch into per-row context so we can recover memory below */
589 	oldcontext = MemoryContextSwitchTo(myState->tmpcontext);
590 
591 	/*
592 	 * tell the frontend to expect new tuple data (in binary style)
593 	 */
594 	pq_beginmessage(&buf, 'B');
595 
596 	/*
597 	 * send a bitmap of which attributes are not null
598 	 */
599 	j = 0;
600 	k = 1 << 7;
601 	for (i = 0; i < natts; ++i)
602 	{
603 		if (!slot->tts_isnull[i])
604 			j |= k;				/* set bit if not null */
605 		k >>= 1;
606 		if (k == 0)				/* end of byte? */
607 		{
608 			pq_sendint(&buf, j, 1);
609 			j = 0;
610 			k = 1 << 7;
611 		}
612 	}
613 	if (k != (1 << 7))			/* flush last partial byte */
614 		pq_sendint(&buf, j, 1);
615 
616 	/*
617 	 * send the attributes of this tuple
618 	 */
619 	for (i = 0; i < natts; ++i)
620 	{
621 		PrinttupAttrInfo *thisState = myState->myinfo + i;
622 		Datum		attr = slot->tts_values[i];
623 		bytea	   *outputbytes;
624 
625 		if (slot->tts_isnull[i])
626 			continue;
627 
628 		Assert(thisState->format == 1);
629 
630 		outputbytes = SendFunctionCall(&thisState->finfo, attr);
631 		pq_sendint(&buf, VARSIZE(outputbytes) - VARHDRSZ, 4);
632 		pq_sendbytes(&buf, VARDATA(outputbytes),
633 					 VARSIZE(outputbytes) - VARHDRSZ);
634 	}
635 
636 	pq_endmessage(&buf);
637 
638 	/* Return to caller's context, and flush row's temporary memory */
639 	MemoryContextSwitchTo(oldcontext);
640 	MemoryContextReset(myState->tmpcontext);
641 
642 	return true;
643 }
644