PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
logicalfuncs.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * logicalfuncs.c
4  *
5  * Support functions for using logical decoding and management of
6  * logical replication slots via SQL.
7  *
8  *
9  * Copyright (c) 2012-2016, PostgreSQL Global Development Group
10  *
11  * IDENTIFICATION
12  * src/backend/replication/logicalfuncs.c
13  *-------------------------------------------------------------------------
14  */
15 
16 #include "postgres.h"
17 
18 #include <unistd.h>
19 
20 #include "fmgr.h"
21 #include "funcapi.h"
22 #include "miscadmin.h"
23 
24 #include "access/xlog_internal.h"
25 #include "access/xlogutils.h"
26 
27 #include "access/xact.h"
28 
29 #include "catalog/pg_type.h"
30 
31 #include "nodes/makefuncs.h"
32 
33 #include "mb/pg_wchar.h"
34 
35 #include "utils/array.h"
36 #include "utils/builtins.h"
37 #include "utils/inval.h"
38 #include "utils/memutils.h"
39 #include "utils/pg_lsn.h"
40 #include "utils/resowner.h"
41 #include "utils/lsyscache.h"
42 
43 #include "replication/decode.h"
44 #include "replication/logical.h"
46 #include "replication/message.h"
47 
48 #include "storage/fd.h"
49 
50 /* private date for writing out data */
51 typedef struct DecodingOutputState
52 {
58 
59 /*
60  * Prepare for an output plugin write.
61  */
62 static void
64  bool last_write)
65 {
66  resetStringInfo(ctx->out);
67 }
68 
69 /*
70  * Perform output plugin write into tuplestore.
71  */
72 static void
74  bool last_write)
75 {
76  Datum values[3];
77  bool nulls[3];
79 
80  /* SQL Datums can only be of a limited length... */
81  if (ctx->out->len > MaxAllocSize - VARHDRSZ)
82  elog(ERROR, "too much output for sql interface");
83 
85 
86  memset(nulls, 0, sizeof(nulls));
87  values[0] = LSNGetDatum(lsn);
88  values[1] = TransactionIdGetDatum(xid);
89 
90  /*
91  * Assert ctx->out is in database encoding when we're writing textual
92  * output.
93  */
94  if (!p->binary_output)
96  ctx->out->data, ctx->out->len,
97  false));
98 
99  /* ick, but cstring_to_text_with_len works for bytea perfectly fine */
100  values[2] = PointerGetDatum(
101  cstring_to_text_with_len(ctx->out->data, ctx->out->len));
102 
103  tuplestore_putvalues(p->tupstore, p->tupdesc, values, nulls);
104  p->returned_rows++;
105 }
106 
107 static void
109 {
110  if (!superuser() && !has_rolreplication(GetUserId()))
111  ereport(ERROR,
112  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
113  (errmsg("must be superuser or replication role to use replication slots"))));
114 }
115 
116 int
118  int reqLen, XLogRecPtr targetRecPtr, char *cur_page, TimeLineID *pageTLI)
119 {
120  return read_local_xlog_page(state, targetPagePtr, reqLen,
121  targetRecPtr, cur_page, pageTLI);
122 }
123 
124 /*
125  * Helper function for the various SQL callable logical decoding functions.
126  */
127 static Datum
128 pg_logical_slot_get_changes_guts(FunctionCallInfo fcinfo, bool confirm, bool binary)
129 {
130  Name name;
131  XLogRecPtr upto_lsn;
132  int32 upto_nchanges;
133  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
134  MemoryContext per_query_ctx;
135  MemoryContext oldcontext;
136  XLogRecPtr end_of_wal;
137  XLogRecPtr startptr;
139  ResourceOwner old_resowner = CurrentResourceOwner;
140  ArrayType *arr;
141  Size ndim;
142  List *options = NIL;
144 
146 
148 
149  if (PG_ARGISNULL(0))
150  ereport(ERROR,
151  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
152  errmsg("slot name must not be null")));
153  name = PG_GETARG_NAME(0);
154 
155  if (PG_ARGISNULL(1))
156  upto_lsn = InvalidXLogRecPtr;
157  else
158  upto_lsn = PG_GETARG_LSN(1);
159 
160  if (PG_ARGISNULL(2))
161  upto_nchanges = InvalidXLogRecPtr;
162  else
163  upto_nchanges = PG_GETARG_INT32(2);
164 
165  if (PG_ARGISNULL(3))
166  ereport(ERROR,
167  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
168  errmsg("options array must not be null")));
169  arr = PG_GETARG_ARRAYTYPE_P(3);
170 
171  /* check to see if caller supports us returning a tuplestore */
172  if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
173  ereport(ERROR,
174  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
175  errmsg("set-valued function called in context that cannot accept a set")));
176  if (!(rsinfo->allowedModes & SFRM_Materialize))
177  ereport(ERROR,
178  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
179  errmsg("materialize mode required, but it is not allowed in this context")));
180 
181  /* state to write output to */
182  p = palloc0(sizeof(DecodingOutputState));
183 
184  p->binary_output = binary;
185 
186  /* Build a tuple descriptor for our result type */
187  if (get_call_result_type(fcinfo, NULL, &p->tupdesc) != TYPEFUNC_COMPOSITE)
188  elog(ERROR, "return type must be a row type");
189 
190  per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
191  oldcontext = MemoryContextSwitchTo(per_query_ctx);
192 
193  /* Deconstruct options array */
194  ndim = ARR_NDIM(arr);
195  if (ndim > 1)
196  {
197  ereport(ERROR,
198  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
199  errmsg("array must be one-dimensional")));
200  }
201  else if (array_contains_nulls(arr))
202  {
203  ereport(ERROR,
204  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
205  errmsg("array must not contain nulls")));
206  }
207  else if (ndim == 1)
208  {
209  int nelems;
210  Datum *datum_opts;
211  int i;
212 
213  Assert(ARR_ELEMTYPE(arr) == TEXTOID);
214 
215  deconstruct_array(arr, TEXTOID, -1, false, 'i',
216  &datum_opts, NULL, &nelems);
217 
218  if (nelems % 2 != 0)
219  ereport(ERROR,
220  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
221  errmsg("array must have even number of elements")));
222 
223  for (i = 0; i < nelems; i += 2)
224  {
225  char *name = TextDatumGetCString(datum_opts[i]);
226  char *opt = TextDatumGetCString(datum_opts[i + 1]);
227 
228  options = lappend(options, makeDefElem(name, (Node *) makeString(opt)));
229  }
230  }
231 
232  p->tupstore = tuplestore_begin_heap(true, false, work_mem);
233  rsinfo->returnMode = SFRM_Materialize;
234  rsinfo->setResult = p->tupstore;
235  rsinfo->setDesc = p->tupdesc;
236 
237  /* compute the current end-of-wal */
238  if (!RecoveryInProgress())
239  end_of_wal = GetFlushRecPtr();
240  else
241  end_of_wal = GetXLogReplayRecPtr(NULL);
242 
244 
245  PG_TRY();
246  {
247  /* restart at slot's confirmed_flush */
249  options,
253 
254  MemoryContextSwitchTo(oldcontext);
255 
256  /*
257  * Check whether the output plugin writes textual output if that's
258  * what we need.
259  */
260  if (!binary &&
261  ctx->options.output_type !=OUTPUT_PLUGIN_TEXTUAL_OUTPUT)
262  ereport(ERROR,
263  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
264  errmsg("logical decoding output plugin \"%s\" produces binary output, but function \"%s\" expects textual data",
266  format_procedure(fcinfo->flinfo->fn_oid))));
267 
268  ctx->output_writer_private = p;
269 
270  /*
271  * Decoding of WAL must start at restart_lsn so that the entirety of
272  * xacts that committed after the slot's confirmed_flush can be
273  * accumulated into reorder buffers.
274  */
275  startptr = MyReplicationSlot->data.restart_lsn;
276 
278 
279  /* invalidate non-timetravel entries */
281 
282  while ((startptr != InvalidXLogRecPtr && startptr < end_of_wal) ||
283  (ctx->reader->EndRecPtr != InvalidXLogRecPtr && ctx->reader->EndRecPtr < end_of_wal))
284  {
285  XLogRecord *record;
286  char *errm = NULL;
287 
288  record = XLogReadRecord(ctx->reader, startptr, &errm);
289  if (errm)
290  elog(ERROR, "%s", errm);
291 
292  /*
293  * Now that we've set up the xlog reader state, subsequent calls
294  * pass InvalidXLogRecPtr to say "continue from last record"
295  */
296  startptr = InvalidXLogRecPtr;
297 
298  /*
299  * The {begin_txn,change,commit_txn}_wrapper callbacks above will
300  * store the description into our tuplestore.
301  */
302  if (record != NULL)
303  LogicalDecodingProcessRecord(ctx, ctx->reader);
304 
305  /* check limits */
306  if (upto_lsn != InvalidXLogRecPtr &&
307  upto_lsn <= ctx->reader->EndRecPtr)
308  break;
309  if (upto_nchanges != 0 &&
310  upto_nchanges <= p->returned_rows)
311  break;
313  }
314 
315  tuplestore_donestoring(tupstore);
316 
317  CurrentResourceOwner = old_resowner;
318 
319  /*
320  * Next time, start where we left off. (Hunting things, the family
321  * business..)
322  */
323  if (ctx->reader->EndRecPtr != InvalidXLogRecPtr && confirm)
324  LogicalConfirmReceivedLocation(ctx->reader->EndRecPtr);
325 
326  /* free context, call shutdown callback */
327  FreeDecodingContext(ctx);
328 
331  }
332  PG_CATCH();
333  {
334  /* clear all timetravel entries */
336 
337  PG_RE_THROW();
338  }
339  PG_END_TRY();
340 
341  return (Datum) 0;
342 }
343 
344 /*
345  * SQL function returning the changestream as text, consuming the data.
346  */
347 Datum
349 {
350  return pg_logical_slot_get_changes_guts(fcinfo, true, false);
351 }
352 
353 /*
354  * SQL function returning the changestream as text, only peeking ahead.
355  */
356 Datum
358 {
359  return pg_logical_slot_get_changes_guts(fcinfo, false, false);
360 }
361 
362 /*
363  * SQL function returning the changestream in binary, consuming the data.
364  */
365 Datum
367 {
368  return pg_logical_slot_get_changes_guts(fcinfo, true, true);
369 }
370 
371 /*
372  * SQL function returning the changestream in binary, only peeking ahead.
373  */
374 Datum
376 {
377  return pg_logical_slot_get_changes_guts(fcinfo, false, true);
378 }
379 
380 
381 /*
382  * SQL function for writing logical decoding message into WAL.
383  */
384 Datum
386 {
387  bool transactional = PG_GETARG_BOOL(0);
388  char *prefix = text_to_cstring(PG_GETARG_TEXT_PP(1));
389  bytea *data = PG_GETARG_BYTEA_PP(2);
390  XLogRecPtr lsn;
391 
392  lsn = LogLogicalMessage(prefix, VARDATA_ANY(data), VARSIZE_ANY_EXHDR(data),
393  transactional);
394  PG_RETURN_LSN(lsn);
395 }
396 
397 Datum
399 {
400  /* bytea and text are compatible */
401  return pg_logical_emit_message_bytea(fcinfo);
402 }
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, Datum *values, bool *isnull)
Definition: tuplestore.c:735
Value * makeString(char *str)
Definition: value.c:53
#define NIL
Definition: pg_list.h:69
#define PG_GETARG_INT32(n)
Definition: fmgr.h:225
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define IsA(nodeptr, _type_)
Definition: nodes.h:542
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:212
#define VARDATA_ANY(PTR)
Definition: postgres.h:349
uint32 TimeLineID
Definition: xlogdefs.h:45
uint32 TransactionId
Definition: c.h:393
int logical_read_local_xlog_page(XLogReaderState *state, XLogRecPtr targetPagePtr, int reqLen, XLogRecPtr targetRecPtr, char *cur_page, TimeLineID *pageTLI)
Definition: logicalfuncs.c:117
Oid GetUserId(void)
Definition: miscinit.c:282
#define TEXTOID
Definition: pg_type.h:324
#define PointerGetDatum(X)
Definition: postgres.h:564
#define VARHDRSZ
Definition: c.h:440
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
static void check_permissions(void)
Definition: logicalfuncs.c:108
#define tuplestore_donestoring(state)
Definition: tuplestore.h:60
Datum pg_logical_emit_message_bytea(PG_FUNCTION_ARGS)
Definition: logicalfuncs.c:385
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Definition: nodes.h:491
int errcode(int sqlerrcode)
Definition: elog.c:575
#define LSNGetDatum(X)
Definition: pg_lsn.h:38
bool superuser(void)
Definition: superuser.c:47
Datum pg_logical_slot_get_binary_changes(PG_FUNCTION_ARGS)
Definition: logicalfuncs.c:366
Tuplestorestate * tupstore
Definition: logicalfuncs.c:53
XLogRecPtr GetFlushRecPtr(void)
Definition: xlog.c:7894
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:230
ReplicationSlotPersistentData data
Definition: slot.h:114
bool RecoveryInProgress(void)
Definition: xlog.c:7547
void InvalidateSystemCaches(void)
Definition: inval.c:622
XLogRecord * XLogReadRecord(XLogReaderState *state, XLogRecPtr RecPtr, char **errormsg)
Definition: xlogreader.c:193
signed int int32
Definition: c.h:253
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:270
#define PG_RETURN_LSN(x)
Definition: pg_lsn.h:41
int read_local_xlog_page(XLogReaderState *state, XLogRecPtr targetPagePtr, int reqLen, XLogRecPtr targetRecPtr, char *cur_page, TimeLineID *pageTLI)
Definition: xlogutils.c:764
FmgrInfo * flinfo
Definition: fmgr.h:71
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:244
static Datum pg_logical_slot_get_changes_guts(FunctionCallInfo fcinfo, bool confirm, bool binary)
Definition: logicalfuncs.c:128
#define ERROR
Definition: elog.h:43
void LogicalDecodingProcessRecord(LogicalDecodingContext *ctx, XLogReaderState *record)
Definition: decode.c:93
struct DecodingOutputState DecodingOutputState
XLogRecPtr GetXLogReplayRecPtr(TimeLineID *replayTLI)
Definition: xlog.c:10616
Definition: c.h:488
LogicalDecodingContext * CreateDecodingContext(XLogRecPtr start_lsn, List *output_plugin_options, XLogPageReadCB read_page, LogicalOutputPluginWriterPrepareWrite prepare_write, LogicalOutputPluginWriterWrite do_write)
Definition: logical.c:329
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:162
fmNodePtr resultinfo
Definition: fmgr.h:73
void resetStringInfo(StringInfo str)
Definition: stringinfo.c:62
void ReplicationSlotRelease(void)
Definition: slot.c:367
static void LogicalOutputPrepareWrite(LogicalDecodingContext *ctx, XLogRecPtr lsn, TransactionId xid, bool last_write)
Definition: logicalfuncs.c:63
bool pg_verify_mbstr(int encoding, const char *mbstr, int len, bool noError)
Definition: wchar.c:1877
#define ereport(elevel, rest)
Definition: elog.h:122
List * lappend(List *list, void *datum)
Definition: list.c:128
XLogRecPtr LogLogicalMessage(const char *prefix, const char *message, size_t size, bool transactional)
Definition: message.c:51
#define MaxAllocSize
Definition: memutils.h:40
DefElem * makeDefElem(char *name, Node *arg)
Definition: makefuncs.c:513
Tuplestorestate * tuplestore_begin_heap(bool randomAccess, bool interXact, int maxKBytes)
Definition: tuplestore.c:316
#define TextDatumGetCString(d)
Definition: builtins.h:807
#define TransactionIdGetDatum(X)
Definition: postgres.h:529
void * palloc0(Size size)
Definition: mcxt.c:923
#define PG_GETARG_LSN(n)
Definition: pg_lsn.h:40
uintptr_t Datum
Definition: postgres.h:374
int GetDatabaseEncoding(void)
Definition: mbutils.c:1015
int work_mem
Definition: globals.c:110
Oid fn_oid
Definition: fmgr.h:56
int allowedModes
Definition: execnodes.h:192
char * format_procedure(Oid procedure_oid)
Definition: regproc.c:365
Datum pg_logical_slot_get_changes(PG_FUNCTION_ARGS)
Definition: logicalfuncs.c:348
SetFunctionReturnMode returnMode
Definition: execnodes.h:194
#define PG_CATCH()
Definition: elog.h:292
ReplicationSlot * MyReplicationSlot
Definition: slot.c:94
#define PG_ARGISNULL(n)
Definition: fmgr.h:166
#define NULL
Definition: c.h:226
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:667
Definition: regguts.h:313
void FreeDecodingContext(LogicalDecodingContext *ctx)
Definition: logical.c:459
XLogRecPtr restart_lsn
Definition: slot.h:67
void ReplicationSlotAcquire(const char *name)
Definition: slot.c:318
size_t Size
Definition: c.h:352
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:268
bool has_rolreplication(Oid roleid)
Definition: miscinit.c:463
#define PG_RE_THROW()
Definition: elog.h:313
void LogicalConfirmReceivedLocation(XLogRecPtr lsn)
Definition: logical.c:886
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:127
#define ARR_NDIM(a)
Definition: array.h:271
const char * name
Definition: encode.c:521
Tuplestorestate * setResult
Definition: execnodes.h:197
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3475
static Datum values[MAXATTR]
Definition: bootstrap.c:160
char * text_to_cstring(const text *t)
Definition: varlena.c:183
ExprContext * econtext
Definition: execnodes.h:190
TupleDesc setDesc
Definition: execnodes.h:198
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:342
int errmsg(const char *fmt,...)
Definition: elog.c:797
StringInfo out
Definition: logical.h:57
int i
#define NameStr(name)
Definition: c.h:494
Datum pg_logical_slot_peek_changes(PG_FUNCTION_ARGS)
Definition: logicalfuncs.c:357
Definition: c.h:434
#define PG_FUNCTION_ARGS
Definition: fmgr.h:150
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:97
#define elog
Definition: elog.h:218
#define PG_TRY()
Definition: elog.h:283
bool array_contains_nulls(ArrayType *array)
Definition: arrayfuncs.c:3542
Definition: pg_list.h:45
void CheckLogicalDecodingRequirements(void)
Definition: logical.c:76
#define ARR_ELEMTYPE(a)
Definition: array.h:273
void * output_writer_private
Definition: logical.h:67
#define PG_END_TRY()
Definition: elog.h:299
static void LogicalOutputWrite(LogicalDecodingContext *ctx, XLogRecPtr lsn, TransactionId xid, bool last_write)
Definition: logicalfuncs.c:73
#define PG_GETARG_NAME(n)
Definition: fmgr.h:234
Datum pg_logical_emit_message_text(PG_FUNCTION_ARGS)
Definition: logicalfuncs.c:398
ResourceOwner ResourceOwnerCreate(ResourceOwner parent, const char *name)
Definition: resowner.c:416
Datum pg_logical_slot_peek_binary_changes(PG_FUNCTION_ARGS)
Definition: logicalfuncs.c:375