PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
printtup.c File Reference
#include "postgres.h"
#include "access/printtup.h"
#include "libpq/libpq.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 bool printtup_20 (TupleTableSlot *slot, DestReceiver *self)
 
static bool printtup_internal_20 (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 (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

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

Definition at line 514 of file printtup.c.

References i, tupleDesc::natts, NULL, printatt(), and TupleDescAttr.

515 {
516  int natts = typeinfo->natts;
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, TupleDescAttr(typeinfo, i), NULL);
524  printf("\t----\n");
525 }
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:84
int natts
Definition: tupdesc.h:73
static void printatt(unsigned attributeId, Form_pg_attribute attributeP, char *value)
Definition: printtup.c:493
#define NULL
Definition: c.h:229
int i
bool debugtup ( TupleTableSlot slot,
DestReceiver self 
)

Definition at line 532 of file printtup.c.

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

Referenced by print_slot().

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(TupleDescAttr(typeinfo, i)->atttypid,
549  &typoutput, &typisvarlena);
550 
551  value = OidOutputFunctionCall(typoutput, attr);
552 
553  printatt((unsigned) i + 1, TupleDescAttr(typeinfo, i), value);
554  }
555  printf("\t----\n");
556 
557  return true;
558 }
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2632
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:84
unsigned int Oid
Definition: postgres_ext.h:31
int natts
Definition: tupdesc.h:73
static struct @121 value
TupleDesc tts_tupleDescriptor
Definition: tuptable.h:121
static void printatt(unsigned attributeId, Form_pg_attribute attributeP, char *value)
Definition: printtup.c:493
uintptr_t Datum
Definition: postgres.h:372
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:1747
int i
Datum slot_getattr(TupleTableSlot *slot, int attnum, bool *isnull)
Definition: heaptuple.c:1142
static void printatt ( unsigned  attributeId,
Form_pg_attribute  attributeP,
char *  value 
)
static

Definition at line 493 of file printtup.c.

References NameStr, and NULL.

Referenced by debugStartup(), and debugtup().

496 {
497  printf("\t%2d: %s%s%s%s\t(typeid = %u, len = %d, typmod = %d, byval = %c)\n",
498  attributeId,
499  NameStr(attributeP->attname),
500  value != NULL ? " = \"" : "",
501  value != NULL ? value : "",
502  value != NULL ? "\"" : "",
503  (unsigned int) (attributeP->atttypid),
504  attributeP->attlen,
505  attributeP->atttypmod,
506  attributeP->attbyval ? 't' : 'f');
507 }
static struct @121 value
#define NULL
Definition: c.h:229
#define NameStr(name)
Definition: c.h:499
static bool printtup ( TupleTableSlot slot,
DestReceiver self 
)
static

Definition at line 300 of file printtup.c.

References DR_printtup::attrinfo, buf, DatumGetPointer, PrinttupAttrInfo::finfo, PrinttupAttrInfo::format, i, MemoryContextReset(), MemoryContextSwitchTo(), DR_printtup::myinfo, DR_printtup::nattrs, tupleDesc::natts, OutputFunctionCall(), pq_beginmessage(), pq_endmessage(), pq_sendbytes(), pq_sendcountedtext(), pq_sendint(), 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().

301 {
302  TupleDesc typeinfo = slot->tts_tupleDescriptor;
303  DR_printtup *myState = (DR_printtup *) self;
304  MemoryContext oldcontext;
306  int natts = typeinfo->natts;
307  int i;
308 
309  /* Set or update my derived attribute info, if needed */
310  if (myState->attrinfo != typeinfo || myState->nattrs != natts)
311  printtup_prepare_info(myState, typeinfo, natts);
312 
313  /* Make sure the tuple is fully deconstructed */
314  slot_getallattrs(slot);
315 
316  /* Switch into per-row context so we can recover memory below */
317  oldcontext = MemoryContextSwitchTo(myState->tmpcontext);
318 
319  /*
320  * Prepare a DataRow message (note buffer is in per-row context)
321  */
322  pq_beginmessage(&buf, 'D');
323 
324  pq_sendint(&buf, natts, 2);
325 
326  /*
327  * send the attributes of this tuple
328  */
329  for (i = 0; i < natts; ++i)
330  {
331  PrinttupAttrInfo *thisState = myState->myinfo + i;
332  Datum attr = slot->tts_values[i];
333 
334  if (slot->tts_isnull[i])
335  {
336  pq_sendint(&buf, -1, 4);
337  continue;
338  }
339 
340  /*
341  * Here we catch undefined bytes in datums that are returned to the
342  * client without hitting disk; see comments at the related check in
343  * PageAddItem(). This test is most useful for uncompressed,
344  * non-external datums, but we're quite likely to see such here when
345  * testing new C functions.
346  */
347  if (thisState->typisvarlena)
349  VARSIZE_ANY(attr));
350 
351  if (thisState->format == 0)
352  {
353  /* Text output */
354  char *outputstr;
355 
356  outputstr = OutputFunctionCall(&thisState->finfo, attr);
357  pq_sendcountedtext(&buf, outputstr, strlen(outputstr), false);
358  }
359  else
360  {
361  /* Binary output */
362  bytea *outputbytes;
363 
364  outputbytes = SendFunctionCall(&thisState->finfo, attr);
365  pq_sendint(&buf, VARSIZE(outputbytes) - VARHDRSZ, 4);
366  pq_sendbytes(&buf, VARDATA(outputbytes),
367  VARSIZE(outputbytes) - VARHDRSZ);
368  }
369  }
370 
371  pq_endmessage(&buf);
372 
373  /* Return to caller's context, and flush row's temporary memory */
374  MemoryContextSwitchTo(oldcontext);
375  MemoryContextReset(myState->tmpcontext);
376 
377  return true;
378 }
#define VARDATA(PTR)
Definition: postgres.h:303
#define VARSIZE(PTR)
Definition: postgres.h:304
#define VARHDRSZ
Definition: c.h:445
#define VALGRIND_CHECK_MEM_IS_DEFINED(addr, size)
Definition: memdebug.h:23
static void printtup_prepare_info(DR_printtup *myState, TupleDesc typeinfo, int numAttrs)
Definition: printtup.c:249
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Datum * tts_values
Definition: tuptable.h:125
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:135
int natts
Definition: tupdesc.h:73
void pq_beginmessage(StringInfo buf, char msgtype)
Definition: pqformat.c:88
char * OutputFunctionCall(FmgrInfo *flinfo, Datum val)
Definition: fmgr.c:1667
TupleDesc attrinfo
Definition: printtup.c:62
PrinttupAttrInfo * myinfo
Definition: printtup.c:64
static char * buf
Definition: pg_test_fsync.c:66
bool * tts_isnull
Definition: tuptable.h:126
bytea * SendFunctionCall(FmgrInfo *flinfo, Datum val)
Definition: fmgr.c:1728
int nattrs
Definition: printtup.c:63
void pq_sendcountedtext(StringInfo buf, const char *str, int slen, bool countincludesself)
Definition: pqformat.c:131
void slot_getallattrs(TupleTableSlot *slot)
Definition: heaptuple.c:1238
MemoryContext tmpcontext
Definition: printtup.c:65
TupleDesc tts_tupleDescriptor
Definition: tuptable.h:121
uintptr_t Datum
Definition: postgres.h:372
#define VARSIZE_ANY(PTR)
Definition: postgres.h:334
bool typisvarlena
Definition: printtup.c:52
#define DatumGetPointer(X)
Definition: postgres.h:555
void pq_sendbytes(StringInfo buf, const char *data, int datalen)
Definition: pqformat.c:115
void pq_sendint(StringInfo buf, int i, int b)
Definition: pqformat.c:236
void pq_endmessage(StringInfo buf)
Definition: pqformat.c:344
int i
Definition: c.h:439
FmgrInfo finfo
Definition: printtup.c:54
static bool printtup_20 ( TupleTableSlot slot,
DestReceiver self 
)
static

Definition at line 385 of file printtup.c.

References Assert, DR_printtup::attrinfo, buf, PrinttupAttrInfo::finfo, PrinttupAttrInfo::format, i, MemoryContextReset(), MemoryContextSwitchTo(), DR_printtup::myinfo, DR_printtup::nattrs, tupleDesc::natts, OutputFunctionCall(), pq_beginmessage(), pq_endmessage(), pq_sendcountedtext(), pq_sendint(), printtup_prepare_info(), slot_getallattrs(), DR_printtup::tmpcontext, TupleTableSlot::tts_isnull, TupleTableSlot::tts_tupleDescriptor, and TupleTableSlot::tts_values.

Referenced by SetRemoteDestReceiverParams().

386 {
387  TupleDesc typeinfo = slot->tts_tupleDescriptor;
388  DR_printtup *myState = (DR_printtup *) self;
389  MemoryContext oldcontext;
391  int natts = typeinfo->natts;
392  int i,
393  j,
394  k;
395 
396  /* Set or update my derived attribute info, if needed */
397  if (myState->attrinfo != typeinfo || myState->nattrs != natts)
398  printtup_prepare_info(myState, typeinfo, natts);
399 
400  /* Make sure the tuple is fully deconstructed */
401  slot_getallattrs(slot);
402 
403  /* Switch into per-row context so we can recover memory below */
404  oldcontext = MemoryContextSwitchTo(myState->tmpcontext);
405 
406  /*
407  * tell the frontend to expect new tuple data (in ASCII style)
408  */
409  pq_beginmessage(&buf, 'D');
410 
411  /*
412  * send a bitmap of which attributes are not null
413  */
414  j = 0;
415  k = 1 << 7;
416  for (i = 0; i < natts; ++i)
417  {
418  if (!slot->tts_isnull[i])
419  j |= k; /* set bit if not null */
420  k >>= 1;
421  if (k == 0) /* end of byte? */
422  {
423  pq_sendint(&buf, j, 1);
424  j = 0;
425  k = 1 << 7;
426  }
427  }
428  if (k != (1 << 7)) /* flush last partial byte */
429  pq_sendint(&buf, j, 1);
430 
431  /*
432  * send the attributes of this tuple
433  */
434  for (i = 0; i < natts; ++i)
435  {
436  PrinttupAttrInfo *thisState = myState->myinfo + i;
437  Datum attr = slot->tts_values[i];
438  char *outputstr;
439 
440  if (slot->tts_isnull[i])
441  continue;
442 
443  Assert(thisState->format == 0);
444 
445  outputstr = OutputFunctionCall(&thisState->finfo, attr);
446  pq_sendcountedtext(&buf, outputstr, strlen(outputstr), true);
447  }
448 
449  pq_endmessage(&buf);
450 
451  /* Return to caller's context, and flush row's temporary memory */
452  MemoryContextSwitchTo(oldcontext);
453  MemoryContextReset(myState->tmpcontext);
454 
455  return true;
456 }
static void printtup_prepare_info(DR_printtup *myState, TupleDesc typeinfo, int numAttrs)
Definition: printtup.c:249
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Datum * tts_values
Definition: tuptable.h:125
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:135
int natts
Definition: tupdesc.h:73
void pq_beginmessage(StringInfo buf, char msgtype)
Definition: pqformat.c:88
char * OutputFunctionCall(FmgrInfo *flinfo, Datum val)
Definition: fmgr.c:1667
TupleDesc attrinfo
Definition: printtup.c:62
PrinttupAttrInfo * myinfo
Definition: printtup.c:64
static char * buf
Definition: pg_test_fsync.c:66
bool * tts_isnull
Definition: tuptable.h:126
int nattrs
Definition: printtup.c:63
void pq_sendcountedtext(StringInfo buf, const char *str, int slen, bool countincludesself)
Definition: pqformat.c:131
void slot_getallattrs(TupleTableSlot *slot)
Definition: heaptuple.c:1238
MemoryContext tmpcontext
Definition: printtup.c:65
TupleDesc tts_tupleDescriptor
Definition: tuptable.h:121
uintptr_t Datum
Definition: postgres.h:372
#define Assert(condition)
Definition: c.h:676
void pq_sendint(StringInfo buf, int i, int b)
Definition: pqformat.c:236
void pq_endmessage(StringInfo buf)
Definition: pqformat.c:344
int i
FmgrInfo finfo
Definition: printtup.c:54
DestReceiver* printtup_create_DR ( CommandDest  dest)

Definition at line 73 of file printtup.c.

References DestRemote, NULL, palloc0(), printtup(), printtup_destroy(), printtup_shutdown(), and printtup_startup().

Referenced by CreateDestReceiver().

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 }
void * palloc0(Size size)
Definition: mcxt.c:878
static void printtup_destroy(DestReceiver *self)
Definition: printtup.c:483
#define NULL
Definition: c.h:229
static void printtup_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
Definition: printtup.c:125
static bool printtup(TupleTableSlot *slot, DestReceiver *self)
Definition: printtup.c:300
static void printtup_shutdown(DestReceiver *self)
Definition: printtup.c:463
static void printtup_destroy ( DestReceiver self)
static

Definition at line 483 of file printtup.c.

References pfree().

Referenced by printtup_create_DR().

484 {
485  pfree(self);
486 }
void pfree(void *pointer)
Definition: mcxt.c:950
static bool printtup_internal_20 ( TupleTableSlot slot,
DestReceiver self 
)
static

Definition at line 570 of file printtup.c.

References Assert, DR_printtup::attrinfo, buf, PrinttupAttrInfo::finfo, PrinttupAttrInfo::format, i, MemoryContextReset(), MemoryContextSwitchTo(), DR_printtup::myinfo, DR_printtup::nattrs, tupleDesc::natts, pq_beginmessage(), pq_endmessage(), pq_sendbytes(), pq_sendint(), printtup_prepare_info(), SendFunctionCall(), slot_getallattrs(), DR_printtup::tmpcontext, TupleTableSlot::tts_isnull, TupleTableSlot::tts_tupleDescriptor, TupleTableSlot::tts_values, VARDATA, VARHDRSZ, and VARSIZE.

Referenced by SetRemoteDestReceiverParams().

571 {
572  TupleDesc typeinfo = slot->tts_tupleDescriptor;
573  DR_printtup *myState = (DR_printtup *) self;
574  MemoryContext oldcontext;
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 }
#define VARDATA(PTR)
Definition: postgres.h:303
#define VARSIZE(PTR)
Definition: postgres.h:304
#define VARHDRSZ
Definition: c.h:445
static void printtup_prepare_info(DR_printtup *myState, TupleDesc typeinfo, int numAttrs)
Definition: printtup.c:249
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Datum * tts_values
Definition: tuptable.h:125
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:135
int natts
Definition: tupdesc.h:73
void pq_beginmessage(StringInfo buf, char msgtype)
Definition: pqformat.c:88
TupleDesc attrinfo
Definition: printtup.c:62
PrinttupAttrInfo * myinfo
Definition: printtup.c:64
static char * buf
Definition: pg_test_fsync.c:66
bool * tts_isnull
Definition: tuptable.h:126
bytea * SendFunctionCall(FmgrInfo *flinfo, Datum val)
Definition: fmgr.c:1728
int nattrs
Definition: printtup.c:63
void slot_getallattrs(TupleTableSlot *slot)
Definition: heaptuple.c:1238
MemoryContext tmpcontext
Definition: printtup.c:65
TupleDesc tts_tupleDescriptor
Definition: tuptable.h:121
uintptr_t Datum
Definition: postgres.h:372
#define Assert(condition)
Definition: c.h:676
void pq_sendbytes(StringInfo buf, const char *data, int datalen)
Definition: pqformat.c:115
void pq_sendint(StringInfo buf, int i, int b)
Definition: pqformat.c:236
void pq_endmessage(StringInfo buf)
Definition: pqformat.c:344
int i
Definition: c.h:439
FmgrInfo finfo
Definition: printtup.c:54
static void printtup_prepare_info ( DR_printtup myState,
TupleDesc  typeinfo,
int  numAttrs 
)
static

Definition at line 249 of file printtup.c.

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, NULL, palloc0(), pfree(), DR_printtup::portal, TupleDescAttr, PrinttupAttrInfo::typisvarlena, PrinttupAttrInfo::typoutput, and PrinttupAttrInfo::typsend.

Referenced by printtup(), printtup_20(), and printtup_internal_20().

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:255
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2632
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:84
int errcode(int sqlerrcode)
Definition: elog.c:575
TupleDesc attrinfo
Definition: printtup.c:62
void pfree(void *pointer)
Definition: mcxt.c:950
#define ERROR
Definition: elog.h:43
Portal portal
Definition: printtup.c:60
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:127
PrinttupAttrInfo * myinfo
Definition: printtup.c:64
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
int nattrs
Definition: printtup.c:63
#define ereport(elevel, rest)
Definition: elog.h:122
void getTypeBinaryOutputInfo(Oid type, Oid *typSend, bool *typIsVarlena)
Definition: lsyscache.c:2698
void * palloc0(Size size)
Definition: mcxt.c:878
int16 * formats
Definition: portal.h:157
#define NULL
Definition: c.h:229
bool typisvarlena
Definition: printtup.c:52
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
static char format
Definition: pg_basebackup.c:80
FmgrInfo finfo
Definition: printtup.c:54
static void printtup_shutdown ( DestReceiver self)
static

Definition at line 463 of file printtup.c.

References DR_printtup::attrinfo, MemoryContextDelete(), DR_printtup::myinfo, NULL, pfree(), and DR_printtup::tmpcontext.

Referenced by printtup_create_DR().

464 {
465  DR_printtup *myState = (DR_printtup *) self;
466 
467  if (myState->myinfo)
468  pfree(myState->myinfo);
469  myState->myinfo = NULL;
470 
471  myState->attrinfo = NULL;
472 
473  if (myState->tmpcontext)
475  myState->tmpcontext = NULL;
476 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
TupleDesc attrinfo
Definition: printtup.c:62
void pfree(void *pointer)
Definition: mcxt.c:950
PrinttupAttrInfo * myinfo
Definition: printtup.c:64
MemoryContext tmpcontext
Definition: printtup.c:65
#define NULL
Definition: c.h:229
static void printtup_startup ( DestReceiver self,
int  operation,
TupleDesc  typeinfo 
)
static

Definition at line 125 of file printtup.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate(), CurrentMemoryContext, FetchPortalTargetList(), PortalData::formats, FrontendProtocol, PortalData::name, NULL, PG_PROTOCOL_MAJOR, DR_printtup::portal, pq_puttextmessage(), DR_printtup::sendDescrip, SendRowDescriptionMessage(), and DR_printtup::tmpcontext.

Referenced by printtup_create_DR().

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  */
137  "printtup",
139 
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 }
#define PG_PROTOCOL_MAJOR(v)
Definition: pqcomm.h:104
void SendRowDescriptionMessage(TupleDesc typeinfo, List *targetlist, int16 *formats)
Definition: printtup.c:188
const char * name
Definition: portal.h:117
Portal portal
Definition: printtup.c:60
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:165
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
MemoryContext tmpcontext
Definition: printtup.c:65
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:322
bool sendDescrip
Definition: printtup.c:61
int16 * formats
Definition: portal.h:157
#define NULL
Definition: c.h:229
List * FetchPortalTargetList(Portal portal)
Definition: pquery.c:338
void pq_puttextmessage(char msgtype, const char *str)
Definition: pqformat.c:400
ProtocolVersion FrontendProtocol
Definition: globals.c:27
void SendRowDescriptionMessage ( TupleDesc  typeinfo,
List targetlist,
int16 formats 
)

Definition at line 188 of file printtup.c.

References buf, FrontendProtocol, getBaseTypeAndTypmod(), i, lfirst, list_head(), lnext, NameStr, tupleDesc::natts, PG_PROTOCOL_MAJOR, pq_beginmessage(), pq_endmessage(), pq_sendint(), pq_sendstring(), TargetEntry::resorigcol, TargetEntry::resorigtbl, and TupleDescAttr.

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

189 {
190  int natts = typeinfo->natts;
191  int proto = PG_PROTOCOL_MAJOR(FrontendProtocol);
192  int i;
194  ListCell *tlist_item = list_head(targetlist);
195 
196  pq_beginmessage(&buf, 'T'); /* tuple descriptor message type */
197  pq_sendint(&buf, natts, 2); /* # of attrs in tuples */
198 
199  for (i = 0; i < natts; ++i)
200  {
201  Form_pg_attribute att = TupleDescAttr(typeinfo, i);
202  Oid atttypid = att->atttypid;
203  int32 atttypmod = att->atttypmod;
204 
205  pq_sendstring(&buf, NameStr(att->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, att->attlen, sizeof(att->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 }
Oid getBaseTypeAndTypmod(Oid typid, int32 *typmod)
Definition: lsyscache.c:2288
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:84
Oid resorigtbl
Definition: primnodes.h:1373
void pq_sendstring(StringInfo buf, const char *str)
Definition: pqformat.c:186
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_PROTOCOL_MAJOR(v)
Definition: pqcomm.h:104
int natts
Definition: tupdesc.h:73
void pq_beginmessage(StringInfo buf, char msgtype)
Definition: pqformat.c:88
signed int int32
Definition: c.h:256
static char * buf
Definition: pg_test_fsync.c:66
static ListCell * list_head(const List *l)
Definition: pg_list.h:77
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
#define lnext(lc)
Definition: pg_list.h:105
#define lfirst(lc)
Definition: pg_list.h:106
AttrNumber resorigcol
Definition: primnodes.h:1374
void pq_sendint(StringInfo buf, int i, int b)
Definition: pqformat.c:236
void pq_endmessage(StringInfo buf)
Definition: pqformat.c:344
int i
#define NameStr(name)
Definition: c.h:499
ProtocolVersion FrontendProtocol
Definition: globals.c:27
void SetRemoteDestReceiverParams ( DestReceiver self,
Portal  portal 
)

Definition at line 101 of file printtup.c.

References Assert, DestRemote, DestRemoteExecute, PortalData::formats, FrontendProtocol, _DestReceiver::mydest, PG_PROTOCOL_MAJOR, DR_printtup::portal, printtup_20(), printtup_internal_20(), DR_printtup::pub, and _DestReceiver::receiveSlot.

Referenced by exec_execute_message(), and exec_simple_query().

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 
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)
119  else
120  myState->pub.receiveSlot = printtup_20;
121  }
122 }
static bool printtup_internal_20(TupleTableSlot *slot, DestReceiver *self)
Definition: printtup.c:570
DestReceiver pub
Definition: printtup.c:59
CommandDest mydest
Definition: dest.h:128
#define PG_PROTOCOL_MAJOR(v)
Definition: pqcomm.h:104
Portal portal
Definition: printtup.c:60
int16 * formats
Definition: portal.h:157
#define Assert(condition)
Definition: c.h:676
static bool printtup_20(TupleTableSlot *slot, DestReceiver *self)
Definition: printtup.c:385
bool(* receiveSlot)(TupleTableSlot *slot, DestReceiver *self)
Definition: dest.h:118
ProtocolVersion FrontendProtocol
Definition: globals.c:27