Branch data Line data Source code
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-2026, 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/pqformat.h"
20 : : #include "libpq/protocol.h"
21 : : #include "tcop/pquery.h"
22 : : #include "utils/lsyscache.h"
23 : : #include "utils/memdebug.h"
24 : : #include "utils/memutils.h"
25 : : #include "varatt.h"
26 : :
27 : :
28 : : static void printtup_startup(DestReceiver *self, int operation,
29 : : TupleDesc typeinfo);
30 : : static bool printtup(TupleTableSlot *slot, DestReceiver *self);
31 : : static void printtup_shutdown(DestReceiver *self);
32 : : static void printtup_destroy(DestReceiver *self);
33 : :
34 : : /* ----------------------------------------------------------------
35 : : * printtup / debugtup support
36 : : * ----------------------------------------------------------------
37 : : */
38 : :
39 : : /* ----------------
40 : : * Private state for a printtup destination object
41 : : *
42 : : * NOTE: finfo is the lookup info for either typoutput or typsend, whichever
43 : : * we are using for this column.
44 : : * ----------------
45 : : */
46 : : typedef struct
47 : : { /* Per-attribute information */
48 : : Oid typoutput; /* Oid for the type's text output fn */
49 : : Oid typsend; /* Oid for the type's binary output fn */
50 : : bool typisvarlena; /* is it varlena (ie possibly toastable)? */
51 : : int16 format; /* format code for this column */
52 : : FmgrInfo finfo; /* Precomputed call info for output fn */
53 : : } PrinttupAttrInfo;
54 : :
55 : : typedef struct
56 : : {
57 : : DestReceiver pub; /* publicly-known function pointers */
58 : : Portal portal; /* the Portal we are printing from */
59 : : bool sendDescrip; /* send RowDescription at startup? */
60 : : TupleDesc attrinfo; /* The attr info we are set up for */
61 : : int nattrs;
62 : : PrinttupAttrInfo *myinfo; /* Cached info about each attr */
63 : : StringInfoData buf; /* output buffer (*not* in tmpcontext) */
64 : : MemoryContext tmpcontext; /* Memory context for per-row workspace */
65 : : } DR_printtup;
66 : :
67 : : /* ----------------
68 : : * Initialize: create a DestReceiver for printtup
69 : : * ----------------
70 : : */
71 : : DestReceiver *
72 : 413470 : printtup_create_DR(CommandDest dest)
73 : : {
74 : 413470 : DR_printtup *self = palloc0_object(DR_printtup);
75 : :
76 : 413470 : self->pub.receiveSlot = printtup; /* might get changed later */
77 : 413470 : self->pub.rStartup = printtup_startup;
78 : 413470 : self->pub.rShutdown = printtup_shutdown;
79 : 413470 : self->pub.rDestroy = printtup_destroy;
80 : 413470 : self->pub.mydest = dest;
81 : :
82 : : /*
83 : : * Send T message automatically if DestRemote, but not if
84 : : * DestRemoteExecute
85 : : */
86 : 413470 : self->sendDescrip = (dest == DestRemote);
87 : :
88 : 413470 : self->attrinfo = NULL;
89 : 413470 : self->nattrs = 0;
90 : 413470 : self->myinfo = NULL;
91 : 413470 : self->buf.data = NULL;
92 : 413470 : self->tmpcontext = NULL;
93 : :
94 : 413470 : return (DestReceiver *) self;
95 : : }
96 : :
97 : : /*
98 : : * Set parameters for a DestRemote (or DestRemoteExecute) receiver
99 : : */
100 : : void
101 : 413470 : SetRemoteDestReceiverParams(DestReceiver *self, Portal portal)
102 : : {
103 : 413470 : DR_printtup *myState = (DR_printtup *) self;
104 : :
105 : : Assert(myState->pub.mydest == DestRemote ||
106 : : myState->pub.mydest == DestRemoteExecute);
107 : :
108 : 413470 : myState->portal = portal;
109 : 413470 : }
110 : :
111 : : static void
112 : 197680 : printtup_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
113 : : {
114 : 197680 : DR_printtup *myState = (DR_printtup *) self;
115 : 197680 : Portal portal = myState->portal;
116 : :
117 : : /*
118 : : * Create I/O buffer to be used for all messages. This cannot be inside
119 : : * tmpcontext, since we want to re-use it across rows.
120 : : */
121 : 197680 : initStringInfo(&myState->buf);
122 : :
123 : : /*
124 : : * Create a temporary memory context that we can reset once per row to
125 : : * recover palloc'd memory. This avoids any problems with leaks inside
126 : : * datatype output routines, and should be faster than retail pfree's
127 : : * anyway.
128 : : */
129 : 197680 : myState->tmpcontext = AllocSetContextCreate(CurrentMemoryContext,
130 : : "printtup",
131 : : ALLOCSET_DEFAULT_SIZES);
132 : :
133 : : /*
134 : : * If we are supposed to emit row descriptions, then send the tuple
135 : : * descriptor of the tuples.
136 : : */
137 [ + + ]: 197680 : if (myState->sendDescrip)
138 : 192770 : SendRowDescriptionMessage(&myState->buf,
139 : : typeinfo,
140 : : FetchPortalTargetList(portal),
141 : : portal->formats);
142 : :
143 : : /* ----------------
144 : : * We could set up the derived attr info at this time, but we postpone it
145 : : * until the first call of printtup, for 2 reasons:
146 : : * 1. We don't waste time (compared to the old way) if there are no
147 : : * tuples at all to output.
148 : : * 2. Checking in printtup allows us to handle the case that the tuples
149 : : * change type midway through (although this probably can't happen in
150 : : * the current executor).
151 : : * ----------------
152 : : */
153 : 197680 : }
154 : :
155 : : /*
156 : : * SendRowDescriptionMessage --- send a RowDescription message to the frontend
157 : : *
158 : : * Notes: the TupleDesc has typically been manufactured by ExecTypeFromTL()
159 : : * or some similar function; it does not contain a full set of fields.
160 : : * The targetlist will be NIL when executing a utility function that does
161 : : * not have a plan. If the targetlist isn't NIL then it is a Query node's
162 : : * targetlist; it is up to us to ignore resjunk columns in it. The formats[]
163 : : * array pointer might be NULL (if we are doing Describe on a prepared stmt);
164 : : * send zeroes for the format codes in that case.
165 : : */
166 : : void
167 : 197754 : SendRowDescriptionMessage(StringInfo buf, TupleDesc typeinfo,
168 : : List *targetlist, int16 *formats)
169 : : {
170 : 197754 : int natts = typeinfo->natts;
171 : : int i;
172 : 197754 : ListCell *tlist_item = list_head(targetlist);
173 : :
174 : : /* tuple descriptor message type */
175 : 197754 : pq_beginmessage_reuse(buf, PqMsg_RowDescription);
176 : : /* # of attrs in tuples */
177 : 197754 : pq_sendint16(buf, natts);
178 : :
179 : : /*
180 : : * Preallocate memory for the entire message to be sent. That allows to
181 : : * use the significantly faster inline pqformat.h functions and to avoid
182 : : * reallocations.
183 : : *
184 : : * Have to overestimate the size of the column-names, to account for
185 : : * character set overhead.
186 : : */
187 : 197754 : enlargeStringInfo(buf, (NAMEDATALEN * MAX_CONVERSION_GROWTH /* attname */
188 : : + sizeof(Oid) /* resorigtbl */
189 : : + sizeof(AttrNumber) /* resorigcol */
190 : : + sizeof(Oid) /* atttypid */
191 : : + sizeof(int16) /* attlen */
192 : : + sizeof(int32) /* attypmod */
193 : : + sizeof(int16) /* format */
194 : : ) * natts);
195 : :
196 [ + + ]: 802397 : for (i = 0; i < natts; ++i)
197 : : {
198 : 604643 : Form_pg_attribute att = TupleDescAttr(typeinfo, i);
199 : 604643 : Oid atttypid = att->atttypid;
200 : 604643 : int32 atttypmod = att->atttypmod;
201 : : Oid resorigtbl;
202 : : AttrNumber resorigcol;
203 : : int16 format;
204 : :
205 : : /*
206 : : * If column is a domain, send the base type and typmod instead.
207 : : * Lookup before sending any ints, for efficiency.
208 : : */
209 : 604643 : atttypid = getBaseTypeAndTypmod(atttypid, &atttypmod);
210 : :
211 : : /* Do we have a non-resjunk tlist item? */
212 [ + + ]: 604643 : while (tlist_item &&
213 [ - + ]: 592275 : ((TargetEntry *) lfirst(tlist_item))->resjunk)
214 : 0 : tlist_item = lnext(targetlist, tlist_item);
215 [ + + ]: 604643 : if (tlist_item)
216 : : {
217 : 592275 : TargetEntry *tle = (TargetEntry *) lfirst(tlist_item);
218 : :
219 : 592275 : resorigtbl = tle->resorigtbl;
220 : 592275 : resorigcol = tle->resorigcol;
221 : 592275 : tlist_item = lnext(targetlist, tlist_item);
222 : : }
223 : : else
224 : : {
225 : : /* No info available, so send zeroes */
226 : 12368 : resorigtbl = 0;
227 : 12368 : resorigcol = 0;
228 : : }
229 : :
230 [ + + ]: 604643 : if (formats)
231 : 604388 : format = formats[i];
232 : : else
233 : 255 : format = 0;
234 : :
235 : 604643 : pq_writestring(buf, NameStr(att->attname));
236 : 604643 : pq_writeint32(buf, resorigtbl);
237 : 604643 : pq_writeint16(buf, resorigcol);
238 : 604643 : pq_writeint32(buf, atttypid);
239 : 604643 : pq_writeint16(buf, att->attlen);
240 : 604643 : pq_writeint32(buf, atttypmod);
241 : 604643 : pq_writeint16(buf, format);
242 : : }
243 : :
244 : 197754 : pq_endmessage_reuse(buf);
245 : 197754 : }
246 : :
247 : : /*
248 : : * Get the lookup info that printtup() needs
249 : : */
250 : : static void
251 : 161581 : printtup_prepare_info(DR_printtup *myState, TupleDesc typeinfo, int numAttrs)
252 : : {
253 : 161581 : int16 *formats = myState->portal->formats;
254 : : int i;
255 : :
256 : : /* get rid of any old data */
257 [ + + ]: 161581 : if (myState->myinfo)
258 : 1044 : pfree(myState->myinfo);
259 : 161581 : myState->myinfo = NULL;
260 : :
261 : 161581 : myState->attrinfo = typeinfo;
262 : 161581 : myState->nattrs = numAttrs;
263 [ + + ]: 161581 : if (numAttrs <= 0)
264 : 199 : return;
265 : :
266 : 161382 : myState->myinfo = palloc0_array(PrinttupAttrInfo, numAttrs);
267 : :
268 [ + + ]: 630901 : for (i = 0; i < numAttrs; i++)
269 : : {
270 : 469519 : PrinttupAttrInfo *thisState = myState->myinfo + i;
271 [ + + ]: 469519 : int16 format = (formats ? formats[i] : 0);
272 : 469519 : Form_pg_attribute attr = TupleDescAttr(typeinfo, i);
273 : :
274 : 469519 : thisState->format = format;
275 [ + + ]: 469519 : if (format == 0)
276 : : {
277 : 469476 : getTypeOutputInfo(attr->atttypid,
278 : : &thisState->typoutput,
279 : : &thisState->typisvarlena);
280 : 469476 : fmgr_info(thisState->typoutput, &thisState->finfo);
281 : : }
282 [ + - ]: 43 : else if (format == 1)
283 : : {
284 : 43 : getTypeBinaryOutputInfo(attr->atttypid,
285 : : &thisState->typsend,
286 : : &thisState->typisvarlena);
287 : 43 : fmgr_info(thisState->typsend, &thisState->finfo);
288 : : }
289 : : else
290 [ # # ]: 0 : ereport(ERROR,
291 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
292 : : errmsg("unsupported format code: %d", format)));
293 : : }
294 : : }
295 : :
296 : : /* ----------------
297 : : * printtup --- send a tuple to the client
298 : : *
299 : : * Note: if you change this function, see also serializeAnalyzeReceive
300 : : * in explain.c, which is meant to replicate the computations done here.
301 : : * ----------------
302 : : */
303 : : static bool
304 : 3766250 : printtup(TupleTableSlot *slot, DestReceiver *self)
305 : : {
306 : 3766250 : TupleDesc typeinfo = slot->tts_tupleDescriptor;
307 : 3766250 : DR_printtup *myState = (DR_printtup *) self;
308 : : MemoryContext oldcontext;
309 : 3766250 : StringInfo buf = &myState->buf;
310 : 3766250 : int natts = typeinfo->natts;
311 : : int i;
312 : :
313 : : /* Set or update my derived attribute info, if needed */
314 [ + + - + ]: 3766250 : if (myState->attrinfo != typeinfo || myState->nattrs != natts)
315 : 161581 : printtup_prepare_info(myState, typeinfo, natts);
316 : :
317 : : /* Make sure the tuple is fully deconstructed */
318 : 3766250 : slot_getallattrs(slot);
319 : :
320 : : /* Switch into per-row context so we can recover memory below */
321 : 3766250 : oldcontext = MemoryContextSwitchTo(myState->tmpcontext);
322 : :
323 : : /*
324 : : * Prepare a DataRow message (note buffer is in per-query context)
325 : : */
326 : 3766250 : pq_beginmessage_reuse(buf, PqMsg_DataRow);
327 : :
328 : 3766250 : pq_sendint16(buf, natts);
329 : :
330 : : /*
331 : : * send the attributes of this tuple
332 : : */
333 [ + + ]: 22710545 : for (i = 0; i < natts; ++i)
334 : : {
335 : 18944303 : PrinttupAttrInfo *thisState = myState->myinfo + i;
336 : 18944303 : Datum attr = slot->tts_values[i];
337 : :
338 [ + + ]: 18944303 : if (slot->tts_isnull[i])
339 : : {
340 : 1128872 : pq_sendint32(buf, -1);
341 : 1128872 : continue;
342 : : }
343 : :
344 : : /*
345 : : * Here we catch undefined bytes in datums that are returned to the
346 : : * client without hitting disk; see comments at the related check in
347 : : * PageAddItem(). This test is most useful for uncompressed,
348 : : * non-external datums, but we're quite likely to see such here when
349 : : * testing new C functions.
350 : : */
351 : 17815431 : if (thisState->typisvarlena)
352 : : VALGRIND_CHECK_MEM_IS_DEFINED(DatumGetPointer(attr),
353 : : VARSIZE_ANY(DatumGetPointer(attr)));
354 : :
355 [ + + ]: 17815431 : if (thisState->format == 0)
356 : : {
357 : : /* Text output */
358 : : char *outputstr;
359 : :
360 : 17808248 : outputstr = OutputFunctionCall(&thisState->finfo, attr);
361 : 17808240 : pq_sendcountedtext(buf, outputstr, strlen(outputstr));
362 : : }
363 : : else
364 : : {
365 : : /* Binary output */
366 : : bytea *outputbytes;
367 : :
368 : 7183 : outputbytes = SendFunctionCall(&thisState->finfo, attr);
369 : 7183 : pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ);
370 : 7183 : pq_sendbytes(buf, VARDATA(outputbytes),
371 : 7183 : VARSIZE(outputbytes) - VARHDRSZ);
372 : : }
373 : : }
374 : :
375 : 3766242 : pq_endmessage_reuse(buf);
376 : :
377 : : /* Return to caller's context, and flush row's temporary memory */
378 : 3766242 : MemoryContextSwitchTo(oldcontext);
379 : 3766242 : MemoryContextReset(myState->tmpcontext);
380 : :
381 : 3766242 : return true;
382 : : }
383 : :
384 : : /* ----------------
385 : : * printtup_shutdown
386 : : * ----------------
387 : : */
388 : : static void
389 : 192606 : printtup_shutdown(DestReceiver *self)
390 : : {
391 : 192606 : DR_printtup *myState = (DR_printtup *) self;
392 : :
393 [ + + ]: 192606 : if (myState->myinfo)
394 : 160206 : pfree(myState->myinfo);
395 : 192606 : myState->myinfo = NULL;
396 : :
397 : 192606 : myState->attrinfo = NULL;
398 : :
399 [ + - ]: 192606 : if (myState->buf.data)
400 : 192606 : pfree(myState->buf.data);
401 : 192606 : myState->buf.data = NULL;
402 : :
403 [ + - ]: 192606 : if (myState->tmpcontext)
404 : 192606 : MemoryContextDelete(myState->tmpcontext);
405 : 192606 : myState->tmpcontext = NULL;
406 : 192606 : }
407 : :
408 : : /* ----------------
409 : : * printtup_destroy
410 : : * ----------------
411 : : */
412 : : static void
413 : 393132 : printtup_destroy(DestReceiver *self)
414 : : {
415 : 393132 : pfree(self);
416 : 393132 : }
417 : :
418 : : /* ----------------
419 : : * printatt
420 : : * ----------------
421 : : */
422 : : static void
423 : 240 : printatt(unsigned attributeId,
424 : : Form_pg_attribute attributeP,
425 : : char *value)
426 : : {
427 [ + + + + : 240 : printf("\t%2d: %s%s%s%s\t(typeid = %u, len = %d, typmod = %d, byval = %c)\n",
+ + + + ]
428 : : attributeId,
429 : : NameStr(attributeP->attname),
430 : : value != NULL ? " = \"" : "",
431 : : value != NULL ? value : "",
432 : : value != NULL ? "\"" : "",
433 : : attributeP->atttypid,
434 : : attributeP->attlen,
435 : : attributeP->atttypmod,
436 : : attributeP->attbyval ? 't' : 'f');
437 : 240 : }
438 : :
439 : : /* ----------------
440 : : * debugStartup - prepare to print tuples for an interactive backend
441 : : * ----------------
442 : : */
443 : : void
444 : 119 : debugStartup(DestReceiver *self, int operation, TupleDesc typeinfo)
445 : : {
446 : 119 : int natts = typeinfo->natts;
447 : : int i;
448 : :
449 : : /*
450 : : * show the return type of the tuples
451 : : */
452 [ + + ]: 238 : for (i = 0; i < natts; ++i)
453 : 119 : printatt((unsigned) i + 1, TupleDescAttr(typeinfo, i), NULL);
454 : 119 : printf("\t----\n");
455 : 119 : }
456 : :
457 : : /* ----------------
458 : : * debugtup - print one tuple for an interactive backend
459 : : * ----------------
460 : : */
461 : : bool
462 : 121 : debugtup(TupleTableSlot *slot, DestReceiver *self)
463 : : {
464 : 121 : TupleDesc typeinfo = slot->tts_tupleDescriptor;
465 : 121 : int natts = typeinfo->natts;
466 : : int i;
467 : : Datum attr;
468 : : char *value;
469 : : bool isnull;
470 : : Oid typoutput;
471 : : bool typisvarlena;
472 : :
473 [ + + ]: 242 : for (i = 0; i < natts; ++i)
474 : : {
475 : 121 : attr = slot_getattr(slot, i + 1, &isnull);
476 [ - + ]: 121 : if (isnull)
477 : 0 : continue;
478 : 121 : getTypeOutputInfo(TupleDescAttr(typeinfo, i)->atttypid,
479 : : &typoutput, &typisvarlena);
480 : :
481 : 121 : value = OidOutputFunctionCall(typoutput, attr);
482 : :
483 : 121 : printatt((unsigned) i + 1, TupleDescAttr(typeinfo, i), value);
484 : : }
485 : 121 : printf("\t----\n");
486 : :
487 : 121 : return true;
488 : : }
|