PostgreSQL Source Code  git master
tstoreReceiver.c File Reference
#include "postgres.h"
#include "access/tuptoaster.h"
#include "executor/tstoreReceiver.h"
Include dependency graph for tstoreReceiver.c:

Go to the source code of this file.

Data Structures

struct  TStoreState
 

Functions

static bool tstoreReceiveSlot_notoast (TupleTableSlot *slot, DestReceiver *self)
 
static bool tstoreReceiveSlot_detoast (TupleTableSlot *slot, DestReceiver *self)
 
static void tstoreStartupReceiver (DestReceiver *self, int operation, TupleDesc typeinfo)
 
static void tstoreShutdownReceiver (DestReceiver *self)
 
static void tstoreDestroyReceiver (DestReceiver *self)
 
DestReceiverCreateTuplestoreDestReceiver (void)
 
void SetTuplestoreDestReceiverParams (DestReceiver *self, Tuplestorestate *tStore, MemoryContext tContext, bool detoast)
 

Function Documentation

◆ CreateTuplestoreDestReceiver()

DestReceiver* CreateTuplestoreDestReceiver ( void  )

Definition at line 190 of file tstoreReceiver.c.

References DestTuplestore, palloc0(), tstoreDestroyReceiver(), tstoreReceiveSlot_notoast(), tstoreShutdownReceiver(), and tstoreStartupReceiver().

Referenced by CreateDestReceiver().

191 {
192  TStoreState *self = (TStoreState *) palloc0(sizeof(TStoreState));
193 
194  self->pub.receiveSlot = tstoreReceiveSlot_notoast; /* might change */
195  self->pub.rStartup = tstoreStartupReceiver;
196  self->pub.rShutdown = tstoreShutdownReceiver;
197  self->pub.rDestroy = tstoreDestroyReceiver;
198  self->pub.mydest = DestTuplestore;
199 
200  /* private fields will be set by SetTuplestoreDestReceiverParams */
201 
202  return (DestReceiver *) self;
203 }
static void tstoreShutdownReceiver(DestReceiver *self)
static bool tstoreReceiveSlot_notoast(TupleTableSlot *slot, DestReceiver *self)
static void tstoreStartupReceiver(DestReceiver *self, int operation, TupleDesc typeinfo)
void * palloc0(Size size)
Definition: mcxt.c:864
static void tstoreDestroyReceiver(DestReceiver *self)

◆ SetTuplestoreDestReceiverParams()

void SetTuplestoreDestReceiverParams ( DestReceiver self,
Tuplestorestate tStore,
MemoryContext  tContext,
bool  detoast 
)

Definition at line 209 of file tstoreReceiver.c.

References Assert, TStoreState::cxt, DestTuplestore, TStoreState::detoast, _DestReceiver::mydest, TStoreState::pub, and TStoreState::tstore.

Referenced by FillPortalStore(), and PersistHoldablePortal().

213 {
214  TStoreState *myState = (TStoreState *) self;
215 
216  Assert(myState->pub.mydest == DestTuplestore);
217  myState->tstore = tStore;
218  myState->cxt = tContext;
219  myState->detoast = detoast;
220 }
MemoryContext cxt
CommandDest mydest
Definition: dest.h:128
DestReceiver pub
Tuplestorestate * tstore
#define Assert(condition)
Definition: c.h:680

◆ tstoreDestroyReceiver()

static void tstoreDestroyReceiver ( DestReceiver self)
static

Definition at line 181 of file tstoreReceiver.c.

References pfree().

Referenced by CreateTuplestoreDestReceiver().

182 {
183  pfree(self);
184 }
void pfree(void *pointer)
Definition: mcxt.c:936

◆ tstoreReceiveSlot_detoast()

static bool tstoreReceiveSlot_detoast ( TupleTableSlot slot,
DestReceiver self 
)
static

Definition at line 109 of file tstoreReceiver.c.

References TStoreState::cxt, DatumGetPointer, heap_tuple_fetch_attr(), i, MemoryContextSwitchTo(), tupleDesc::natts, TStoreState::outvalues, pfree(), PointerGetDatum, slot_getallattrs(), TStoreState::tofree, TStoreState::tstore, TupleTableSlot::tts_isnull, TupleTableSlot::tts_tupleDescriptor, TupleTableSlot::tts_values, TupleDescAttr, tuplestore_putvalues(), val, and VARATT_IS_EXTERNAL.

Referenced by tstoreStartupReceiver().

110 {
111  TStoreState *myState = (TStoreState *) self;
112  TupleDesc typeinfo = slot->tts_tupleDescriptor;
113  int natts = typeinfo->natts;
114  int nfree;
115  int i;
116  MemoryContext oldcxt;
117 
118  /* Make sure the tuple is fully deconstructed */
119  slot_getallattrs(slot);
120 
121  /*
122  * Fetch back any out-of-line datums. We build the new datums array in
123  * myState->outvalues[] (but we can re-use the slot's isnull array). Also,
124  * remember the fetched values to free afterwards.
125  */
126  nfree = 0;
127  for (i = 0; i < natts; i++)
128  {
129  Datum val = slot->tts_values[i];
130  Form_pg_attribute attr = TupleDescAttr(typeinfo, i);
131 
132  if (!attr->attisdropped && attr->attlen == -1 && !slot->tts_isnull[i])
133  {
135  {
137  DatumGetPointer(val)));
138  myState->tofree[nfree++] = val;
139  }
140  }
141 
142  myState->outvalues[i] = val;
143  }
144 
145  /*
146  * Push the modified tuple into the tuplestore.
147  */
148  oldcxt = MemoryContextSwitchTo(myState->cxt);
149  tuplestore_putvalues(myState->tstore, typeinfo,
150  myState->outvalues, slot->tts_isnull);
151  MemoryContextSwitchTo(oldcxt);
152 
153  /* And release any temporary detoasted values */
154  for (i = 0; i < nfree; i++)
155  pfree(DatumGetPointer(myState->tofree[i]));
156 
157  return true;
158 }
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, Datum *values, bool *isnull)
Definition: tuplestore.c:750
Datum * tofree
#define PointerGetDatum(X)
Definition: postgres.h:562
struct varlena * heap_tuple_fetch_attr(struct varlena *attr)
Definition: tuptoaster.c:101
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:90
MemoryContext cxt
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Datum * tts_values
Definition: tuptable.h:125
int natts
Definition: tupdesc.h:79
#define VARATT_IS_EXTERNAL(PTR)
Definition: postgres.h:314
void pfree(void *pointer)
Definition: mcxt.c:936
Tuplestorestate * tstore
bool * tts_isnull
Definition: tuptable.h:126
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
void slot_getallattrs(TupleTableSlot *slot)
Definition: heaptuple.c:1238
TupleDesc tts_tupleDescriptor
Definition: tuptable.h:121
uintptr_t Datum
Definition: postgres.h:372
#define DatumGetPointer(X)
Definition: postgres.h:555
Datum * outvalues
int i
Definition: c.h:497
long val
Definition: informix.c:689

◆ tstoreReceiveSlot_notoast()

static bool tstoreReceiveSlot_notoast ( TupleTableSlot slot,
DestReceiver self 
)
static

Definition at line 95 of file tstoreReceiver.c.

References TStoreState::tstore, and tuplestore_puttupleslot().

Referenced by CreateTuplestoreDestReceiver(), and tstoreStartupReceiver().

96 {
97  TStoreState *myState = (TStoreState *) self;
98 
99  tuplestore_puttupleslot(myState->tstore, slot);
100 
101  return true;
102 }
void tuplestore_puttupleslot(Tuplestorestate *state, TupleTableSlot *slot)
Definition: tuplestore.c:708
Tuplestorestate * tstore

◆ tstoreShutdownReceiver()

static void tstoreShutdownReceiver ( DestReceiver self)
static

Definition at line 164 of file tstoreReceiver.c.

References TStoreState::outvalues, pfree(), and TStoreState::tofree.

Referenced by CreateTuplestoreDestReceiver().

165 {
166  TStoreState *myState = (TStoreState *) self;
167 
168  /* Release workspace if any */
169  if (myState->outvalues)
170  pfree(myState->outvalues);
171  myState->outvalues = NULL;
172  if (myState->tofree)
173  pfree(myState->tofree);
174  myState->tofree = NULL;
175 }
Datum * tofree
void pfree(void *pointer)
Definition: mcxt.c:936
Datum * outvalues

◆ tstoreStartupReceiver()

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

Definition at line 48 of file tstoreReceiver.c.

References TStoreState::cxt, TStoreState::detoast, i, MemoryContextAlloc(), tupleDesc::natts, TStoreState::outvalues, TStoreState::pub, _DestReceiver::receiveSlot, TStoreState::tofree, tstoreReceiveSlot_detoast(), tstoreReceiveSlot_notoast(), and TupleDescAttr.

Referenced by CreateTuplestoreDestReceiver().

49 {
50  TStoreState *myState = (TStoreState *) self;
51  bool needtoast = false;
52  int natts = typeinfo->natts;
53  int i;
54 
55  /* Check if any columns require detoast work */
56  if (myState->detoast)
57  {
58  for (i = 0; i < natts; i++)
59  {
60  Form_pg_attribute attr = TupleDescAttr(typeinfo, i);
61 
62  if (attr->attisdropped)
63  continue;
64  if (attr->attlen == -1)
65  {
66  needtoast = true;
67  break;
68  }
69  }
70  }
71 
72  /* Set up appropriate callback */
73  if (needtoast)
74  {
76  /* Create workspace */
77  myState->outvalues = (Datum *)
78  MemoryContextAlloc(myState->cxt, natts * sizeof(Datum));
79  myState->tofree = (Datum *)
80  MemoryContextAlloc(myState->cxt, natts * sizeof(Datum));
81  }
82  else
83  {
85  myState->outvalues = NULL;
86  myState->tofree = NULL;
87  }
88 }
bool(* receiveSlot)(TupleTableSlot *slot, DestReceiver *self)
Definition: dest.h:118
Datum * tofree
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:90
MemoryContext cxt
DestReceiver pub
int natts
Definition: tupdesc.h:79
static bool tstoreReceiveSlot_notoast(TupleTableSlot *slot, DestReceiver *self)
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
uintptr_t Datum
Definition: postgres.h:372
static bool tstoreReceiveSlot_detoast(TupleTableSlot *slot, DestReceiver *self)
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:693
Datum * outvalues
int i