PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
printtup.h File Reference
#include "utils/portal.h"
Include dependency graph for printtup.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

DestReceiverprinttup_create_DR (CommandDest dest)
 
void SetRemoteDestReceiverParams (DestReceiver *self, Portal portal)
 
void SendRowDescriptionMessage (TupleDesc typeinfo, List *targetlist, int16 *formats)
 
void debugStartup (DestReceiver *self, int operation, TupleDesc typeinfo)
 
bool debugtup (TupleTableSlot *slot, DestReceiver *self)
 
void spi_dest_startup (DestReceiver *self, int operation, TupleDesc typeinfo)
 
bool spi_printtup (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:187
static void printatt(unsigned attributeId, Form_pg_attribute attributeP, char *value)
Definition: printtup.c:492
#define NULL
Definition: c.h:229
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 }
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2632
Form_pg_attribute * attrs
Definition: tupdesc.h:74
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:492
uintptr_t Datum
Definition: postgres.h:372
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:1747
int i
Datum slot_getattr(TupleTableSlot *slot, int attnum, bool *isnull)
Definition: heaptuple.c:1141
DestReceiver* printtup_create_DR ( CommandDest  dest)

Definition at line 73 of file printtup.c.

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

Referenced by CreateDestReceiver().

74 {
75  DR_printtup *self = (DR_printtup *) palloc0(sizeof(DR_printtup));
76 
77  self->pub.receiveSlot = printtup; /* might get changed later */
78  self->pub.rStartup = printtup_startup;
79  self->pub.rShutdown = printtup_shutdown;
80  self->pub.rDestroy = printtup_destroy;
81  self->pub.mydest = dest;
82 
83  /*
84  * Send T message automatically if DestRemote, but not if
85  * DestRemoteExecute
86  */
87  self->sendDescrip = (dest == DestRemote);
88 
89  self->attrinfo = NULL;
90  self->nattrs = 0;
91  self->myinfo = NULL;
92  self->tmpcontext = NULL;
93 
94  return (DestReceiver *) self;
95 }
void * palloc0(Size size)
Definition: mcxt.c:878
static void printtup_destroy(DestReceiver *self)
Definition: printtup.c:482
#define NULL
Definition: c.h:229
static void printtup_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
Definition: printtup.c:125
static bool printtup(TupleTableSlot *slot, DestReceiver *self)
Definition: printtup.c:299
static void printtup_shutdown(DestReceiver *self)
Definition: printtup.c:462
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:2288
Form_pg_attribute * attrs
Definition: tupdesc.h:74
Oid resorigtbl
Definition: primnodes.h:1373
void pq_sendstring(StringInfo buf, const char *str)
Definition: pqformat.c:186
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_PROTOCOL_MAJOR(v)
Definition: pqcomm.h:104
int natts
Definition: tupdesc.h:73
void pq_beginmessage(StringInfo buf, char msgtype)
Definition: pqformat.c:88
signed int int32
Definition: c.h:256
static char * buf
Definition: pg_test_fsync.c:66
static ListCell * list_head(const List *l)
Definition: pg_list.h:77
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
#define lnext(lc)
Definition: pg_list.h:105
#define lfirst(lc)
Definition: pg_list.h:106
AttrNumber resorigcol
Definition: primnodes.h:1374
void pq_sendint(StringInfo buf, int i, int b)
Definition: pqformat.c:236
void pq_endmessage(StringInfo buf)
Definition: pqformat.c:344
int i
#define NameStr(name)
Definition: c.h:499
ProtocolVersion FrontendProtocol
Definition: globals.c:27
void SetRemoteDestReceiverParams ( DestReceiver self,
Portal  portal 
)

Definition at line 101 of file printtup.c.

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

Referenced by exec_execute_message(), and exec_simple_query().

102 {
103  DR_printtup *myState = (DR_printtup *) self;
104 
105  Assert(myState->pub.mydest == DestRemote ||
106  myState->pub.mydest == DestRemoteExecute);
107 
108  myState->portal = portal;
109 
111  {
112  /*
113  * In protocol 2.0 the Bind message does not exist, so there is no way
114  * for the columns to have different print formats; it's sufficient to
115  * look at the first one.
116  */
117  if (portal->formats && portal->formats[0] != 0)
119  else
120  myState->pub.receiveSlot = printtup_20;
121  }
122 }
static bool printtup_internal_20(TupleTableSlot *slot, DestReceiver *self)
Definition: printtup.c:570
DestReceiver pub
Definition: printtup.c:59
CommandDest mydest
Definition: dest.h:128
#define PG_PROTOCOL_MAJOR(v)
Definition: pqcomm.h:104
Portal portal
Definition: printtup.c:60
int16 * formats
Definition: portal.h:157
#define Assert(condition)
Definition: c.h:675
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
void spi_dest_startup ( DestReceiver self,
int  operation,
TupleDesc  typeinfo 
)

Definition at line 1649 of file spi.c.

References _SPI_procmem(), SPITupleTable::alloced, ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate(), CreateTupleDescCopy(), CurrentMemoryContext, elog, ERROR, SPITupleTable::free, GetCurrentSubTransactionId(), MemoryContextSwitchTo(), SPITupleTable::next, NULL, palloc(), palloc0(), slist_push_head(), SPITupleTable::subid, SPITupleTable::tupdesc, SPITupleTable::tuptabcxt, _SPI_connection::tuptable, _SPI_connection::tuptables, and SPITupleTable::vals.

1650 {
1651  SPITupleTable *tuptable;
1652  MemoryContext oldcxt;
1653  MemoryContext tuptabcxt;
1654 
1655  if (_SPI_current == NULL)
1656  elog(ERROR, "spi_dest_startup called while not connected to SPI");
1657 
1658  if (_SPI_current->tuptable != NULL)
1659  elog(ERROR, "improper call to spi_dest_startup");
1660 
1661  /* We create the tuple table context as a child of procCxt */
1662 
1663  oldcxt = _SPI_procmem(); /* switch to procedure memory context */
1664 
1666  "SPI TupTable",
1668  MemoryContextSwitchTo(tuptabcxt);
1669 
1670  _SPI_current->tuptable = tuptable = (SPITupleTable *)
1671  palloc0(sizeof(SPITupleTable));
1672  tuptable->tuptabcxt = tuptabcxt;
1673  tuptable->subid = GetCurrentSubTransactionId();
1674 
1675  /*
1676  * The tuptable is now valid enough to be freed by AtEOSubXact_SPI, so put
1677  * it onto the SPI context's tuptables list. This will ensure it's not
1678  * leaked even in the unlikely event the following few lines fail.
1679  */
1680  slist_push_head(&_SPI_current->tuptables, &tuptable->next);
1681 
1682  /* set up initial allocations */
1683  tuptable->alloced = tuptable->free = 128;
1684  tuptable->vals = (HeapTuple *) palloc(tuptable->alloced * sizeof(HeapTuple));
1685  tuptable->tupdesc = CreateTupleDescCopy(typeinfo);
1686 
1687  MemoryContextSwitchTo(oldcxt);
1688 }
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:141
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static void slist_push_head(slist_head *head, slist_node *node)
Definition: ilist.h:574
slist_node next
Definition: spi.h:29
static _SPI_connection * _SPI_current
Definition: spi.c:45
HeapTuple * vals
Definition: spi.h:28
#define ERROR
Definition: elog.h:43
SPITupleTable * tuptable
Definition: spi_priv.h:27
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:165
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:322
void * palloc0(Size size)
Definition: mcxt.c:878
TupleDesc tupdesc
Definition: spi.h:27
#define NULL
Definition: c.h:229
SubTransactionId GetCurrentSubTransactionId(void)
Definition: xact.c:649
uint64 free
Definition: spi.h:26
void * palloc(Size size)
Definition: mcxt.c:849
uint64 alloced
Definition: spi.h:25
#define elog
Definition: elog.h:219
static MemoryContext _SPI_procmem(void)
Definition: spi.c:2440
SubTransactionId subid
Definition: spi.h:30
slist_head tuptables
Definition: spi_priv.h:30
MemoryContext tuptabcxt
Definition: spi.h:24
bool spi_printtup ( TupleTableSlot slot,
DestReceiver self 
)

Definition at line 1696 of file spi.c.

References SPITupleTable::alloced, elog, ERROR, ExecCopySlotTuple(), SPITupleTable::free, MemoryContextSwitchTo(), NULL, repalloc_huge(), SPITupleTable::tuptabcxt, _SPI_connection::tuptable, and SPITupleTable::vals.

1697 {
1698  SPITupleTable *tuptable;
1699  MemoryContext oldcxt;
1700 
1701  if (_SPI_current == NULL)
1702  elog(ERROR, "spi_printtup called while not connected to SPI");
1703 
1704  tuptable = _SPI_current->tuptable;
1705  if (tuptable == NULL)
1706  elog(ERROR, "improper call to spi_printtup");
1707 
1708  oldcxt = MemoryContextSwitchTo(tuptable->tuptabcxt);
1709 
1710  if (tuptable->free == 0)
1711  {
1712  /* Double the size of the pointer array */
1713  tuptable->free = tuptable->alloced;
1714  tuptable->alloced += tuptable->free;
1715  tuptable->vals = (HeapTuple *) repalloc_huge(tuptable->vals,
1716  tuptable->alloced * sizeof(HeapTuple));
1717  }
1718 
1719  tuptable->vals[tuptable->alloced - tuptable->free] =
1720  ExecCopySlotTuple(slot);
1721  (tuptable->free)--;
1722 
1723  MemoryContextSwitchTo(oldcxt);
1724 
1725  return true;
1726 }
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static _SPI_connection * _SPI_current
Definition: spi.c:45
HeapTuple * vals
Definition: spi.h:28
#define ERROR
Definition: elog.h:43
SPITupleTable * tuptable
Definition: spi_priv.h:27
HeapTuple ExecCopySlotTuple(TupleTableSlot *slot)
Definition: execTuples.c:545
#define NULL
Definition: c.h:229
uint64 free
Definition: spi.h:26
void * repalloc_huge(void *pointer, Size size)
Definition: mcxt.c:1031
uint64 alloced
Definition: spi.h:25
#define elog
Definition: elog.h:219
MemoryContext tuptabcxt
Definition: spi.h:24