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)
 
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

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

Definition at line 588 of file printtup.c.

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

589 {
590  int natts = typeinfo->natts;
591  int i;
592 
593  /*
594  * show the return type of the tuples
595  */
596  for (i = 0; i < natts; ++i)
597  printatt((unsigned) i + 1, TupleDescAttr(typeinfo, i), NULL);
598  printf("\t----\n");
599 }
#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:567
int i
bool debugtup ( TupleTableSlot slot,
DestReceiver self 
)

Definition at line 606 of file printtup.c.

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

Referenced by print_slot().

607 {
608  TupleDesc typeinfo = slot->tts_tupleDescriptor;
609  int natts = typeinfo->natts;
610  int i;
611  Datum attr;
612  char *value;
613  bool isnull;
614  Oid typoutput;
615  bool typisvarlena;
616 
617  for (i = 0; i < natts; ++i)
618  {
619  attr = slot_getattr(slot, i + 1, &isnull);
620  if (isnull)
621  continue;
622  getTypeOutputInfo(TupleDescAttr(typeinfo, i)->atttypid,
623  &typoutput, &typisvarlena);
624 
625  value = OidOutputFunctionCall(typoutput, attr);
626 
627  printatt((unsigned) i + 1, TupleDescAttr(typeinfo, i), value);
628  }
629  printf("\t----\n");
630 
631  return true;
632 }
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:567
uintptr_t Datum
Definition: postgres.h:372
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:1742
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 567 of file printtup.c.

References NameStr.

Referenced by debugStartup(), and debugtup().

570 {
571  printf("\t%2d: %s%s%s%s\t(typeid = %u, len = %d, typmod = %d, byval = %c)\n",
572  attributeId,
573  NameStr(attributeP->attname),
574  value != NULL ? " = \"" : "",
575  value != NULL ? value : "",
576  value != NULL ? "\"" : "",
577  (unsigned int) (attributeP->atttypid),
578  attributeP->attlen,
579  attributeP->atttypmod,
580  attributeP->attbyval ? 't' : 'f');
581 }
static struct @121 value
#define NameStr(name)
Definition: c.h:493
static bool printtup ( TupleTableSlot slot,
DestReceiver self 
)
static

Definition at line 374 of file printtup.c.

References DR_printtup::attrinfo, DR_printtup::buf, buf, DatumGetPointer, PrinttupAttrInfo::finfo, PrinttupAttrInfo::format, i, MemoryContextReset(), MemoryContextSwitchTo(), DR_printtup::myinfo, DR_printtup::nattrs, tupleDesc::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().

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

Definition at line 459 of file printtup.c.

References Assert, DR_printtup::attrinfo, DR_printtup::buf, buf, PrinttupAttrInfo::finfo, PrinttupAttrInfo::format, i, MemoryContextReset(), MemoryContextSwitchTo(), DR_printtup::myinfo, DR_printtup::nattrs, tupleDesc::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().

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

Definition at line 78 of file printtup.c.

References 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->tmpcontext = NULL;
98 
99  return (DestReceiver *) self;
100 }
void * palloc0(Size size)
Definition: mcxt.c:877
static void printtup_destroy(DestReceiver *self)
Definition: printtup.c:557
static void printtup_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
Definition: printtup.c:130
static bool printtup(TupleTableSlot *slot, DestReceiver *self)
Definition: printtup.c:374
static void printtup_shutdown(DestReceiver *self)
Definition: printtup.c:537
static void printtup_destroy ( DestReceiver self)
static

Definition at line 557 of file printtup.c.

References pfree().

Referenced by printtup_create_DR().

558 {
559  pfree(self);
560 }
void pfree(void *pointer)
Definition: mcxt.c:949
static bool printtup_internal_20 ( TupleTableSlot slot,
DestReceiver self 
)
static

Definition at line 644 of file printtup.c.

References Assert, DR_printtup::attrinfo, DR_printtup::buf, buf, PrinttupAttrInfo::finfo, PrinttupAttrInfo::format, i, MemoryContextReset(), MemoryContextSwitchTo(), DR_printtup::myinfo, DR_printtup::nattrs, tupleDesc::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().

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

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

324 {
325  int16 *formats = myState->portal->formats;
326  int i;
327 
328  /* get rid of any old data */
329  if (myState->myinfo)
330  pfree(myState->myinfo);
331  myState->myinfo = NULL;
332 
333  myState->attrinfo = typeinfo;
334  myState->nattrs = numAttrs;
335  if (numAttrs <= 0)
336  return;
337 
338  myState->myinfo = (PrinttupAttrInfo *)
339  palloc0(numAttrs * sizeof(PrinttupAttrInfo));
340 
341  for (i = 0; i < numAttrs; i++)
342  {
343  PrinttupAttrInfo *thisState = myState->myinfo + i;
344  int16 format = (formats ? formats[i] : 0);
345  Form_pg_attribute attr = TupleDescAttr(typeinfo, i);
346 
347  thisState->format = format;
348  if (format == 0)
349  {
350  getTypeOutputInfo(attr->atttypid,
351  &thisState->typoutput,
352  &thisState->typisvarlena);
353  fmgr_info(thisState->typoutput, &thisState->finfo);
354  }
355  else if (format == 1)
356  {
357  getTypeBinaryOutputInfo(attr->atttypid,
358  &thisState->typsend,
359  &thisState->typisvarlena);
360  fmgr_info(thisState->typsend, &thisState->finfo);
361  }
362  else
363  ereport(ERROR,
364  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
365  errmsg("unsupported format code: %d", format)));
366  }
367 }
signed short int16
Definition: c.h:245
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:67
void pfree(void *pointer)
Definition: mcxt.c:949
#define ERROR
Definition: elog.h:43
Portal portal
Definition: printtup.c:65
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:122
PrinttupAttrInfo * myinfo
Definition: printtup.c:69
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
int nattrs
Definition: printtup.c:68
#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:877
int16 * formats
Definition: portal.h:157
bool typisvarlena
Definition: printtup.c:56
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
static char format
Definition: pg_basebackup.c:81
FmgrInfo finfo
Definition: printtup.c:58
static void printtup_shutdown ( DestReceiver self)
static

Definition at line 537 of file printtup.c.

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

Referenced by printtup_create_DR().

538 {
539  DR_printtup *myState = (DR_printtup *) self;
540 
541  if (myState->myinfo)
542  pfree(myState->myinfo);
543  myState->myinfo = NULL;
544 
545  myState->attrinfo = NULL;
546 
547  if (myState->tmpcontext)
549  myState->tmpcontext = NULL;
550 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
TupleDesc attrinfo
Definition: printtup.c:67
void pfree(void *pointer)
Definition: mcxt.c:949
PrinttupAttrInfo * myinfo
Definition: printtup.c:69
MemoryContext tmpcontext
Definition: printtup.c:70
static void printtup_startup ( DestReceiver self,
int  operation,
TupleDesc  typeinfo 
)
static

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

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

Definition at line 296 of file printtup.c.

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

Referenced by SendRowDescriptionMessage().

297 {
298  int natts = typeinfo->natts;
299  int i;
300 
301  for (i = 0; i < natts; ++i)
302  {
303  Form_pg_attribute att = TupleDescAttr(typeinfo, i);
304  Oid atttypid = att->atttypid;
305  int32 atttypmod = att->atttypmod;
306 
307  /* If column is a domain, send the base type and typmod instead */
308  atttypid = getBaseTypeAndTypmod(atttypid, &atttypmod);
309 
310  pq_sendstring(buf, NameStr(att->attname));
311  /* column ID only info appears in protocol 3.0 and up */
312  pq_sendint32(buf, atttypid);
313  pq_sendint16(buf, att->attlen);
314  pq_sendint32(buf, atttypmod);
315  /* format info only appears in protocol 3.0 and up */
316  }
317 }
Oid getBaseTypeAndTypmod(Oid typid, int32 *typmod)
Definition: lsyscache.c:2288
static void pq_sendint32(StringInfo buf, int32 i)
Definition: pqformat.h:148
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:84
void pq_sendstring(StringInfo buf, const char *str)
Definition: pqformat.c:197
unsigned int Oid
Definition: postgres_ext.h:31
int natts
Definition: tupdesc.h:73
signed int int32
Definition: c.h:246
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
static void pq_sendint16(StringInfo buf, int16 i)
Definition: pqformat.h:140
int i
#define NameStr(name)
Definition: c.h:493
static void SendRowDescriptionCols_3 ( StringInfo  buf,
TupleDesc  typeinfo,
List targetlist,
int16 formats 
)
static

Definition at line 220 of file printtup.c.

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

Referenced by SendRowDescriptionMessage().

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

Definition at line 197 of file printtup.c.

References FrontendProtocol, tupleDesc::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().

199 {
200  int natts = typeinfo->natts;
201  int proto = PG_PROTOCOL_MAJOR(FrontendProtocol);
202 
203  /* tuple descriptor message type */
204  pq_beginmessage_reuse(buf, 'T');
205  /* # of attrs in tuples */
206  pq_sendint16(buf, natts);
207 
208  if (proto >= 3)
209  SendRowDescriptionCols_3(buf, typeinfo, targetlist, formats);
210  else
211  SendRowDescriptionCols_2(buf, typeinfo, targetlist, formats);
212 
213  pq_endmessage_reuse(buf);
214 }
#define PG_PROTOCOL_MAJOR(v)
Definition: pqcomm.h:104
int natts
Definition: tupdesc.h:73
static void SendRowDescriptionCols_3(StringInfo buf, TupleDesc typeinfo, List *targetlist, int16 *formats)
Definition: printtup.c:220
static void pq_sendint16(StringInfo buf, int16 i)
Definition: pqformat.h:140
static void SendRowDescriptionCols_2(StringInfo buf, TupleDesc typeinfo, List *targetlist, int16 *formats)
Definition: printtup.c:296
void pq_endmessage_reuse(StringInfo buf)
Definition: pqformat.c:316
ProtocolVersion FrontendProtocol
Definition: globals.c:27
void pq_beginmessage_reuse(StringInfo buf, char msgtype)
Definition: pqformat.c:108
void SetRemoteDestReceiverParams ( DestReceiver self,
Portal  portal 
)

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

107 {
108  DR_printtup *myState = (DR_printtup *) self;
109 
110  Assert(myState->pub.mydest == DestRemote ||
111  myState->pub.mydest == DestRemoteExecute);
112 
113  myState->portal = portal;
114 
116  {
117  /*
118  * In protocol 2.0 the Bind message does not exist, so there is no way
119  * for the columns to have different print formats; it's sufficient to
120  * look at the first one.
121  */
122  if (portal->formats && portal->formats[0] != 0)
124  else
125  myState->pub.receiveSlot = printtup_20;
126  }
127 }
static bool printtup_internal_20(TupleTableSlot *slot, DestReceiver *self)
Definition: printtup.c:644
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:65
int16 * formats
Definition: portal.h:157
#define Assert(condition)
Definition: c.h:681
static bool printtup_20(TupleTableSlot *slot, DestReceiver *self)
Definition: printtup.c:459
bool(* receiveSlot)(TupleTableSlot *slot, DestReceiver *self)
Definition: dest.h:118
ProtocolVersion FrontendProtocol
Definition: globals.c:27