PostgreSQL Source Code  git master
tqueue.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * tqueue.c
4  * Use shm_mq to send & receive tuples between parallel backends
5  *
6  * A DestReceiver of type DestTupleQueue, which is a TQueueDestReceiver
7  * under the hood, writes tuples from the executor to a shm_mq.
8  *
9  * A TupleQueueReader reads tuples from a shm_mq and returns the tuples.
10  *
11  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
12  * Portions Copyright (c) 1994, Regents of the University of California
13  *
14  * IDENTIFICATION
15  * src/backend/executor/tqueue.c
16  *
17  *-------------------------------------------------------------------------
18  */
19 
20 #include "postgres.h"
21 
22 #include "access/htup_details.h"
23 #include "executor/tqueue.h"
24 
25 /*
26  * DestReceiver object's private contents
27  *
28  * queue is a pointer to data supplied by DestReceiver's caller.
29  */
30 typedef struct TQueueDestReceiver
31 {
32  DestReceiver pub; /* public fields */
33  shm_mq_handle *queue; /* shm_mq to send to */
35 
36 /*
37  * TupleQueueReader object's private contents
38  *
39  * queue is a pointer to data supplied by reader's caller.
40  *
41  * "typedef struct TupleQueueReader TupleQueueReader" is in tqueue.h
42  */
44 {
45  shm_mq_handle *queue; /* shm_mq to receive from */
46 };
47 
48 /*
49  * Receive a tuple from a query, and send it to the designated shm_mq.
50  *
51  * Returns true if successful, false if shm_mq has been detached.
52  */
53 static bool
55 {
56  TQueueDestReceiver *tqueue = (TQueueDestReceiver *) self;
57  HeapTuple tuple;
58  shm_mq_result result;
59 
60  /* Send the tuple itself. */
61  tuple = ExecMaterializeSlot(slot);
62  result = shm_mq_send(tqueue->queue, tuple->t_len, tuple->t_data, false);
63 
64  /* Check for failure. */
65  if (result == SHM_MQ_DETACHED)
66  return false;
67  else if (result != SHM_MQ_SUCCESS)
68  ereport(ERROR,
69  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
70  errmsg("could not send tuple to shared-memory queue")));
71 
72  return true;
73 }
74 
75 /*
76  * Prepare to receive tuples from executor.
77  */
78 static void
79 tqueueStartupReceiver(DestReceiver *self, int operation, TupleDesc typeinfo)
80 {
81  /* do nothing */
82 }
83 
84 /*
85  * Clean up at end of an executor run
86  */
87 static void
89 {
90  TQueueDestReceiver *tqueue = (TQueueDestReceiver *) self;
91 
92  if (tqueue->queue != NULL)
93  shm_mq_detach(tqueue->queue);
94  tqueue->queue = NULL;
95 }
96 
97 /*
98  * Destroy receiver when done with it
99  */
100 static void
102 {
103  TQueueDestReceiver *tqueue = (TQueueDestReceiver *) self;
104 
105  /* We probably already detached from queue, but let's be sure */
106  if (tqueue->queue != NULL)
107  shm_mq_detach(tqueue->queue);
108  pfree(self);
109 }
110 
111 /*
112  * Create a DestReceiver that writes tuples to a tuple queue.
113  */
114 DestReceiver *
116 {
117  TQueueDestReceiver *self;
118 
119  self = (TQueueDestReceiver *) palloc0(sizeof(TQueueDestReceiver));
120 
121  self->pub.receiveSlot = tqueueReceiveSlot;
122  self->pub.rStartup = tqueueStartupReceiver;
123  self->pub.rShutdown = tqueueShutdownReceiver;
124  self->pub.rDestroy = tqueueDestroyReceiver;
125  self->pub.mydest = DestTupleQueue;
126  self->queue = handle;
127 
128  return (DestReceiver *) self;
129 }
130 
131 /*
132  * Create a tuple queue reader.
133  */
136 {
137  TupleQueueReader *reader = palloc0(sizeof(TupleQueueReader));
138 
139  reader->queue = handle;
140 
141  return reader;
142 }
143 
144 /*
145  * Destroy a tuple queue reader.
146  *
147  * Note: cleaning up the underlying shm_mq is the caller's responsibility.
148  * We won't access it here, as it may be detached already.
149  */
150 void
152 {
153  pfree(reader);
154 }
155 
156 /*
157  * Fetch a tuple from a tuple queue reader.
158  *
159  * The return value is NULL if there are no remaining tuples or if
160  * nowait = true and no tuple is ready to return. *done, if not NULL,
161  * is set to true when there are no remaining tuples and otherwise to false.
162  *
163  * The returned tuple, if any, is allocated in CurrentMemoryContext.
164  *
165  * Even when shm_mq_receive() returns SHM_MQ_WOULD_BLOCK, this can still
166  * accumulate bytes from a partially-read message, so it's useful to call
167  * this with nowait = true even if nothing is returned.
168  */
169 HeapTuple
170 TupleQueueReaderNext(TupleQueueReader *reader, bool nowait, bool *done)
171 {
172  HeapTupleData htup;
173  shm_mq_result result;
174  Size nbytes;
175  void *data;
176 
177  if (done != NULL)
178  *done = false;
179 
180  /* Attempt to read a message. */
181  result = shm_mq_receive(reader->queue, &nbytes, &data, nowait);
182 
183  /* If queue is detached, set *done and return NULL. */
184  if (result == SHM_MQ_DETACHED)
185  {
186  if (done != NULL)
187  *done = true;
188  return NULL;
189  }
190 
191  /* In non-blocking mode, bail out if no message ready yet. */
192  if (result == SHM_MQ_WOULD_BLOCK)
193  return NULL;
194  Assert(result == SHM_MQ_SUCCESS);
195 
196  /*
197  * Set up a dummy HeapTupleData pointing to the data from the shm_mq
198  * (which had better be sufficiently aligned).
199  */
201  htup.t_tableOid = InvalidOid;
202  htup.t_len = nbytes;
203  htup.t_data = data;
204 
205  return heap_copytuple(&htup);
206 }
HeapTuple heap_copytuple(HeapTuple tuple)
Definition: heaptuple.c:611
static void tqueueDestroyReceiver(DestReceiver *self)
Definition: tqueue.c:101
void DestroyTupleQueueReader(TupleQueueReader *reader)
Definition: tqueue.c:151
static void tqueueStartupReceiver(DestReceiver *self, int operation, TupleDesc typeinfo)
Definition: tqueue.c:79
shm_mq_handle * queue
Definition: tqueue.c:45
void shm_mq_detach(shm_mq_handle *mqh)
Definition: shm_mq.c:775
HeapTuple TupleQueueReaderNext(TupleQueueReader *reader, bool nowait, bool *done)
Definition: tqueue.c:170
int errcode(int sqlerrcode)
Definition: elog.c:575
static bool tqueueReceiveSlot(TupleTableSlot *slot, DestReceiver *self)
Definition: tqueue.c:54
HeapTupleHeader t_data
Definition: htup.h:67
void pfree(void *pointer)
Definition: mcxt.c:949
#define ERROR
Definition: elog.h:43
ItemPointerData t_self
Definition: htup.h:65
uint32 t_len
Definition: htup.h:64
Oid t_tableOid
Definition: htup.h:66
#define ereport(elevel, rest)
Definition: elog.h:122
TupleQueueReader * CreateTupleQueueReader(shm_mq_handle *handle)
Definition: tqueue.c:135
void * palloc0(Size size)
Definition: mcxt.c:877
#define InvalidOid
Definition: postgres_ext.h:36
shm_mq_result
Definition: shm_mq.h:36
shm_mq_handle * queue
Definition: tqueue.c:33
#define Assert(condition)
Definition: c.h:670
size_t Size
Definition: c.h:404
shm_mq_result shm_mq_send(shm_mq_handle *mqh, Size nbytes, const void *data, bool nowait)
Definition: shm_mq.c:325
static void tqueueShutdownReceiver(DestReceiver *self)
Definition: tqueue.c:88
HeapTuple ExecMaterializeSlot(TupleTableSlot *slot)
Definition: execTuples.c:725
struct TQueueDestReceiver TQueueDestReceiver
#define ItemPointerSetInvalid(pointer)
Definition: itemptr.h:150
int errmsg(const char *fmt,...)
Definition: elog.c:797
DestReceiver pub
Definition: tqueue.c:32
DestReceiver * CreateTupleQueueDestReceiver(shm_mq_handle *handle)
Definition: tqueue.c:115
shm_mq_result shm_mq_receive(shm_mq_handle *mqh, Size *nbytesp, void **datap, bool nowait)
Definition: shm_mq.c:522