PostgreSQL Source Code  git master
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)
 
static void SendRowDescriptionCols_2 (StringInfo buf, TupleDesc typeinfo, List *targetlist, int16 *formats)
 
static void SendRowDescriptionCols_3 (StringInfo buf, TupleDesc typeinfo, List *targetlist, int16 *formats)
 
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 596 of file printtup.c.

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

597 {
598  int natts = typeinfo->natts;
599  int i;
600 
601  /*
602  * show the return type of the tuples
603  */
604  for (i = 0; i < natts; ++i)
605  printatt((unsigned) i + 1, TupleDescAttr(typeinfo, i), NULL);
606  printf("\t----\n");
607 }
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
#define printf(...)
Definition: port.h:198
static void printatt(unsigned attributeId, Form_pg_attribute attributeP, char *value)
Definition: printtup.c:575
int i

◆ debugtup()

bool debugtup ( TupleTableSlot slot,
DestReceiver self 
)

Definition at line 614 of file printtup.c.

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

Referenced by print_slot().

615 {
616  TupleDesc typeinfo = slot->tts_tupleDescriptor;
617  int natts = typeinfo->natts;
618  int i;
619  Datum attr;
620  char *value;
621  bool isnull;
622  Oid typoutput;
623  bool typisvarlena;
624 
625  for (i = 0; i < natts; ++i)
626  {
627  attr = slot_getattr(slot, i + 1, &isnull);
628  if (isnull)
629  continue;
631  &typoutput, &typisvarlena);
632 
633  value = OidOutputFunctionCall(typoutput, attr);
634 
635  printatt((unsigned) i + 1, TupleDescAttr(typeinfo, i), value);
636  }
637  printf("\t----\n");
638 
639  return true;
640 }
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2674
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
static struct @145 value
#define printf(...)
Definition: port.h:198
unsigned int Oid
Definition: postgres_ext.h:31
Oid atttypid
Definition: pg_attribute.h:49
TupleDesc tts_tupleDescriptor
Definition: tuptable.h:124
static void printatt(unsigned attributeId, Form_pg_attribute attributeP, char *value)
Definition: printtup.c:575
uintptr_t Datum
Definition: postgres.h:367
static Datum slot_getattr(TupleTableSlot *slot, int attnum, bool *isnull)
Definition: tuptable.h:382
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:1655
int i

◆ printatt()

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

Definition at line 575 of file printtup.c.

References NameStr, and printf.

Referenced by debugStartup(), and debugtup().

578 {
579  printf("\t%2d: %s%s%s%s\t(typeid = %u, len = %d, typmod = %d, byval = %c)\n",
580  attributeId,
581  NameStr(attributeP->attname),
582  value != NULL ? " = \"" : "",
583  value != NULL ? value : "",
584  value != NULL ? "\"" : "",
585  (unsigned int) (attributeP->atttypid),
586  attributeP->attlen,
587  attributeP->atttypmod,
588  attributeP->attbyval ? 't' : 'f');
589 }
static struct @145 value
#define printf(...)
Definition: port.h:198
#define NameStr(name)
Definition: c.h:609

◆ printtup()

static bool printtup ( TupleTableSlot slot,
DestReceiver self 
)
static

Definition at line 378 of file printtup.c.

References DR_printtup::attrinfo, buf, DR_printtup::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().

379 {
380  TupleDesc typeinfo = slot->tts_tupleDescriptor;
381  DR_printtup *myState = (DR_printtup *) self;
382  MemoryContext oldcontext;
383  StringInfo buf = &myState->buf;
384  int natts = typeinfo->natts;
385  int i;
386 
387  /* Set or update my derived attribute info, if needed */
388  if (myState->attrinfo != typeinfo || myState->nattrs != natts)
389  printtup_prepare_info(myState, typeinfo, natts);
390 
391  /* Make sure the tuple is fully deconstructed */
392  slot_getallattrs(slot);
393 
394  /* Switch into per-row context so we can recover memory below */
395  oldcontext = MemoryContextSwitchTo(myState->tmpcontext);
396 
397  /*
398  * Prepare a DataRow message (note buffer is in per-row context)
399  */
400  pq_beginmessage_reuse(buf, 'D');
401 
402  pq_sendint16(buf, natts);
403 
404  /*
405  * send the attributes of this tuple
406  */
407  for (i = 0; i < natts; ++i)
408  {
409  PrinttupAttrInfo *thisState = myState->myinfo + i;
410  Datum attr = slot->tts_values[i];
411 
412  if (slot->tts_isnull[i])
413  {
414  pq_sendint32(buf, -1);
415  continue;
416  }
417 
418  /*
419  * Here we catch undefined bytes in datums that are returned to the
420  * client without hitting disk; see comments at the related check in
421  * PageAddItem(). This test is most useful for uncompressed,
422  * non-external datums, but we're quite likely to see such here when
423  * testing new C functions.
424  */
425  if (thisState->typisvarlena)
427  VARSIZE_ANY(attr));
428 
429  if (thisState->format == 0)
430  {
431  /* Text output */
432  char *outputstr;
433 
434  outputstr = OutputFunctionCall(&thisState->finfo, attr);
435  pq_sendcountedtext(buf, outputstr, strlen(outputstr), false);
436  }
437  else
438  {
439  /* Binary output */
440  bytea *outputbytes;
441 
442  outputbytes = SendFunctionCall(&thisState->finfo, attr);
443  pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ);
444  pq_sendbytes(buf, VARDATA(outputbytes),
445  VARSIZE(outputbytes) - VARHDRSZ);
446  }
447  }
448 
449  pq_endmessage_reuse(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 pq_sendint16(StringInfo buf, uint16 i)
Definition: pqformat.h:137
#define VARDATA(PTR)
Definition: postgres.h:302
#define VARSIZE(PTR)
Definition: postgres.h:303
#define VARHDRSZ
Definition: c.h:555
#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:327
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Datum * tts_values
Definition: tuptable.h:126
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:136
char * OutputFunctionCall(FmgrInfo *flinfo, Datum val)
Definition: fmgr.c:1575
static void pq_sendint32(StringInfo buf, uint32 i)
Definition: pqformat.h:145
TupleDesc attrinfo
Definition: printtup.c:66
PrinttupAttrInfo * myinfo
Definition: printtup.c:68
static void slot_getallattrs(TupleTableSlot *slot)
Definition: tuptable.h:355
static char * buf
Definition: pg_test_fsync.c:68
bool * tts_isnull
Definition: tuptable.h:128
bytea * SendFunctionCall(FmgrInfo *flinfo, Datum val)
Definition: fmgr.c:1636
int nattrs
Definition: printtup.c:67
void pq_sendcountedtext(StringInfo buf, const char *str, int slen, bool countincludesself)
Definition: pqformat.c:142
MemoryContext tmpcontext
Definition: printtup.c:70
TupleDesc tts_tupleDescriptor
Definition: tuptable.h:124
uintptr_t Datum
Definition: postgres.h:367
#define VARSIZE_ANY(PTR)
Definition: postgres.h:335
bool typisvarlena
Definition: printtup.c:56
#define DatumGetPointer(X)
Definition: postgres.h:549
void pq_sendbytes(StringInfo buf, const char *data, int datalen)
Definition: pqformat.c:125
int i
StringInfoData buf
Definition: printtup.c:69
Definition: c.h:549
void pq_endmessage_reuse(StringInfo buf)
Definition: pqformat.c:316
FmgrInfo finfo
Definition: printtup.c:58
void pq_beginmessage_reuse(StringInfo buf, char msgtype)
Definition: pqformat.c:108

◆ printtup_20()

static bool printtup_20 ( TupleTableSlot slot,
DestReceiver self 
)
static

Definition at line 463 of file printtup.c.

References Assert, DR_printtup::attrinfo, buf, DR_printtup::buf, PrinttupAttrInfo::finfo, PrinttupAttrInfo::format, i, MemoryContextReset(), MemoryContextSwitchTo(), DR_printtup::myinfo, DR_printtup::nattrs, TupleDescData::natts, OutputFunctionCall(), pq_beginmessage_reuse(), pq_endmessage_reuse(), pq_sendcountedtext(), pq_sendint8(), printtup_prepare_info(), slot_getallattrs(), DR_printtup::tmpcontext, TupleTableSlot::tts_isnull, TupleTableSlot::tts_tupleDescriptor, and TupleTableSlot::tts_values.

Referenced by SetRemoteDestReceiverParams().

464 {
465  TupleDesc typeinfo = slot->tts_tupleDescriptor;
466  DR_printtup *myState = (DR_printtup *) self;
467  MemoryContext oldcontext;
468  StringInfo buf = &myState->buf;
469  int natts = typeinfo->natts;
470  int i,
471  j,
472  k;
473 
474  /* Set or update my derived attribute info, if needed */
475  if (myState->attrinfo != typeinfo || myState->nattrs != natts)
476  printtup_prepare_info(myState, typeinfo, natts);
477 
478  /* Make sure the tuple is fully deconstructed */
479  slot_getallattrs(slot);
480 
481  /* Switch into per-row context so we can recover memory below */
482  oldcontext = MemoryContextSwitchTo(myState->tmpcontext);
483 
484  /*
485  * tell the frontend to expect new tuple data (in ASCII style)
486  */
487  pq_beginmessage_reuse(buf, 'D');
488 
489  /*
490  * send a bitmap of which attributes are not null
491  */
492  j = 0;
493  k = 1 << 7;
494  for (i = 0; i < natts; ++i)
495  {
496  if (!slot->tts_isnull[i])
497  j |= k; /* set bit if not null */
498  k >>= 1;
499  if (k == 0) /* end of byte? */
500  {
501  pq_sendint8(buf, j);
502  j = 0;
503  k = 1 << 7;
504  }
505  }
506  if (k != (1 << 7)) /* flush last partial byte */
507  pq_sendint8(buf, j);
508 
509  /*
510  * send the attributes of this tuple
511  */
512  for (i = 0; i < natts; ++i)
513  {
514  PrinttupAttrInfo *thisState = myState->myinfo + i;
515  Datum attr = slot->tts_values[i];
516  char *outputstr;
517 
518  if (slot->tts_isnull[i])
519  continue;
520 
521  Assert(thisState->format == 0);
522 
523  outputstr = OutputFunctionCall(&thisState->finfo, attr);
524  pq_sendcountedtext(buf, outputstr, strlen(outputstr), true);
525  }
526 
527  pq_endmessage_reuse(buf);
528 
529  /* Return to caller's context, and flush row's temporary memory */
530  MemoryContextSwitchTo(oldcontext);
531  MemoryContextReset(myState->tmpcontext);
532 
533  return true;
534 }
static void printtup_prepare_info(DR_printtup *myState, TupleDesc typeinfo, int numAttrs)
Definition: printtup.c:327
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Datum * tts_values
Definition: tuptable.h:126
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:136
char * OutputFunctionCall(FmgrInfo *flinfo, Datum val)
Definition: fmgr.c:1575
TupleDesc attrinfo
Definition: printtup.c:66
PrinttupAttrInfo * myinfo
Definition: printtup.c:68
static void slot_getallattrs(TupleTableSlot *slot)
Definition: tuptable.h:355
static char * buf
Definition: pg_test_fsync.c:68
bool * tts_isnull
Definition: tuptable.h:128
int nattrs
Definition: printtup.c:67
void pq_sendcountedtext(StringInfo buf, const char *str, int slen, bool countincludesself)
Definition: pqformat.c:142
MemoryContext tmpcontext
Definition: printtup.c:70
TupleDesc tts_tupleDescriptor
Definition: tuptable.h:124
uintptr_t Datum
Definition: postgres.h:367
#define Assert(condition)
Definition: c.h:732
int i
StringInfoData buf
Definition: printtup.c:69
static void pq_sendint8(StringInfo buf, uint8 i)
Definition: pqformat.h:129
void pq_endmessage_reuse(StringInfo buf)
Definition: pqformat.c:316
FmgrInfo finfo
Definition: printtup.c:58
void pq_beginmessage_reuse(StringInfo buf, char msgtype)
Definition: pqformat.c:108

◆ printtup_create_DR()

DestReceiver* printtup_create_DR ( CommandDest  dest)

Definition at line 78 of file printtup.c.

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

Referenced by CreateDestReceiver().

79 {
80  DR_printtup *self = (DR_printtup *) palloc0(sizeof(DR_printtup));
81 
82  self->pub.receiveSlot = printtup; /* might get changed later */
83  self->pub.rStartup = printtup_startup;
84  self->pub.rShutdown = printtup_shutdown;
85  self->pub.rDestroy = printtup_destroy;
86  self->pub.mydest = dest;
87 
88  /*
89  * Send T message automatically if DestRemote, but not if
90  * DestRemoteExecute
91  */
92  self->sendDescrip = (dest == DestRemote);
93 
94  self->attrinfo = NULL;
95  self->nattrs = 0;
96  self->myinfo = NULL;
97  self->buf.data = NULL;
98  self->tmpcontext = NULL;
99 
100  return (DestReceiver *) self;
101 }
void * palloc0(Size size)
Definition: mcxt.c:980
static void printtup_destroy(DestReceiver *self)
Definition: printtup.c:565
static void printtup_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
Definition: printtup.c:131
static bool printtup(TupleTableSlot *slot, DestReceiver *self)
Definition: printtup.c:378
static void printtup_shutdown(DestReceiver *self)
Definition: printtup.c:541

◆ printtup_destroy()

static void printtup_destroy ( DestReceiver self)
static

Definition at line 565 of file printtup.c.

References pfree().

Referenced by printtup_create_DR().

566 {
567  pfree(self);
568 }
void pfree(void *pointer)
Definition: mcxt.c:1056

◆ printtup_internal_20()

static bool printtup_internal_20 ( TupleTableSlot slot,
DestReceiver self 
)
static

Definition at line 652 of file printtup.c.

References Assert, DR_printtup::attrinfo, buf, DR_printtup::buf, PrinttupAttrInfo::finfo, PrinttupAttrInfo::format, i, MemoryContextReset(), MemoryContextSwitchTo(), DR_printtup::myinfo, DR_printtup::nattrs, TupleDescData::natts, pq_beginmessage_reuse(), pq_endmessage_reuse(), pq_sendbytes(), pq_sendint32(), pq_sendint8(), 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().

653 {
654  TupleDesc typeinfo = slot->tts_tupleDescriptor;
655  DR_printtup *myState = (DR_printtup *) self;
656  MemoryContext oldcontext;
657  StringInfo buf = &myState->buf;
658  int natts = typeinfo->natts;
659  int i,
660  j,
661  k;
662 
663  /* Set or update my derived attribute info, if needed */
664  if (myState->attrinfo != typeinfo || myState->nattrs != natts)
665  printtup_prepare_info(myState, typeinfo, natts);
666 
667  /* Make sure the tuple is fully deconstructed */
668  slot_getallattrs(slot);
669 
670  /* Switch into per-row context so we can recover memory below */
671  oldcontext = MemoryContextSwitchTo(myState->tmpcontext);
672 
673  /*
674  * tell the frontend to expect new tuple data (in binary style)
675  */
676  pq_beginmessage_reuse(buf, 'B');
677 
678  /*
679  * send a bitmap of which attributes are not null
680  */
681  j = 0;
682  k = 1 << 7;
683  for (i = 0; i < natts; ++i)
684  {
685  if (!slot->tts_isnull[i])
686  j |= k; /* set bit if not null */
687  k >>= 1;
688  if (k == 0) /* end of byte? */
689  {
690  pq_sendint8(buf, j);
691  j = 0;
692  k = 1 << 7;
693  }
694  }
695  if (k != (1 << 7)) /* flush last partial byte */
696  pq_sendint8(buf, j);
697 
698  /*
699  * send the attributes of this tuple
700  */
701  for (i = 0; i < natts; ++i)
702  {
703  PrinttupAttrInfo *thisState = myState->myinfo + i;
704  Datum attr = slot->tts_values[i];
705  bytea *outputbytes;
706 
707  if (slot->tts_isnull[i])
708  continue;
709 
710  Assert(thisState->format == 1);
711 
712  outputbytes = SendFunctionCall(&thisState->finfo, attr);
713  pq_sendint32(buf, VARSIZE(outputbytes) - VARHDRSZ);
714  pq_sendbytes(buf, VARDATA(outputbytes),
715  VARSIZE(outputbytes) - VARHDRSZ);
716  }
717 
718  pq_endmessage_reuse(buf);
719 
720  /* Return to caller's context, and flush row's temporary memory */
721  MemoryContextSwitchTo(oldcontext);
722  MemoryContextReset(myState->tmpcontext);
723 
724  return true;
725 }
#define VARDATA(PTR)
Definition: postgres.h:302
#define VARSIZE(PTR)
Definition: postgres.h:303
#define VARHDRSZ
Definition: c.h:555
static void printtup_prepare_info(DR_printtup *myState, TupleDesc typeinfo, int numAttrs)
Definition: printtup.c:327
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Datum * tts_values
Definition: tuptable.h:126
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:136
static void pq_sendint32(StringInfo buf, uint32 i)
Definition: pqformat.h:145
TupleDesc attrinfo
Definition: printtup.c:66
PrinttupAttrInfo * myinfo
Definition: printtup.c:68
static void slot_getallattrs(TupleTableSlot *slot)
Definition: tuptable.h:355
static char * buf
Definition: pg_test_fsync.c:68
bool * tts_isnull
Definition: tuptable.h:128
bytea * SendFunctionCall(FmgrInfo *flinfo, Datum val)
Definition: fmgr.c:1636
int nattrs
Definition: printtup.c:67
MemoryContext tmpcontext
Definition: printtup.c:70
TupleDesc tts_tupleDescriptor
Definition: tuptable.h:124
uintptr_t Datum
Definition: postgres.h:367
#define Assert(condition)
Definition: c.h:732
void pq_sendbytes(StringInfo buf, const char *data, int datalen)
Definition: pqformat.c:125
int i
StringInfoData buf
Definition: printtup.c:69
Definition: c.h:549
static void pq_sendint8(StringInfo buf, uint8 i)
Definition: pqformat.h:129
void pq_endmessage_reuse(StringInfo buf)
Definition: pqformat.c:316
FmgrInfo finfo
Definition: printtup.c:58
void pq_beginmessage_reuse(StringInfo buf, char msgtype)
Definition: pqformat.c:108

◆ printtup_prepare_info()

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

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

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

328 {
329  int16 *formats = myState->portal->formats;
330  int i;
331 
332  /* get rid of any old data */
333  if (myState->myinfo)
334  pfree(myState->myinfo);
335  myState->myinfo = NULL;
336 
337  myState->attrinfo = typeinfo;
338  myState->nattrs = numAttrs;
339  if (numAttrs <= 0)
340  return;
341 
342  myState->myinfo = (PrinttupAttrInfo *)
343  palloc0(numAttrs * sizeof(PrinttupAttrInfo));
344 
345  for (i = 0; i < numAttrs; i++)
346  {
347  PrinttupAttrInfo *thisState = myState->myinfo + i;
348  int16 format = (formats ? formats[i] : 0);
349  Form_pg_attribute attr = TupleDescAttr(typeinfo, i);
350 
351  thisState->format = format;
352  if (format == 0)
353  {
354  getTypeOutputInfo(attr->atttypid,
355  &thisState->typoutput,
356  &thisState->typisvarlena);
357  fmgr_info(thisState->typoutput, &thisState->finfo);
358  }
359  else if (format == 1)
360  {
361  getTypeBinaryOutputInfo(attr->atttypid,
362  &thisState->typsend,
363  &thisState->typisvarlena);
364  fmgr_info(thisState->typsend, &thisState->finfo);
365  }
366  else
367  ereport(ERROR,
368  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
369  errmsg("unsupported format code: %d", format)));
370  }
371 }
signed short int16
Definition: c.h:345
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2674
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
int errcode(int sqlerrcode)
Definition: elog.c:570
TupleDesc attrinfo
Definition: printtup.c:66
void pfree(void *pointer)
Definition: mcxt.c:1056
#define ERROR
Definition: elog.h:43
Portal portal
Definition: printtup.c:64
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:124
PrinttupAttrInfo * myinfo
Definition: printtup.c:68
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
int nattrs
Definition: printtup.c:67
#define ereport(elevel, rest)
Definition: elog.h:141
void getTypeBinaryOutputInfo(Oid type, Oid *typSend, bool *typIsVarlena)
Definition: lsyscache.c:2740
void * palloc0(Size size)
Definition: mcxt.c:980
int16 * formats
Definition: portal.h:159
bool typisvarlena
Definition: printtup.c:56
int errmsg(const char *fmt,...)
Definition: elog.c:784
int i
static char format
Definition: pg_basebackup.c:85
FmgrInfo finfo
Definition: printtup.c:58

◆ printtup_shutdown()

static void printtup_shutdown ( DestReceiver self)
static

Definition at line 541 of file printtup.c.

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

Referenced by printtup_create_DR().

542 {
543  DR_printtup *myState = (DR_printtup *) self;
544 
545  if (myState->myinfo)
546  pfree(myState->myinfo);
547  myState->myinfo = NULL;
548 
549  myState->attrinfo = NULL;
550 
551  if (myState->buf.data)
552  pfree(myState->buf.data);
553  myState->buf.data = NULL;
554 
555  if (myState->tmpcontext)
557  myState->tmpcontext = NULL;
558 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:211
TupleDesc attrinfo
Definition: printtup.c:66
void pfree(void *pointer)
Definition: mcxt.c:1056
PrinttupAttrInfo * myinfo
Definition: printtup.c:68
MemoryContext tmpcontext
Definition: printtup.c:70
StringInfoData buf
Definition: printtup.c:69

◆ printtup_startup()

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

Definition at line 131 of file printtup.c.

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

Referenced by printtup_create_DR().

132 {
133  DR_printtup *myState = (DR_printtup *) self;
134  Portal portal = myState->portal;
135 
136  /*
137  * Create I/O buffer to be used for all messages. This cannot be inside
138  * tmpcontext, since we want to re-use it across rows.
139  */
140  initStringInfo(&myState->buf);
141 
142  /*
143  * Create a temporary memory context that we can reset once per row to
144  * recover palloc'd memory. This avoids any problems with leaks inside
145  * datatype output routines, and should be faster than retail pfree's
146  * anyway.
147  */
149  "printtup",
151 
153  {
154  /*
155  * Send portal name to frontend (obsolete cruft, gone in proto 3.0)
156  *
157  * If portal name not specified, use "blank" portal.
158  */
159  const char *portalName = portal->name;
160 
161  if (portalName == NULL || portalName[0] == '\0')
162  portalName = "blank";
163 
164  pq_puttextmessage('P', portalName);
165  }
166 
167  /*
168  * If we are supposed to emit row descriptions, then send the tuple
169  * descriptor of the tuples.
170  */
171  if (myState->sendDescrip)
172  SendRowDescriptionMessage(&myState->buf,
173  typeinfo,
174  FetchPortalTargetList(portal),
175  portal->formats);
176 
177  /* ----------------
178  * We could set up the derived attr info at this time, but we postpone it
179  * until the first call of printtup, for 2 reasons:
180  * 1. We don't waste time (compared to the old way) if there are no
181  * tuples at all to output.
182  * 2. Checking in printtup allows us to handle the case that the tuples
183  * change type midway through (although this probably can't happen in
184  * the current executor).
185  * ----------------
186  */
187 }
#define AllocSetContextCreate
Definition: memutils.h:170
#define PG_PROTOCOL_MAJOR(v)
Definition: pqcomm.h:104
const char * name
Definition: portal.h:117
Portal portal
Definition: printtup.c:64
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
MemoryContext tmpcontext
Definition: printtup.c:70
bool sendDescrip
Definition: printtup.c:65
int16 * formats
Definition: portal.h:159
StringInfoData buf
Definition: printtup.c:69
List * FetchPortalTargetList(Portal portal)
Definition: pquery.c:336
void SendRowDescriptionMessage(StringInfo buf, TupleDesc typeinfo, List *targetlist, int16 *formats)
Definition: printtup.c:201
void pq_puttextmessage(char msgtype, const char *str)
Definition: pqformat.c:369
ProtocolVersion FrontendProtocol
Definition: globals.c:28

◆ SendRowDescriptionCols_2()

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

Definition at line 300 of file printtup.c.

References atttypid, getBaseTypeAndTypmod(), i, NameStr, TupleDescData::natts, pq_sendint16(), pq_sendint32(), pq_sendstring(), and TupleDescAttr.

Referenced by SendRowDescriptionMessage().

301 {
302  int natts = typeinfo->natts;
303  int i;
304 
305  for (i = 0; i < natts; ++i)
306  {
307  Form_pg_attribute att = TupleDescAttr(typeinfo, i);
308  Oid atttypid = att->atttypid;
309  int32 atttypmod = att->atttypmod;
310 
311  /* If column is a domain, send the base type and typmod instead */
312  atttypid = getBaseTypeAndTypmod(atttypid, &atttypmod);
313 
314  pq_sendstring(buf, NameStr(att->attname));
315  /* column ID only info appears in protocol 3.0 and up */
316  pq_sendint32(buf, atttypid);
317  pq_sendint16(buf, att->attlen);
318  pq_sendint32(buf, atttypmod);
319  /* format info only appears in protocol 3.0 and up */
320  }
321 }
static void pq_sendint16(StringInfo buf, uint16 i)
Definition: pqformat.h:137
Oid getBaseTypeAndTypmod(Oid typid, int32 *typmod)
Definition: lsyscache.c:2316
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
void pq_sendstring(StringInfo buf, const char *str)
Definition: pqformat.c:197
unsigned int Oid
Definition: postgres_ext.h:31
signed int int32
Definition: c.h:346
static void pq_sendint32(StringInfo buf, uint32 i)
Definition: pqformat.h:145
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
Oid atttypid
Definition: pg_attribute.h:49
int i
#define NameStr(name)
Definition: c.h:609

◆ SendRowDescriptionCols_3()

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

Definition at line 224 of file printtup.c.

References atttypid, enlargeStringInfo(), format, getBaseTypeAndTypmod(), i, lfirst, list_head(), lnext(), MAX_CONVERSION_GROWTH, NAMEDATALEN, NameStr, TupleDescData::natts, pq_writeint16(), pq_writeint32(), pq_writestring(), TargetEntry::resorigcol, TargetEntry::resorigtbl, and TupleDescAttr.

Referenced by SendRowDescriptionMessage().

225 {
226  int natts = typeinfo->natts;
227  int i;
228  ListCell *tlist_item = list_head(targetlist);
229 
230  /*
231  * Preallocate memory for the entire message to be sent. That allows to
232  * use the significantly faster inline pqformat.h functions and to avoid
233  * reallocations.
234  *
235  * Have to overestimate the size of the column-names, to account for
236  * character set overhead.
237  */
239  + sizeof(Oid) /* resorigtbl */
240  + sizeof(AttrNumber) /* resorigcol */
241  + sizeof(Oid) /* atttypid */
242  + sizeof(int16) /* attlen */
243  + sizeof(int32) /* attypmod */
244  + sizeof(int16) /* format */
245  ) * natts);
246 
247  for (i = 0; i < natts; ++i)
248  {
249  Form_pg_attribute att = TupleDescAttr(typeinfo, i);
250  Oid atttypid = att->atttypid;
251  int32 atttypmod = att->atttypmod;
252  Oid resorigtbl;
253  AttrNumber resorigcol;
254  int16 format;
255 
256  /*
257  * If column is a domain, send the base type and typmod instead.
258  * Lookup before sending any ints, for efficiency.
259  */
260  atttypid = getBaseTypeAndTypmod(atttypid, &atttypmod);
261 
262  /* Do we have a non-resjunk tlist item? */
263  while (tlist_item &&
264  ((TargetEntry *) lfirst(tlist_item))->resjunk)
265  tlist_item = lnext(targetlist, tlist_item);
266  if (tlist_item)
267  {
268  TargetEntry *tle = (TargetEntry *) lfirst(tlist_item);
269 
270  resorigtbl = tle->resorigtbl;
271  resorigcol = tle->resorigcol;
272  tlist_item = lnext(targetlist, tlist_item);
273  }
274  else
275  {
276  /* No info available, so send zeroes */
277  resorigtbl = 0;
278  resorigcol = 0;
279  }
280 
281  if (formats)
282  format = formats[i];
283  else
284  format = 0;
285 
286  pq_writestring(buf, NameStr(att->attname));
287  pq_writeint32(buf, resorigtbl);
288  pq_writeint16(buf, resorigcol);
289  pq_writeint32(buf, atttypid);
290  pq_writeint16(buf, att->attlen);
291  pq_writeint32(buf, atttypmod);
292  pq_writeint16(buf, format);
293  }
294 }
signed short int16
Definition: c.h:345
Oid getBaseTypeAndTypmod(Oid typid, int32 *typmod)
Definition: lsyscache.c:2316
static void pq_writestring(StringInfoData *pg_restrict buf, const char *pg_restrict str)
Definition: pqformat.h:109
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:321
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
Oid resorigtbl
Definition: primnodes.h:1398
unsigned int Oid
Definition: postgres_ext.h:31
signed int int32
Definition: c.h:346
#define NAMEDATALEN
static void pq_writeint16(StringInfoData *pg_restrict buf, uint16 i)
Definition: pqformat.h:61
static void pq_writeint32(StringInfoData *pg_restrict buf, uint32 i)
Definition: pqformat.h:75
void enlargeStringInfo(StringInfo str, int needed)
Definition: stringinfo.c:270
static ListCell * list_head(const List *l)
Definition: pg_list.h:125
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
Oid atttypid
Definition: pg_attribute.h:49
#define lfirst(lc)
Definition: pg_list.h:190
#define MAX_CONVERSION_GROWTH
Definition: pg_wchar.h:316
AttrNumber resorigcol
Definition: primnodes.h:1399
int i
#define NameStr(name)
Definition: c.h:609
static char format
Definition: pg_basebackup.c:85
int16 AttrNumber
Definition: attnum.h:21

◆ SendRowDescriptionMessage()

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

Definition at line 201 of file printtup.c.

References FrontendProtocol, TupleDescData::natts, PG_PROTOCOL_MAJOR, pq_beginmessage_reuse(), pq_endmessage_reuse(), pq_sendint16(), SendRowDescriptionCols_2(), and SendRowDescriptionCols_3().

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

203 {
204  int natts = typeinfo->natts;
205  int proto = PG_PROTOCOL_MAJOR(FrontendProtocol);
206 
207  /* tuple descriptor message type */
208  pq_beginmessage_reuse(buf, 'T');
209  /* # of attrs in tuples */
210  pq_sendint16(buf, natts);
211 
212  if (proto >= 3)
213  SendRowDescriptionCols_3(buf, typeinfo, targetlist, formats);
214  else
215  SendRowDescriptionCols_2(buf, typeinfo, targetlist, formats);
216 
217  pq_endmessage_reuse(buf);
218 }
static void pq_sendint16(StringInfo buf, uint16 i)
Definition: pqformat.h:137
#define PG_PROTOCOL_MAJOR(v)
Definition: pqcomm.h:104
static void SendRowDescriptionCols_3(StringInfo buf, TupleDesc typeinfo, List *targetlist, int16 *formats)
Definition: printtup.c:224
static void SendRowDescriptionCols_2(StringInfo buf, TupleDesc typeinfo, List *targetlist, int16 *formats)
Definition: printtup.c:300
void pq_endmessage_reuse(StringInfo buf)
Definition: pqformat.c:316
ProtocolVersion FrontendProtocol
Definition: globals.c:28
void pq_beginmessage_reuse(StringInfo buf, char msgtype)
Definition: pqformat.c:108

◆ SetRemoteDestReceiverParams()

void SetRemoteDestReceiverParams ( DestReceiver self,
Portal  portal 
)

Definition at line 107 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().

108 {
109  DR_printtup *myState = (DR_printtup *) self;
110 
111  Assert(myState->pub.mydest == DestRemote ||
112  myState->pub.mydest == DestRemoteExecute);
113 
114  myState->portal = portal;
115 
117  {
118  /*
119  * In protocol 2.0 the Bind message does not exist, so there is no way
120  * for the columns to have different print formats; it's sufficient to
121  * look at the first one.
122  */
123  if (portal->formats && portal->formats[0] != 0)
125  else
126  myState->pub.receiveSlot = printtup_20;
127  }
128 }
bool(* receiveSlot)(TupleTableSlot *slot, DestReceiver *self)
Definition: dest.h:118
static bool printtup_internal_20(TupleTableSlot *slot, DestReceiver *self)
Definition: printtup.c:652
DestReceiver pub
Definition: printtup.c:63
CommandDest mydest
Definition: dest.h:128
#define PG_PROTOCOL_MAJOR(v)
Definition: pqcomm.h:104
Portal portal
Definition: printtup.c:64
int16 * formats
Definition: portal.h:159
#define Assert(condition)
Definition: c.h:732
static bool printtup_20(TupleTableSlot *slot, DestReceiver *self)
Definition: printtup.c:463
ProtocolVersion FrontendProtocol
Definition: globals.c:28