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

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

514 {
515  int natts = typeinfo->natts;
516  Form_pg_attribute *attinfo = typeinfo->attrs;
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, attinfo[i], NULL);
524  printf("\t----\n");
525 }
Form_pg_attribute * attrs
Definition: tupdesc.h:74
int natts
Definition: tupdesc.h:73
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:184
static void printatt(unsigned attributeId, Form_pg_attribute attributeP, char *value)
Definition: printtup.c:492
#define NULL
Definition: c.h:226
int i
bool debugtup ( TupleTableSlot slot,
DestReceiver self 
)

Definition at line 532 of file printtup.c.

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

Definition at line 492 of file printtup.c.

References NameStr, and NULL.

Referenced by debugStartup(), and debugtup().

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

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

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

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

385 {
386  TupleDesc typeinfo = slot->tts_tupleDescriptor;
387  DR_printtup *myState = (DR_printtup *) self;
388  MemoryContext oldcontext;
390  int natts = typeinfo->natts;
391  int i,
392  j,
393  k;
394 
395  /* Set or update my derived attribute info, if needed */
396  if (myState->attrinfo != typeinfo || myState->nattrs != natts)
397  printtup_prepare_info(myState, typeinfo, natts);
398 
399  /* Make sure the tuple is fully deconstructed */
400  slot_getallattrs(slot);
401 
402  /* Switch into per-row context so we can recover memory below */
403  oldcontext = MemoryContextSwitchTo(myState->tmpcontext);
404 
405  /*
406  * tell the frontend to expect new tuple data (in ASCII style)
407  */
408  pq_beginmessage(&buf, 'D');
409 
410  /*
411  * send a bitmap of which attributes are not null
412  */
413  j = 0;
414  k = 1 << 7;
415  for (i = 0; i < natts; ++i)
416  {
417  if (!slot->tts_isnull[i])
418  j |= k; /* set bit if not null */
419  k >>= 1;
420  if (k == 0) /* end of byte? */
421  {
422  pq_sendint(&buf, j, 1);
423  j = 0;
424  k = 1 << 7;
425  }
426  }
427  if (k != (1 << 7)) /* flush last partial byte */
428  pq_sendint(&buf, j, 1);
429 
430  /*
431  * send the attributes of this tuple
432  */
433  for (i = 0; i < natts; ++i)
434  {
435  PrinttupAttrInfo *thisState = myState->myinfo + i;
436  Datum attr = slot->tts_values[i];
437  char *outputstr;
438 
439  if (slot->tts_isnull[i])
440  continue;
441 
442  Assert(thisState->format == 0);
443 
444  outputstr = OutputFunctionCall(&thisState->finfo, attr);
445  pq_sendcountedtext(&buf, outputstr, strlen(outputstr), true);
446  }
447 
448  pq_endmessage(&buf);
449 
450  /* Return to caller's context, and flush row's temporary memory */
451  MemoryContextSwitchTo(oldcontext);
452  MemoryContextReset(myState->tmpcontext);
453 
454  return true;
455 }
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:1926
TupleDesc attrinfo
Definition: printtup.c:62
PrinttupAttrInfo * myinfo
Definition: printtup.c:64
static char * buf
Definition: pg_test_fsync.c:65
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:1239
MemoryContext tmpcontext
Definition: printtup.c:65
TupleDesc tts_tupleDescriptor
Definition: tuptable.h:121
uintptr_t Datum
Definition: postgres.h:374
#define Assert(condition)
Definition: c.h:670
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:920
static void printtup_destroy(DestReceiver *self)
Definition: printtup.c:482
#define NULL
Definition: c.h:226
static void printtup_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
Definition: printtup.c:125
static bool printtup(TupleTableSlot *slot, DestReceiver *self)
Definition: printtup.c:299
static void printtup_shutdown(DestReceiver *self)
Definition: printtup.c:462
static void printtup_destroy ( DestReceiver self)
static

Definition at line 482 of file printtup.c.

References pfree().

Referenced by printtup_create_DR().

483 {
484  pfree(self);
485 }
void pfree(void *pointer)
Definition: mcxt.c:992
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:305
#define VARSIZE(PTR)
Definition: postgres.h:306
#define VARHDRSZ
Definition: c.h:440
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:65
bool * tts_isnull
Definition: tuptable.h:126
bytea * SendFunctionCall(FmgrInfo *flinfo, Datum val)
Definition: fmgr.c:1987
int nattrs
Definition: printtup.c:63
void slot_getallattrs(TupleTableSlot *slot)
Definition: heaptuple.c:1239
MemoryContext tmpcontext
Definition: printtup.c:65
TupleDesc tts_tupleDescriptor
Definition: tuptable.h:121
uintptr_t Datum
Definition: postgres.h:374
#define Assert(condition)
Definition: c.h:670
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:434
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, tupleDesc::attrs, 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, 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 
272  thisState->format = format;
273  if (format == 0)
274  {
275  getTypeOutputInfo(typeinfo->attrs[i]->atttypid,
276  &thisState->typoutput,
277  &thisState->typisvarlena);
278  fmgr_info(thisState->typoutput, &thisState->finfo);
279  }
280  else if (format == 1)
281  {
282  getTypeBinaryOutputInfo(typeinfo->attrs[i]->atttypid,
283  &thisState->typsend,
284  &thisState->typisvarlena);
285  fmgr_info(thisState->typsend, &thisState->finfo);
286  }
287  else
288  ereport(ERROR,
289  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
290  errmsg("unsupported format code: %d", format)));
291  }
292 }
signed short int16
Definition: c.h:252
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2600
Form_pg_attribute * attrs
Definition: tupdesc.h:74
int errcode(int sqlerrcode)
Definition: elog.c:575
TupleDesc attrinfo
Definition: printtup.c:62
void pfree(void *pointer)
Definition: mcxt.c:992
#define ERROR
Definition: elog.h:43
Portal portal
Definition: printtup.c:60
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:159
PrinttupAttrInfo * myinfo
Definition: printtup.c:64
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:2666
void * palloc0(Size size)
Definition: mcxt.c:920
int16 * formats
Definition: portal.h:155
#define NULL
Definition: c.h:226
bool typisvarlena
Definition: printtup.c:52
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
static char format
Definition: pg_basebackup.c:83
FmgrInfo finfo
Definition: printtup.c:54
static void printtup_shutdown ( DestReceiver self)
static

Definition at line 462 of file printtup.c.

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

Referenced by printtup_create_DR().

463 {
464  DR_printtup *myState = (DR_printtup *) self;
465 
466  if (myState->myinfo)
467  pfree(myState->myinfo);
468  myState->myinfo = NULL;
469 
470  myState->attrinfo = NULL;
471 
472  if (myState->tmpcontext)
474  myState->tmpcontext = NULL;
475 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
TupleDesc attrinfo
Definition: printtup.c:62
void pfree(void *pointer)
Definition: mcxt.c:992
PrinttupAttrInfo * myinfo
Definition: printtup.c:64
MemoryContext tmpcontext
Definition: printtup.c:65
#define NULL
Definition: c.h:226
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:145
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:440
bool sendDescrip
Definition: printtup.c:61
int16 * formats
Definition: portal.h:155
#define NULL
Definition: c.h:226
List * FetchPortalTargetList(Portal portal)
Definition: pquery.c:330
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 tupleDesc::attrs, buf, FrontendProtocol, getBaseTypeAndTypmod(), i, lfirst, list_head(), lnext, NameStr, tupleDesc::natts, PG_PROTOCOL_MAJOR, pq_beginmessage(), pq_endmessage(), pq_sendint(), pq_sendstring(), TargetEntry::resorigcol, and TargetEntry::resorigtbl.

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

189 {
190  Form_pg_attribute *attrs = typeinfo->attrs;
191  int natts = typeinfo->natts;
192  int proto = PG_PROTOCOL_MAJOR(FrontendProtocol);
193  int i;
195  ListCell *tlist_item = list_head(targetlist);
196 
197  pq_beginmessage(&buf, 'T'); /* tuple descriptor message type */
198  pq_sendint(&buf, natts, 2); /* # of attrs in tuples */
199 
200  for (i = 0; i < natts; ++i)
201  {
202  Oid atttypid = attrs[i]->atttypid;
203  int32 atttypmod = attrs[i]->atttypmod;
204 
205  pq_sendstring(&buf, NameStr(attrs[i]->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, attrs[i]->attlen, sizeof(attrs[i]->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:2256
Form_pg_attribute * attrs
Definition: tupdesc.h:74
Oid resorigtbl
Definition: primnodes.h:1335
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:253
static char * buf
Definition: pg_test_fsync.c:65
static ListCell * list_head(const List *l)
Definition: pg_list.h:77
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:184
#define lnext(lc)
Definition: pg_list.h:105
#define lfirst(lc)
Definition: pg_list.h:106
AttrNumber resorigcol
Definition: primnodes.h:1336
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:494
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:155
#define Assert(condition)
Definition: c.h:670
static bool printtup_20(TupleTableSlot *slot, DestReceiver *self)
Definition: printtup.c:384
bool(* receiveSlot)(TupleTableSlot *slot, DestReceiver *self)
Definition: dest.h:118
ProtocolVersion FrontendProtocol
Definition: globals.c:27