Age Owner Branch data TLA 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 *
6479 tgl@sss.pgh.pa.us 72 :CBC 407361 : printtup_create_DR(CommandDest dest)
73 : : {
260 michael@paquier.xyz 74 : 407361 : DR_printtup *self = palloc0_object(DR_printtup);
75 : :
6286 bruce@momjian.us 76 : 407361 : self->pub.receiveSlot = printtup; /* might get changed later */
8422 tgl@sss.pgh.pa.us 77 : 407361 : self->pub.rStartup = printtup_startup;
78 : 407361 : self->pub.rShutdown = printtup_shutdown;
79 : 407361 : self->pub.rDestroy = printtup_destroy;
8514 80 : 407361 : self->pub.mydest = dest;
81 : :
82 : : /*
83 : : * Send T message automatically if DestRemote, but not if
84 : : * DestRemoteExecute
85 : : */
7602 alvherre@alvh.no-ip. 86 : 407361 : self->sendDescrip = (dest == DestRemote);
87 : :
10074 tgl@sss.pgh.pa.us 88 : 407361 : self->attrinfo = NULL;
89 : 407361 : self->nattrs = 0;
90 : 407361 : self->myinfo = NULL;
2719 91 : 407361 : self->buf.data = NULL;
4680 92 : 407361 : self->tmpcontext = NULL;
93 : :
9956 bruce@momjian.us 94 : 407361 : return (DestReceiver *) self;
95 : : }
96 : :
97 : : /*
98 : : * Set parameters for a DestRemote (or DestRemoteExecute) receiver
99 : : */
100 : : void
6479 tgl@sss.pgh.pa.us 101 : 407361 : SetRemoteDestReceiverParams(DestReceiver *self, Portal portal)
102 : : {
103 : 407361 : DR_printtup *myState = (DR_printtup *) self;
104 : :
105 [ + + - + ]: 407361 : Assert(myState->pub.mydest == DestRemote ||
106 : : myState->pub.mydest == DestRemoteExecute);
107 : :
108 : 407361 : myState->portal = portal;
109 : 407361 : }
110 : :
111 : : static void
8512 112 : 198191 : printtup_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
113 : : {
8515 114 : 198191 : DR_printtup *myState = (DR_printtup *) self;
8424 bruce@momjian.us 115 : 198191 : 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 : : */
3242 andres@anarazel.de 121 : 198191 : 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 : : */
4680 tgl@sss.pgh.pa.us 129 : 198191 : 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 : : */
7320 137 [ + + ]: 198191 : if (myState->sendDescrip)
3242 andres@anarazel.de 138 : 191257 : 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 : : */
10074 tgl@sss.pgh.pa.us 153 : 198191 : }
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
3242 andres@anarazel.de 167 : 198244 : SendRowDescriptionMessage(StringInfo buf, TupleDesc typeinfo,
168 : : List *targetlist, int16 *formats)
169 : : {
8515 tgl@sss.pgh.pa.us 170 : 198244 : int natts = typeinfo->natts;
171 : : int i;
2002 heikki.linnakangas@i 172 : 198244 : ListCell *tlist_item = list_head(targetlist);
173 : :
174 : : /* tuple descriptor message type */
771 nathan@postgresql.or 175 : 198244 : pq_beginmessage_reuse(buf, PqMsg_RowDescription);
176 : : /* # of attrs in tuples */
3242 andres@anarazel.de 177 : 198244 : 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 : 198244 : 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 : :
8515 tgl@sss.pgh.pa.us 196 [ + + ]: 802540 : for (i = 0; i < natts; ++i)
197 : : {
3294 andres@anarazel.de 198 : 604296 : Form_pg_attribute att = TupleDescAttr(typeinfo, i);
199 : 604296 : Oid atttypid = att->atttypid;
200 : 604296 : 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 : : */
3242 209 : 604296 : atttypid = getBaseTypeAndTypmod(atttypid, &atttypmod);
210 : :
211 : : /* Do we have a non-resjunk tlist item? */
212 [ + + ]: 604296 : while (tlist_item &&
213 [ - + ]: 592029 : ((TargetEntry *) lfirst(tlist_item))->resjunk)
2600 tgl@sss.pgh.pa.us 214 :UBC 0 : tlist_item = lnext(targetlist, tlist_item);
3242 andres@anarazel.de 215 [ + + ]:CBC 604296 : if (tlist_item)
216 : : {
217 : 592029 : TargetEntry *tle = (TargetEntry *) lfirst(tlist_item);
218 : :
219 : 592029 : resorigtbl = tle->resorigtbl;
220 : 592029 : resorigcol = tle->resorigcol;
2600 tgl@sss.pgh.pa.us 221 : 592029 : tlist_item = lnext(targetlist, tlist_item);
222 : : }
223 : : else
224 : : {
225 : : /* No info available, so send zeroes */
3242 andres@anarazel.de 226 : 12267 : resorigtbl = 0;
227 : 12267 : resorigcol = 0;
228 : : }
229 : :
230 [ + + ]: 604296 : if (formats)
231 : 604084 : format = formats[i];
232 : : else
233 : 212 : format = 0;
234 : :
235 : 604296 : pq_writestring(buf, NameStr(att->attname));
236 : 604296 : pq_writeint32(buf, resorigtbl);
237 : 604296 : pq_writeint16(buf, resorigcol);
238 : 604296 : pq_writeint32(buf, atttypid);
239 : 604296 : pq_writeint16(buf, att->attlen);
240 : 604296 : pq_writeint32(buf, atttypmod);
241 : 604296 : pq_writeint16(buf, format);
242 : : }
243 : :
2002 heikki.linnakangas@i 244 : 198244 : pq_endmessage_reuse(buf);
8515 tgl@sss.pgh.pa.us 245 : 198244 : }
246 : :
247 : : /*
248 : : * Get the lookup info that printtup() needs
249 : : */
250 : : static void
9956 bruce@momjian.us 251 : 162158 : printtup_prepare_info(DR_printtup *myState, TupleDesc typeinfo, int numAttrs)
252 : : {
8511 tgl@sss.pgh.pa.us 253 : 162158 : int16 *formats = myState->portal->formats;
254 : : int i;
255 : :
256 : : /* get rid of any old data */
10074 257 [ + + ]: 162158 : if (myState->myinfo)
8119 258 : 1044 : pfree(myState->myinfo);
10074 259 : 162158 : myState->myinfo = NULL;
260 : :
261 : 162158 : myState->attrinfo = typeinfo;
262 : 162158 : myState->nattrs = numAttrs;
263 [ + + ]: 162158 : if (numAttrs <= 0)
264 : 199 : return;
265 : :
10 michael@paquier.xyz 266 :GNC 161959 : myState->myinfo = palloc0_array(PrinttupAttrInfo, numAttrs);
267 : :
10074 tgl@sss.pgh.pa.us 268 [ + + ]:CBC 631361 : for (i = 0; i < numAttrs; i++)
269 : : {
9956 bruce@momjian.us 270 : 469402 : PrinttupAttrInfo *thisState = myState->myinfo + i;
8511 tgl@sss.pgh.pa.us 271 [ + + ]: 469402 : int16 format = (formats ? formats[i] : 0);
3294 andres@anarazel.de 272 : 469402 : Form_pg_attribute attr = TupleDescAttr(typeinfo, i);
273 : :
8511 tgl@sss.pgh.pa.us 274 : 469402 : thisState->format = format;
275 [ + + ]: 469402 : if (format == 0)
276 : : {
3294 andres@anarazel.de 277 : 469363 : getTypeOutputInfo(attr->atttypid,
278 : : &thisState->typoutput,
279 : : &thisState->typisvarlena);
10074 tgl@sss.pgh.pa.us 280 : 469363 : fmgr_info(thisState->typoutput, &thisState->finfo);
281 : : }
8511 282 [ + - ]: 39 : else if (format == 1)
283 : : {
3294 andres@anarazel.de 284 : 39 : getTypeBinaryOutputInfo(attr->atttypid,
285 : : &thisState->typsend,
286 : : &thisState->typisvarlena);
8511 tgl@sss.pgh.pa.us 287 : 39 : fmgr_info(thisState->typsend, &thisState->finfo);
288 : : }
289 : : else
8438 tgl@sss.pgh.pa.us 290 [ # # ]:UBC 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
7834 tgl@sss.pgh.pa.us 304 :CBC 3764410 : printtup(TupleTableSlot *slot, DestReceiver *self)
305 : : {
7621 bruce@momjian.us 306 : 3764410 : TupleDesc typeinfo = slot->tts_tupleDescriptor;
8512 tgl@sss.pgh.pa.us 307 : 3764410 : DR_printtup *myState = (DR_printtup *) self;
308 : : MemoryContext oldcontext;
3242 andres@anarazel.de 309 : 3764410 : StringInfo buf = &myState->buf;
8494 tgl@sss.pgh.pa.us 310 : 3764410 : int natts = typeinfo->natts;
311 : : int i;
312 : :
313 : : /* Set or update my derived attribute info, if needed */
8512 314 [ + + - + ]: 3764410 : if (myState->attrinfo != typeinfo || myState->nattrs != natts)
315 : 162158 : printtup_prepare_info(myState, typeinfo, natts);
316 : :
317 : : /* Make sure the tuple is fully deconstructed */
7834 318 : 3764410 : slot_getallattrs(slot);
319 : :
320 : : /* Switch into per-row context so we can recover memory below */
4680 321 : 3764410 : oldcontext = MemoryContextSwitchTo(myState->tmpcontext);
322 : :
323 : : /*
324 : : * Prepare a DataRow message (note buffer is in per-query context)
325 : : */
771 nathan@postgresql.or 326 : 3764410 : pq_beginmessage_reuse(buf, PqMsg_DataRow);
327 : :
3242 andres@anarazel.de 328 : 3764410 : pq_sendint16(buf, natts);
329 : :
330 : : /*
331 : : * send the attributes of this tuple
332 : : */
8512 tgl@sss.pgh.pa.us 333 [ + + ]: 22694383 : for (i = 0; i < natts; ++i)
334 : : {
335 : 18929981 : PrinttupAttrInfo *thisState = myState->myinfo + i;
4680 336 : 18929981 : Datum attr = slot->tts_values[i];
337 : :
7834 338 [ + + ]: 18929981 : if (slot->tts_isnull[i])
339 : : {
3242 andres@anarazel.de 340 : 1127226 : pq_sendint32(buf, -1);
8512 tgl@sss.pgh.pa.us 341 : 1127226 : 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 : : */
8511 351 : 17802755 : if (thisState->typisvarlena)
352 : : VALGRIND_CHECK_MEM_IS_DEFINED(DatumGetPointer(attr),
353 : : VARSIZE_ANY(DatumGetPointer(attr)));
354 : :
355 [ + + ]: 17802755 : if (thisState->format == 0)
356 : : {
357 : : /* Text output */
358 : : char *outputstr;
359 : :
7450 360 : 17795576 : outputstr = OutputFunctionCall(&thisState->finfo, attr);
906 heikki.linnakangas@i 361 : 17795568 : pq_sendcountedtext(buf, outputstr, strlen(outputstr));
362 : : }
363 : : else
364 : : {
365 : : /* Binary output */
366 : : bytea *outputbytes;
367 : :
7450 tgl@sss.pgh.pa.us 368 : 7179 : outputbytes = SendFunctionCall(&thisState->finfo, attr);
3242 andres@anarazel.de 369 : 7179 : pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ);
370 : 7179 : pq_sendbytes(buf, VARDATA(outputbytes),
8511 tgl@sss.pgh.pa.us 371 : 7179 : VARSIZE(outputbytes) - VARHDRSZ);
372 : : }
373 : : }
374 : :
3242 andres@anarazel.de 375 : 3764402 : pq_endmessage_reuse(buf);
376 : :
377 : : /* Return to caller's context, and flush row's temporary memory */
4680 tgl@sss.pgh.pa.us 378 : 3764402 : MemoryContextSwitchTo(oldcontext);
379 : 3764402 : MemoryContextReset(myState->tmpcontext);
380 : :
3734 rhaas@postgresql.org 381 : 3764402 : return true;
382 : : }
383 : :
384 : : /* ----------------
385 : : * printtup_shutdown
386 : : * ----------------
387 : : */
388 : : static void
8514 tgl@sss.pgh.pa.us 389 : 193127 : printtup_shutdown(DestReceiver *self)
390 : : {
9956 bruce@momjian.us 391 : 193127 : DR_printtup *myState = (DR_printtup *) self;
392 : :
10074 tgl@sss.pgh.pa.us 393 [ + + ]: 193127 : if (myState->myinfo)
394 : 160783 : pfree(myState->myinfo);
8514 395 : 193127 : myState->myinfo = NULL;
396 : :
397 : 193127 : myState->attrinfo = NULL;
398 : :
2719 399 [ + - ]: 193127 : if (myState->buf.data)
400 : 193127 : pfree(myState->buf.data);
401 : 193127 : myState->buf.data = NULL;
402 : :
4680 403 [ + - ]: 193127 : if (myState->tmpcontext)
404 : 193127 : MemoryContextDelete(myState->tmpcontext);
405 : 193127 : myState->tmpcontext = NULL;
8514 406 : 193127 : }
407 : :
408 : : /* ----------------
409 : : * printtup_destroy
410 : : * ----------------
411 : : */
412 : : static void
413 : 387048 : printtup_destroy(DestReceiver *self)
414 : : {
415 : 387048 : pfree(self);
10074 416 : 387048 : }
417 : :
418 : : /* ----------------
419 : : * printatt
420 : : * ----------------
421 : : */
422 : : static void
11006 scrappy@hub.org 423 : 236 : printatt(unsigned attributeId,
424 : : Form_pg_attribute attributeP,
425 : : char *value)
426 : : {
10332 bruce@momjian.us 427 [ + + + + : 236 : 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');
11006 scrappy@hub.org 437 : 236 : }
438 : :
439 : : /* ----------------
440 : : * debugStartup - prepare to print tuples for an interactive backend
441 : : * ----------------
442 : : */
443 : : void
8512 tgl@sss.pgh.pa.us 444 : 117 : debugStartup(DestReceiver *self, int operation, TupleDesc typeinfo)
445 : : {
446 : 117 : int natts = typeinfo->natts;
447 : : int i;
448 : :
449 : : /*
450 : : * show the return type of the tuples
451 : : */
452 [ + + ]: 234 : for (i = 0; i < natts; ++i)
3294 andres@anarazel.de 453 : 117 : printatt((unsigned) i + 1, TupleDescAttr(typeinfo, i), NULL);
8512 tgl@sss.pgh.pa.us 454 : 117 : printf("\t----\n");
8947 455 : 117 : }
456 : :
457 : : /* ----------------
458 : : * debugtup - print one tuple for an interactive backend
459 : : * ----------------
460 : : */
461 : : bool
7834 462 : 119 : debugtup(TupleTableSlot *slot, DestReceiver *self)
463 : : {
464 : 119 : TupleDesc typeinfo = slot->tts_tupleDescriptor;
8494 465 : 119 : int natts = typeinfo->natts;
466 : : int i;
467 : : Datum attr;
468 : : char *value;
469 : : bool isnull;
470 : : Oid typoutput;
471 : : bool typisvarlena;
472 : :
9400 473 [ + + ]: 238 : for (i = 0; i < natts; ++i)
474 : : {
4680 475 : 119 : attr = slot_getattr(slot, i + 1, &isnull);
10077 476 [ - + ]: 119 : if (isnull)
10077 tgl@sss.pgh.pa.us 477 :UBC 0 : continue;
3294 andres@anarazel.de 478 :CBC 119 : getTypeOutputInfo(TupleDescAttr(typeinfo, i)->atttypid,
479 : : &typoutput, &typisvarlena);
480 : :
7450 tgl@sss.pgh.pa.us 481 : 119 : value = OidOutputFunctionCall(typoutput, attr);
482 : :
3294 andres@anarazel.de 483 : 119 : printatt((unsigned) i + 1, TupleDescAttr(typeinfo, i), value);
484 : : }
10581 bruce@momjian.us 485 : 119 : printf("\t----\n");
486 : :
3734 rhaas@postgresql.org 487 : 119 : return true;
488 : : }
|