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: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
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
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
void spi_dest_startup ( DestReceiver self,
int  operation,
TupleDesc  typeinfo 
)

Definition at line 1636 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.

1637 {
1638  SPITupleTable *tuptable;
1639  MemoryContext oldcxt;
1640  MemoryContext tuptabcxt;
1641 
1642  if (_SPI_current == NULL)
1643  elog(ERROR, "spi_dest_startup called while not connected to SPI");
1644 
1645  if (_SPI_current->tuptable != NULL)
1646  elog(ERROR, "improper call to spi_dest_startup");
1647 
1648  /* We create the tuple table context as a child of procCxt */
1649 
1650  oldcxt = _SPI_procmem(); /* switch to procedure memory context */
1651 
1653  "SPI TupTable",
1655  MemoryContextSwitchTo(tuptabcxt);
1656 
1657  _SPI_current->tuptable = tuptable = (SPITupleTable *)
1658  palloc0(sizeof(SPITupleTable));
1659  tuptable->tuptabcxt = tuptabcxt;
1660  tuptable->subid = GetCurrentSubTransactionId();
1661 
1662  /*
1663  * The tuptable is now valid enough to be freed by AtEOSubXact_SPI, so put
1664  * it onto the SPI context's tuptables list. This will ensure it's not
1665  * leaked even in the unlikely event the following few lines fail.
1666  */
1667  slist_push_head(&_SPI_current->tuptables, &tuptable->next);
1668 
1669  /* set up initial allocations */
1670  tuptable->alloced = tuptable->free = 128;
1671  tuptable->vals = (HeapTuple *) palloc(tuptable->alloced * sizeof(HeapTuple));
1672  tuptable->tupdesc = CreateTupleDescCopy(typeinfo);
1673 
1674  MemoryContextSwitchTo(oldcxt);
1675 }
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:28
static _SPI_connection * _SPI_current
Definition: spi.c:45
HeapTuple * vals
Definition: spi.h:27
#define ERROR
Definition: elog.h:43
SPITupleTable * tuptable
Definition: spi_priv.h:26
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:145
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:440
void * palloc0(Size size)
Definition: mcxt.c:920
TupleDesc tupdesc
Definition: spi.h:26
#define NULL
Definition: c.h:226
SubTransactionId GetCurrentSubTransactionId(void)
Definition: xact.c:648
uint64 free
Definition: spi.h:25
void * palloc(Size size)
Definition: mcxt.c:891
uint64 alloced
Definition: spi.h:24
#define elog
Definition: elog.h:219
static MemoryContext _SPI_procmem(void)
Definition: spi.c:2421
SubTransactionId subid
Definition: spi.h:29
slist_head tuptables
Definition: spi_priv.h:29
MemoryContext tuptabcxt
Definition: spi.h:23
bool spi_printtup ( TupleTableSlot slot,
DestReceiver self 
)

Definition at line 1683 of file spi.c.

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

1684 {
1685  SPITupleTable *tuptable;
1686  MemoryContext oldcxt;
1687 
1688  if (_SPI_current == NULL)
1689  elog(ERROR, "spi_printtup called while not connected to SPI");
1690 
1691  tuptable = _SPI_current->tuptable;
1692  if (tuptable == NULL)
1693  elog(ERROR, "improper call to spi_printtup");
1694 
1695  oldcxt = MemoryContextSwitchTo(tuptable->tuptabcxt);
1696 
1697  if (tuptable->free == 0)
1698  {
1699  /* Double the size of the pointer array */
1700  tuptable->free = tuptable->alloced;
1701  tuptable->alloced += tuptable->free;
1702  tuptable->vals = (HeapTuple *) repalloc_huge(tuptable->vals,
1703  tuptable->alloced * sizeof(HeapTuple));
1704  }
1705 
1706  tuptable->vals[tuptable->alloced - tuptable->free] =
1707  ExecCopySlotTuple(slot);
1708  (tuptable->free)--;
1709 
1710  MemoryContextSwitchTo(oldcxt);
1711 
1712  return true;
1713 }
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static _SPI_connection * _SPI_current
Definition: spi.c:45
HeapTuple * vals
Definition: spi.h:27
#define ERROR
Definition: elog.h:43
SPITupleTable * tuptable
Definition: spi_priv.h:26
HeapTuple ExecCopySlotTuple(TupleTableSlot *slot)
Definition: execTuples.c:545
#define NULL
Definition: c.h:226
uint64 free
Definition: spi.h:25
void * repalloc_huge(void *pointer, Size size)
Definition: mcxt.c:1104
uint64 alloced
Definition: spi.h:24
#define elog
Definition: elog.h:219
MemoryContext tuptabcxt
Definition: spi.h:23