PostgreSQL Source Code  git master
printtup.c File Reference
#include "postgres.h"
#include "access/printtup.h"
#include "libpq/pqformat.h"
#include "tcop/pquery.h"
#include "utils/lsyscache.h"
#include "utils/memdebug.h"
#include "utils/memutils.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 443 of file printtup.c.

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

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

◆ debugtup()

bool debugtup ( TupleTableSlot slot,
DestReceiver self 
)

Definition at line 461 of file printtup.c.

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

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 422 of file printtup.c.

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

References NameStr, printf, and value.

Referenced by debugStartup(), and debugtup().

◆ printtup()

static bool printtup ( TupleTableSlot slot,
DestReceiver self 
)
static

Definition at line 303 of file printtup.c.

304 {
305  TupleDesc typeinfo = slot->tts_tupleDescriptor;
306  DR_printtup *myState = (DR_printtup *) self;
307  MemoryContext oldcontext;
308  StringInfo buf = &myState->buf;
309  int natts = typeinfo->natts;
310  int i;
311 
312  /* Set or update my derived attribute info, if needed */
313  if (myState->attrinfo != typeinfo || myState->nattrs != natts)
314  printtup_prepare_info(myState, typeinfo, natts);
315 
316  /* Make sure the tuple is fully deconstructed */
317  slot_getallattrs(slot);
318 
319  /* Switch into per-row context so we can recover memory below */
320  oldcontext = MemoryContextSwitchTo(myState->tmpcontext);
321 
322  /*
323  * Prepare a DataRow message (note buffer is in per-query context)
324  */
326 
327  pq_sendint16(buf, natts);
328 
329  /*
330  * send the attributes of this tuple
331  */
332  for (i = 0; i < natts; ++i)
333  {
334  PrinttupAttrInfo *thisState = myState->myinfo + i;
335  Datum attr = slot->tts_values[i];
336 
337  if (slot->tts_isnull[i])
338  {
339  pq_sendint32(buf, -1);
340  continue;
341  }
342 
343  /*
344  * Here we catch undefined bytes in datums that are returned to the
345  * client without hitting disk; see comments at the related check in
346  * PageAddItem(). This test is most useful for uncompressed,
347  * non-external datums, but we're quite likely to see such here when
348  * testing new C functions.
349  */
350  if (thisState->typisvarlena)
352  VARSIZE_ANY(attr));
353 
354  if (thisState->format == 0)
355  {
356  /* Text output */
357  char *outputstr;
358 
359  outputstr = OutputFunctionCall(&thisState->finfo, attr);
360  pq_sendcountedtext(buf, outputstr, strlen(outputstr));
361  }
362  else
363  {
364  /* Binary output */
365  bytea *outputbytes;
366 
367  outputbytes = SendFunctionCall(&thisState->finfo, attr);
368  pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ);
369  pq_sendbytes(buf, VARDATA(outputbytes),
370  VARSIZE(outputbytes) - VARHDRSZ);
371  }
372  }
373 
375 
376  /* Return to caller's context, and flush row's temporary memory */
377  MemoryContextSwitchTo(oldcontext);
378  MemoryContextReset(myState->tmpcontext);
379 
380  return true;
381 }
#define VARHDRSZ
Definition: c.h:692
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:383
#define VALGRIND_CHECK_MEM_IS_DEFINED(addr, size)
Definition: memdebug.h:23
static char * buf
Definition: pg_test_fsync.c:73
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:312
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:249
MemoryContextSwitchTo(old_ctx)
PrinttupAttrInfo * myinfo
Definition: printtup.c:60
StringInfoData buf
Definition: printtup.c:61
int nattrs
Definition: printtup.c:59
MemoryContext tmpcontext
Definition: printtup.c:62
TupleDesc attrinfo
Definition: printtup.c:58
bool typisvarlena
Definition: printtup.c:48
FmgrInfo finfo
Definition: printtup.c:50
bool * tts_isnull
Definition: tuptable.h:127
Datum * tts_values
Definition: tuptable.h:125
Definition: c.h:687
static void slot_getallattrs(TupleTableSlot *slot)
Definition: tuptable.h:368
#define VARSIZE_ANY(PTR)
Definition: varatt.h:311
#define VARDATA(PTR)
Definition: varatt.h:278
#define VARSIZE(PTR)
Definition: varatt.h:279

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(), 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 70 of file printtup.c.

71 {
72  DR_printtup *self = (DR_printtup *) palloc0(sizeof(DR_printtup));
73 
74  self->pub.receiveSlot = printtup; /* might get changed later */
75  self->pub.rStartup = printtup_startup;
76  self->pub.rShutdown = printtup_shutdown;
77  self->pub.rDestroy = printtup_destroy;
78  self->pub.mydest = dest;
79 
80  /*
81  * Send T message automatically if DestRemote, but not if
82  * DestRemoteExecute
83  */
84  self->sendDescrip = (dest == DestRemote);
85 
86  self->attrinfo = NULL;
87  self->nattrs = 0;
88  self->myinfo = NULL;
89  self->buf.data = NULL;
90  self->tmpcontext = NULL;
91 
92  return (DestReceiver *) self;
93 }
@ DestRemote
Definition: dest.h:89
void * palloc0(Size size)
Definition: mcxt.c:1346
static bool printtup(TupleTableSlot *slot, DestReceiver *self)
Definition: printtup.c:303
static void printtup_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
Definition: printtup.c:110
static void printtup_destroy(DestReceiver *self)
Definition: printtup.c:412
static void printtup_shutdown(DestReceiver *self)
Definition: printtup.c:388

References generate_unaccent_rules::dest, DestRemote, palloc0(), printtup(), printtup_destroy(), printtup_shutdown(), and printtup_startup().

Referenced by CreateDestReceiver().

◆ printtup_destroy()

static void printtup_destroy ( DestReceiver self)
static

Definition at line 412 of file printtup.c.

413 {
414  pfree(self);
415 }
void pfree(void *pointer)
Definition: mcxt.c:1520

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 249 of file printtup.c.

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  Form_pg_attribute attr = TupleDescAttr(typeinfo, i);
272 
273  thisState->format = format;
274  if (format == 0)
275  {
276  getTypeOutputInfo(attr->atttypid,
277  &thisState->typoutput,
278  &thisState->typisvarlena);
279  fmgr_info(thisState->typoutput, &thisState->finfo);
280  }
281  else if (format == 1)
282  {
283  getTypeBinaryOutputInfo(attr->atttypid,
284  &thisState->typsend,
285  &thisState->typisvarlena);
286  fmgr_info(thisState->typsend, &thisState->finfo);
287  }
288  else
289  ereport(ERROR,
290  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
291  errmsg("unsupported format code: %d", format)));
292  }
293 }
signed short int16
Definition: c.h:493
int errcode(int sqlerrcode)
Definition: elog.c:859
int errmsg(const char *fmt,...)
Definition: elog.c:1072
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:127
void getTypeBinaryOutputInfo(Oid type, Oid *typSend, bool *typIsVarlena)
Definition: lsyscache.c:2973
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:209
static char format
Portal portal
Definition: printtup.c:56
int16 * formats
Definition: portal.h:162

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 388 of file printtup.c.

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

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 110 of file printtup.c.

111 {
112  DR_printtup *myState = (DR_printtup *) self;
113  Portal portal = myState->portal;
114 
115  /*
116  * Create I/O buffer to be used for all messages. This cannot be inside
117  * tmpcontext, since we want to re-use it across rows.
118  */
119  initStringInfo(&myState->buf);
120 
121  /*
122  * Create a temporary memory context that we can reset once per row to
123  * recover palloc'd memory. This avoids any problems with leaks inside
124  * datatype output routines, and should be faster than retail pfree's
125  * anyway.
126  */
128  "printtup",
130 
131  /*
132  * If we are supposed to emit row descriptions, then send the tuple
133  * descriptor of the tuples.
134  */
135  if (myState->sendDescrip)
136  SendRowDescriptionMessage(&myState->buf,
137  typeinfo,
138  FetchPortalTargetList(portal),
139  portal->formats);
140 
141  /* ----------------
142  * We could set up the derived attr info at this time, but we postpone it
143  * until the first call of printtup, for 2 reasons:
144  * 1. We don't waste time (compared to the old way) if there are no
145  * tuples at all to output.
146  * 2. Checking in printtup allows us to handle the case that the tuples
147  * change type midway through (although this probably can't happen in
148  * the current executor).
149  * ----------------
150  */
151 }
MemoryContext CurrentMemoryContext
Definition: mcxt.c:143
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:160
List * FetchPortalTargetList(Portal portal)
Definition: pquery.c:326
void SendRowDescriptionMessage(StringInfo buf, TupleDesc typeinfo, List *targetlist, int16 *formats)
Definition: printtup.c:165
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
bool sendDescrip
Definition: printtup.c:57

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 165 of file printtup.c.

167 {
168  int natts = typeinfo->natts;
169  int i;
170  ListCell *tlist_item = list_head(targetlist);
171 
172  /* tuple descriptor message type */
174  /* # of attrs in tuples */
175  pq_sendint16(buf, natts);
176 
177  /*
178  * Preallocate memory for the entire message to be sent. That allows to
179  * use the significantly faster inline pqformat.h functions and to avoid
180  * reallocations.
181  *
182  * Have to overestimate the size of the column-names, to account for
183  * character set overhead.
184  */
186  + sizeof(Oid) /* resorigtbl */
187  + sizeof(AttrNumber) /* resorigcol */
188  + sizeof(Oid) /* atttypid */
189  + sizeof(int16) /* attlen */
190  + sizeof(int32) /* attypmod */
191  + sizeof(int16) /* format */
192  ) * natts);
193 
194  for (i = 0; i < natts; ++i)
195  {
196  Form_pg_attribute att = TupleDescAttr(typeinfo, i);
197  Oid atttypid = att->atttypid;
198  int32 atttypmod = att->atttypmod;
199  Oid resorigtbl;
200  AttrNumber resorigcol;
201  int16 format;
202 
203  /*
204  * If column is a domain, send the base type and typmod instead.
205  * Lookup before sending any ints, for efficiency.
206  */
207  atttypid = getBaseTypeAndTypmod(atttypid, &atttypmod);
208 
209  /* Do we have a non-resjunk tlist item? */
210  while (tlist_item &&
211  ((TargetEntry *) lfirst(tlist_item))->resjunk)
212  tlist_item = lnext(targetlist, tlist_item);
213  if (tlist_item)
214  {
215  TargetEntry *tle = (TargetEntry *) lfirst(tlist_item);
216 
217  resorigtbl = tle->resorigtbl;
218  resorigcol = tle->resorigcol;
219  tlist_item = lnext(targetlist, tlist_item);
220  }
221  else
222  {
223  /* No info available, so send zeroes */
224  resorigtbl = 0;
225  resorigcol = 0;
226  }
227 
228  if (formats)
229  format = formats[i];
230  else
231  format = 0;
232 
233  pq_writestring(buf, NameStr(att->attname));
234  pq_writeint32(buf, resorigtbl);
235  pq_writeint16(buf, resorigcol);
236  pq_writeint32(buf, atttypid);
237  pq_writeint16(buf, att->attlen);
238  pq_writeint32(buf, atttypmod);
240  }
241 
243 }
int16 AttrNumber
Definition: attnum.h:21
signed int int32
Definition: c.h:494
Oid getBaseTypeAndTypmod(Oid typid, int32 *typmod)
Definition: lsyscache.c:2538
#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_writestring(StringInfoData *pg_restrict buf, const char *pg_restrict str)
Definition: pqformat.h:108
static void pq_writeint32(StringInfoData *pg_restrict buf, uint32 i)
Definition: pqformat.h:74
static void pq_writeint16(StringInfoData *pg_restrict buf, uint16 i)
Definition: pqformat.h:60
void enlargeStringInfo(StringInfo str, int needed)
Definition: stringinfo.c:289

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(), 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 99 of file printtup.c.

100 {
101  DR_printtup *myState = (DR_printtup *) self;
102 
103  Assert(myState->pub.mydest == DestRemote ||
104  myState->pub.mydest == DestRemoteExecute);
105 
106  myState->portal = portal;
107 }
#define Assert(condition)
Definition: c.h:858
@ DestRemoteExecute
Definition: dest.h:90
DestReceiver pub
Definition: printtup.c:55
CommandDest mydest
Definition: dest.h:128

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

Referenced by exec_execute_message(), and exec_simple_query().