PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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

DestReceiver* CreateTuplestoreDestReceiver ( void  )

Definition at line 191 of file tstoreReceiver.c.

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

Referenced by CreateDestReceiver().

192 {
193  TStoreState *self = (TStoreState *) palloc0(sizeof(TStoreState));
194 
195  self->pub.receiveSlot = tstoreReceiveSlot_notoast; /* might change */
196  self->pub.rStartup = tstoreStartupReceiver;
197  self->pub.rShutdown = tstoreShutdownReceiver;
198  self->pub.rDestroy = tstoreDestroyReceiver;
199  self->pub.mydest = DestTuplestore;
200 
201  /* private fields will be set by SetTuplestoreDestReceiverParams */
202 
203  return (DestReceiver *) self;
204 }
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:878
static void tstoreDestroyReceiver(DestReceiver *self)
void SetTuplestoreDestReceiverParams ( DestReceiver self,
Tuplestorestate tStore,
MemoryContext  tContext,
bool  detoast 
)

Definition at line 210 of file tstoreReceiver.c.

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

Referenced by FillPortalStore(), and PersistHoldablePortal().

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

Definition at line 182 of file tstoreReceiver.c.

References pfree().

Referenced by CreateTuplestoreDestReceiver().

183 {
184  pfree(self);
185 }
void pfree(void *pointer)
Definition: mcxt.c:950
static bool tstoreReceiveSlot_detoast ( TupleTableSlot slot,
DestReceiver self 
)
static

Definition at line 108 of file tstoreReceiver.c.

References tupleDesc::attrs, 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, tuplestore_putvalues(), val, and VARATT_IS_EXTERNAL.

Referenced by tstoreStartupReceiver().

109 {
110  TStoreState *myState = (TStoreState *) self;
111  TupleDesc typeinfo = slot->tts_tupleDescriptor;
112  Form_pg_attribute *attrs = typeinfo->attrs;
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 
131  if (!attrs[i]->attisdropped &&
132  attrs[i]->attlen == -1 &&
133  !slot->tts_isnull[i])
134  {
136  {
138  DatumGetPointer(val)));
139  myState->tofree[nfree++] = val;
140  }
141  }
142 
143  myState->outvalues[i] = val;
144  }
145 
146  /*
147  * Push the modified tuple into the tuplestore.
148  */
149  oldcxt = MemoryContextSwitchTo(myState->cxt);
150  tuplestore_putvalues(myState->tstore, typeinfo,
151  myState->outvalues, slot->tts_isnull);
152  MemoryContextSwitchTo(oldcxt);
153 
154  /* And release any temporary detoasted values */
155  for (i = 0; i < nfree; i++)
156  pfree(DatumGetPointer(myState->tofree[i]));
157 
158  return true;
159 }
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, Datum *values, bool *isnull)
Definition: tuplestore.c:735
Datum * tofree
#define PointerGetDatum(X)
Definition: postgres.h:562
struct varlena * heap_tuple_fetch_attr(struct varlena *attr)
Definition: tuptoaster.c:101
MemoryContext cxt
Form_pg_attribute * attrs
Definition: tupdesc.h:74
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Datum * tts_values
Definition: tuptable.h:125
int natts
Definition: tupdesc.h:73
#define VARATT_IS_EXTERNAL(PTR)
Definition: postgres.h:314
void pfree(void *pointer)
Definition: mcxt.c:950
Tuplestorestate * tstore
bool * tts_isnull
Definition: tuptable.h:126
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:184
void slot_getallattrs(TupleTableSlot *slot)
Definition: heaptuple.c:1239
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:439
long val
Definition: informix.c:689
static bool tstoreReceiveSlot_notoast ( TupleTableSlot slot,
DestReceiver self 
)
static

Definition at line 94 of file tstoreReceiver.c.

References TStoreState::tstore, and tuplestore_puttupleslot().

Referenced by CreateTuplestoreDestReceiver(), and tstoreStartupReceiver().

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

Definition at line 165 of file tstoreReceiver.c.

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

Referenced by CreateTuplestoreDestReceiver().

166 {
167  TStoreState *myState = (TStoreState *) self;
168 
169  /* Release workspace if any */
170  if (myState->outvalues)
171  pfree(myState->outvalues);
172  myState->outvalues = NULL;
173  if (myState->tofree)
174  pfree(myState->tofree);
175  myState->tofree = NULL;
176 }
Datum * tofree
void pfree(void *pointer)
Definition: mcxt.c:950
#define NULL
Definition: c.h:229
Datum * outvalues
static void tstoreStartupReceiver ( DestReceiver self,
int  operation,
TupleDesc  typeinfo 
)
static

Definition at line 48 of file tstoreReceiver.c.

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

Referenced by CreateTuplestoreDestReceiver().

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