PostgreSQL Source Code git master
printtup.c File Reference
#include "postgres.h"
#include "access/printtup.h"
#include "libpq/pqformat.h"
#include "libpq/protocol.h"
#include "tcop/pquery.h"
#include "utils/lsyscache.h"
#include "utils/memdebug.h"
#include "utils/memutils.h"
#include "varatt.h"
Include dependency graph for printtup.c:

Go to the source code of this file.

Data Structures

struct  PrinttupAttrInfo
 
struct  DR_printtup
 

Functions

static void printtup_startup (DestReceiver *self, int operation, TupleDesc typeinfo)
 
static bool printtup (TupleTableSlot *slot, DestReceiver *self)
 
static void printtup_shutdown (DestReceiver *self)
 
static void printtup_destroy (DestReceiver *self)
 
DestReceiverprinttup_create_DR (CommandDest dest)
 
void SetRemoteDestReceiverParams (DestReceiver *self, Portal portal)
 
void SendRowDescriptionMessage (StringInfo buf, TupleDesc typeinfo, List *targetlist, int16 *formats)
 
static void printtup_prepare_info (DR_printtup *myState, TupleDesc typeinfo, int numAttrs)
 
static void printatt (unsigned attributeId, Form_pg_attribute attributeP, char *value)
 
void debugStartup (DestReceiver *self, int operation, TupleDesc typeinfo)
 
bool debugtup (TupleTableSlot *slot, DestReceiver *self)
 

Function Documentation

◆ debugStartup()

void debugStartup ( DestReceiver self,
int  operation,
TupleDesc  typeinfo 
)

Definition at line 445 of file printtup.c.

446{
447 int natts = typeinfo->natts;
448 int i;
449
450 /*
451 * show the return type of the tuples
452 */
453 for (i = 0; i < natts; ++i)
454 printatt((unsigned) i + 1, TupleDescAttr(typeinfo, i), NULL);
455 printf("\t----\n");
456}
int i
Definition: isn.c:77
#define printf(...)
Definition: port.h:266
static void printatt(unsigned attributeId, Form_pg_attribute attributeP, char *value)
Definition: printtup.c:424
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition: tupdesc.h:160

References i, TupleDescData::natts, printatt(), printf, and TupleDescAttr().

◆ debugtup()

bool debugtup ( TupleTableSlot slot,
DestReceiver self 
)

Definition at line 463 of file printtup.c.

464{
465 TupleDesc typeinfo = slot->tts_tupleDescriptor;
466 int natts = typeinfo->natts;
467 int i;
468 Datum attr;
469 char *value;
470 bool isnull;
471 Oid typoutput;
472 bool typisvarlena;
473
474 for (i = 0; i < natts; ++i)
475 {
476 attr = slot_getattr(slot, i + 1, &isnull);
477 if (isnull)
478 continue;
479 getTypeOutputInfo(TupleDescAttr(typeinfo, i)->atttypid,
480 &typoutput, &typisvarlena);
481
482 value = OidOutputFunctionCall(typoutput, attr);
483
484 printatt((unsigned) i + 1, TupleDescAttr(typeinfo, i), value);
485 }
486 printf("\t----\n");
487
488 return true;
489}
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:1763
static struct @171 value
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:3074
uint64_t Datum
Definition: postgres.h:70
unsigned int Oid
Definition: postgres_ext.h:32
TupleDesc tts_tupleDescriptor
Definition: tuptable.h:122
static Datum slot_getattr(TupleTableSlot *slot, int attnum, bool *isnull)
Definition: tuptable.h:398

References getTypeOutputInfo(), i, TupleDescData::natts, OidOutputFunctionCall(), printatt(), printf, slot_getattr(), TupleTableSlot::tts_tupleDescriptor, TupleDescAttr(), and value.

Referenced by print_slot().

◆ printatt()

static void printatt ( unsigned  attributeId,
Form_pg_attribute  attributeP,
char *  value 
)
static

Definition at line 424 of file printtup.c.

427{
428 printf("\t%2d: %s%s%s%s\t(typeid = %u, len = %d, typmod = %d, byval = %c)\n",
429 attributeId,
430 NameStr(attributeP->attname),
431 value != NULL ? " = \"" : "",
432 value != NULL ? value : "",
433 value != NULL ? "\"" : "",
434 (unsigned int) (attributeP->atttypid),
435 attributeP->attlen,
436 attributeP->atttypmod,
437 attributeP->attbyval ? 't' : 'f');
438}
#define NameStr(name)
Definition: c.h:754

References NameStr, printf, and value.

Referenced by debugStartup(), and debugtup().

◆ printtup()

static bool printtup ( TupleTableSlot slot,
DestReceiver self 
)
static

Definition at line 305 of file printtup.c.

306{
307 TupleDesc typeinfo = slot->tts_tupleDescriptor;
308 DR_printtup *myState = (DR_printtup *) self;
309 MemoryContext oldcontext;
310 StringInfo buf = &myState->buf;
311 int natts = typeinfo->natts;
312 int i;
313
314 /* Set or update my derived attribute info, if needed */
315 if (myState->attrinfo != typeinfo || myState->nattrs != natts)
316 printtup_prepare_info(myState, typeinfo, natts);
317
318 /* Make sure the tuple is fully deconstructed */
319 slot_getallattrs(slot);
320
321 /* Switch into per-row context so we can recover memory below */
322 oldcontext = MemoryContextSwitchTo(myState->tmpcontext);
323
324 /*
325 * Prepare a DataRow message (note buffer is in per-query context)
326 */
328
329 pq_sendint16(buf, natts);
330
331 /*
332 * send the attributes of this tuple
333 */
334 for (i = 0; i < natts; ++i)
335 {
336 PrinttupAttrInfo *thisState = myState->myinfo + i;
337 Datum attr = slot->tts_values[i];
338
339 if (slot->tts_isnull[i])
340 {
341 pq_sendint32(buf, -1);
342 continue;
343 }
344
345 /*
346 * Here we catch undefined bytes in datums that are returned to the
347 * client without hitting disk; see comments at the related check in
348 * PageAddItem(). This test is most useful for uncompressed,
349 * non-external datums, but we're quite likely to see such here when
350 * testing new C functions.
351 */
352 if (thisState->typisvarlena)
355
356 if (thisState->format == 0)
357 {
358 /* Text output */
359 char *outputstr;
360
361 outputstr = OutputFunctionCall(&thisState->finfo, attr);
362 pq_sendcountedtext(buf, outputstr, strlen(outputstr));
363 }
364 else
365 {
366 /* Binary output */
367 bytea *outputbytes;
368
369 outputbytes = SendFunctionCall(&thisState->finfo, attr);
370 pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ);
371 pq_sendbytes(buf, VARDATA(outputbytes),
372 VARSIZE(outputbytes) - VARHDRSZ);
373 }
374 }
375
377
378 /* Return to caller's context, and flush row's temporary memory */
379 MemoryContextSwitchTo(oldcontext);
381
382 return true;
383}
#define VARHDRSZ
Definition: c.h:700
bytea * SendFunctionCall(FmgrInfo *flinfo, Datum val)
Definition: fmgr.c:1744
char * OutputFunctionCall(FmgrInfo *flinfo, Datum val)
Definition: fmgr.c:1683
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:400
#define VALGRIND_CHECK_MEM_IS_DEFINED(addr, size)
Definition: memdebug.h:23
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
static char * buf
Definition: pg_test_fsync.c:72
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:322
void pq_sendbytes(StringInfo buf, const void *data, int datalen)
Definition: pqformat.c:126
void pq_beginmessage_reuse(StringInfo buf, char msgtype)
Definition: pqformat.c:109
void pq_endmessage_reuse(StringInfo buf)
Definition: pqformat.c:314
void pq_sendcountedtext(StringInfo buf, const char *str, int slen)
Definition: pqformat.c:142
static void pq_sendint32(StringInfo buf, uint32 i)
Definition: pqformat.h:144
static void pq_sendint16(StringInfo buf, uint16 i)
Definition: pqformat.h:136
static void printtup_prepare_info(DR_printtup *myState, TupleDesc typeinfo, int numAttrs)
Definition: printtup.c:251
#define PqMsg_DataRow
Definition: protocol.h:43
PrinttupAttrInfo * myinfo
Definition: printtup.c:62
StringInfoData buf
Definition: printtup.c:63
int nattrs
Definition: printtup.c:61
MemoryContext tmpcontext
Definition: printtup.c:64
TupleDesc attrinfo
Definition: printtup.c:60
bool typisvarlena
Definition: printtup.c:50
FmgrInfo finfo
Definition: printtup.c:52
bool * tts_isnull
Definition: tuptable.h:126
Datum * tts_values
Definition: tuptable.h:124
Definition: c.h:695
static void slot_getallattrs(TupleTableSlot *slot)
Definition: tuptable.h:371
static Size VARSIZE_ANY(const void *PTR)
Definition: varatt.h:460
static Size VARSIZE(const void *PTR)
Definition: varatt.h:298
static char * VARDATA(const void *PTR)
Definition: varatt.h:305

References DR_printtup::attrinfo, DR_printtup::buf, buf, DatumGetPointer(), PrinttupAttrInfo::finfo, PrinttupAttrInfo::format, i, MemoryContextReset(), MemoryContextSwitchTo(), DR_printtup::myinfo, DR_printtup::nattrs, TupleDescData::natts, OutputFunctionCall(), pq_beginmessage_reuse(), pq_endmessage_reuse(), pq_sendbytes(), pq_sendcountedtext(), pq_sendint16(), pq_sendint32(), PqMsg_DataRow, printtup_prepare_info(), SendFunctionCall(), slot_getallattrs(), DR_printtup::tmpcontext, TupleTableSlot::tts_isnull, TupleTableSlot::tts_tupleDescriptor, TupleTableSlot::tts_values, PrinttupAttrInfo::typisvarlena, VALGRIND_CHECK_MEM_IS_DEFINED, VARDATA(), VARHDRSZ, VARSIZE(), and VARSIZE_ANY().

Referenced by printtup_create_DR().

◆ printtup_create_DR()

DestReceiver * printtup_create_DR ( CommandDest  dest)

Definition at line 72 of file printtup.c.

73{
74 DR_printtup *self = (DR_printtup *) palloc0(sizeof(DR_printtup));
75
76 self->pub.receiveSlot = printtup; /* might get changed later */
80 self->pub.mydest = dest;
81
82 /*
83 * Send T message automatically if DestRemote, but not if
84 * DestRemoteExecute
85 */
86 self->sendDescrip = (dest == DestRemote);
87
88 self->attrinfo = NULL;
89 self->nattrs = 0;
90 self->myinfo = NULL;
91 self->buf.data = NULL;
92 self->tmpcontext = NULL;
93
94 return (DestReceiver *) self;
95}
@ DestRemote
Definition: dest.h:89
void * palloc0(Size size)
Definition: mcxt.c:1395
static bool printtup(TupleTableSlot *slot, DestReceiver *self)
Definition: printtup.c:305
static void printtup_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
Definition: printtup.c:112
static void printtup_destroy(DestReceiver *self)
Definition: printtup.c:414
static void printtup_shutdown(DestReceiver *self)
Definition: printtup.c:390
DestReceiver pub
Definition: printtup.c:57
bool sendDescrip
Definition: printtup.c:59
void(* rStartup)(DestReceiver *self, int operation, TupleDesc typeinfo)
Definition: dest.h:121
void(* rShutdown)(DestReceiver *self)
Definition: dest.h:124
bool(* receiveSlot)(TupleTableSlot *slot, DestReceiver *self)
Definition: dest.h:118
void(* rDestroy)(DestReceiver *self)
Definition: dest.h:126
CommandDest mydest
Definition: dest.h:128

References DR_printtup::attrinfo, DR_printtup::buf, StringInfoData::data, generate_unaccent_rules::dest, DestRemote, _DestReceiver::mydest, DR_printtup::myinfo, DR_printtup::nattrs, palloc0(), printtup(), printtup_destroy(), printtup_shutdown(), printtup_startup(), DR_printtup::pub, _DestReceiver::rDestroy, _DestReceiver::receiveSlot, _DestReceiver::rShutdown, _DestReceiver::rStartup, DR_printtup::sendDescrip, and DR_printtup::tmpcontext.

Referenced by CreateDestReceiver().

◆ printtup_destroy()

static void printtup_destroy ( DestReceiver self)
static

Definition at line 414 of file printtup.c.

415{
416 pfree(self);
417}
void pfree(void *pointer)
Definition: mcxt.c:1594

References pfree().

Referenced by printtup_create_DR().

◆ printtup_prepare_info()

static void printtup_prepare_info ( DR_printtup myState,
TupleDesc  typeinfo,
int  numAttrs 
)
static

Definition at line 251 of file printtup.c.

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 Form_pg_attribute attr = TupleDescAttr(typeinfo, i);
274
275 thisState->format = format;
276 if (format == 0)
277 {
278 getTypeOutputInfo(attr->atttypid,
279 &thisState->typoutput,
280 &thisState->typisvarlena);
281 fmgr_info(thisState->typoutput, &thisState->finfo);
282 }
283 else if (format == 1)
284 {
285 getTypeBinaryOutputInfo(attr->atttypid,
286 &thisState->typsend,
287 &thisState->typisvarlena);
288 fmgr_info(thisState->typsend, &thisState->finfo);
289 }
290 else
292 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
293 errmsg("unsupported format code: %d", format)));
294 }
295}
int16_t int16
Definition: c.h:536
int errcode(int sqlerrcode)
Definition: elog.c:863
int errmsg(const char *fmt,...)
Definition: elog.c:1080
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:150
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:128
void getTypeBinaryOutputInfo(Oid type, Oid *typSend, bool *typIsVarlena)
Definition: lsyscache.c:3140
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:202
static char format
Portal portal
Definition: printtup.c:58
int16 * formats
Definition: portal.h:161

References DR_printtup::attrinfo, ereport, errcode(), errmsg(), ERROR, PrinttupAttrInfo::finfo, fmgr_info(), PrinttupAttrInfo::format, format, PortalData::formats, getTypeBinaryOutputInfo(), getTypeOutputInfo(), i, DR_printtup::myinfo, DR_printtup::nattrs, palloc0(), pfree(), DR_printtup::portal, TupleDescAttr(), PrinttupAttrInfo::typisvarlena, PrinttupAttrInfo::typoutput, and PrinttupAttrInfo::typsend.

Referenced by printtup().

◆ printtup_shutdown()

static void printtup_shutdown ( DestReceiver self)
static

Definition at line 390 of file printtup.c.

391{
392 DR_printtup *myState = (DR_printtup *) self;
393
394 if (myState->myinfo)
395 pfree(myState->myinfo);
396 myState->myinfo = NULL;
397
398 myState->attrinfo = NULL;
399
400 if (myState->buf.data)
401 pfree(myState->buf.data);
402 myState->buf.data = NULL;
403
404 if (myState->tmpcontext)
406 myState->tmpcontext = NULL;
407}
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:469

References DR_printtup::attrinfo, DR_printtup::buf, StringInfoData::data, MemoryContextDelete(), DR_printtup::myinfo, pfree(), and DR_printtup::tmpcontext.

Referenced by printtup_create_DR().

◆ printtup_startup()

static void printtup_startup ( DestReceiver self,
int  operation,
TupleDesc  typeinfo 
)
static

Definition at line 112 of file printtup.c.

113{
114 DR_printtup *myState = (DR_printtup *) self;
115 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 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 */
130 "printtup",
132
133 /*
134 * If we are supposed to emit row descriptions, then send the tuple
135 * descriptor of the tuples.
136 */
137 if (myState->sendDescrip)
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}
MemoryContext CurrentMemoryContext
Definition: mcxt.c:160
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:160
List * FetchPortalTargetList(Portal portal)
Definition: pquery.c:322
void SendRowDescriptionMessage(StringInfo buf, TupleDesc typeinfo, List *targetlist, int16 *formats)
Definition: printtup.c:167
void initStringInfo(StringInfo str)
Definition: stringinfo.c:97

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, DR_printtup::buf, CurrentMemoryContext, FetchPortalTargetList(), PortalData::formats, initStringInfo(), DR_printtup::portal, DR_printtup::sendDescrip, SendRowDescriptionMessage(), and DR_printtup::tmpcontext.

Referenced by printtup_create_DR().

◆ SendRowDescriptionMessage()

void SendRowDescriptionMessage ( StringInfo  buf,
TupleDesc  typeinfo,
List targetlist,
int16 formats 
)

Definition at line 167 of file printtup.c.

169{
170 int natts = typeinfo->natts;
171 int i;
172 ListCell *tlist_item = list_head(targetlist);
173
174 /* tuple descriptor message type */
176 /* # of attrs in tuples */
177 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 */
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 for (i = 0; i < natts; ++i)
197 {
198 Form_pg_attribute att = TupleDescAttr(typeinfo, i);
199 Oid atttypid = att->atttypid;
200 int32 atttypmod = att->atttypmod;
201 Oid resorigtbl;
202 AttrNumber resorigcol;
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 atttypid = getBaseTypeAndTypmod(atttypid, &atttypmod);
210
211 /* Do we have a non-resjunk tlist item? */
212 while (tlist_item &&
213 ((TargetEntry *) lfirst(tlist_item))->resjunk)
214 tlist_item = lnext(targetlist, tlist_item);
215 if (tlist_item)
216 {
217 TargetEntry *tle = (TargetEntry *) lfirst(tlist_item);
218
219 resorigtbl = tle->resorigtbl;
220 resorigcol = tle->resorigcol;
221 tlist_item = lnext(targetlist, tlist_item);
222 }
223 else
224 {
225 /* No info available, so send zeroes */
226 resorigtbl = 0;
227 resorigcol = 0;
228 }
229
230 if (formats)
231 format = formats[i];
232 else
233 format = 0;
234
235 pq_writestring(buf, NameStr(att->attname));
236 pq_writeint32(buf, resorigtbl);
237 pq_writeint16(buf, resorigcol);
238 pq_writeint32(buf, atttypid);
239 pq_writeint16(buf, att->attlen);
240 pq_writeint32(buf, atttypmod);
242 }
243
245}
int16 AttrNumber
Definition: attnum.h:21
int32_t int32
Definition: c.h:537
Oid getBaseTypeAndTypmod(Oid typid, int32 *typmod)
Definition: lsyscache.c:2705
#define NAMEDATALEN
#define lfirst(lc)
Definition: pg_list.h:172
static ListCell * list_head(const List *l)
Definition: pg_list.h:128
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:343
#define MAX_CONVERSION_GROWTH
Definition: pg_wchar.h:302
static void pq_writeint32(StringInfoData *restrict buf, uint32 i)
Definition: pqformat.h:74
static void pq_writeint16(StringInfoData *restrict buf, uint16 i)
Definition: pqformat.h:60
static void pq_writestring(StringInfoData *restrict buf, const char *restrict str)
Definition: pqformat.h:108
#define PqMsg_RowDescription
Definition: protocol.h:52
void enlargeStringInfo(StringInfo str, int needed)
Definition: stringinfo.c:337

References buf, enlargeStringInfo(), format, getBaseTypeAndTypmod(), i, lfirst, list_head(), lnext(), MAX_CONVERSION_GROWTH, NAMEDATALEN, NameStr, TupleDescData::natts, pq_beginmessage_reuse(), pq_endmessage_reuse(), pq_sendint16(), pq_writeint16(), pq_writeint32(), pq_writestring(), PqMsg_RowDescription, and TupleDescAttr().

Referenced by exec_describe_portal_message(), exec_describe_statement_message(), and printtup_startup().

◆ SetRemoteDestReceiverParams()

void SetRemoteDestReceiverParams ( DestReceiver self,
Portal  portal 
)

Definition at line 101 of file printtup.c.

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}
@ DestRemoteExecute
Definition: dest.h:90
Assert(PointerIsAligned(start, uint64))

References Assert(), DestRemote, DestRemoteExecute, _DestReceiver::mydest, DR_printtup::portal, and DR_printtup::pub.

Referenced by exec_execute_message(), and exec_simple_query().