PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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-2026, PostgreSQL Global Development Group
10 *
11 * IDENTIFICATION
12 * src/backend/replication/logical/logicalfuncs.c
13 *-------------------------------------------------------------------------
14 */
15
16#include "postgres.h"
17
18#include <unistd.h>
19
20#include "access/xlogrecovery.h"
21#include "access/xlogutils.h"
22#include "catalog/pg_type.h"
23#include "fmgr.h"
24#include "funcapi.h"
25#include "mb/pg_wchar.h"
26#include "miscadmin.h"
27#include "nodes/makefuncs.h"
28#include "replication/decode.h"
29#include "replication/logical.h"
30#include "replication/message.h"
31#include "utils/array.h"
32#include "utils/builtins.h"
33#include "utils/inval.h"
34#include "utils/memutils.h"
35#include "utils/pg_lsn.h"
36#include "utils/regproc.h"
37#include "utils/resowner.h"
38
39/* Private data for writing out data */
47
48/*
49 * Prepare for an output plugin write.
50 */
51static void
57
58/*
59 * Perform output plugin write into tuplestore.
60 */
61static void
63 bool last_write)
64{
65 Datum values[3];
66 bool nulls[3];
68
69 /* SQL Datums can only be of a limited length... */
70 if (ctx->out->len > MaxAllocSize - VARHDRSZ)
71 elog(ERROR, "too much output for sql interface");
72
74
75 memset(nulls, 0, sizeof(nulls));
76 values[0] = LSNGetDatum(lsn);
78
79 /*
80 * Assert ctx->out is in database encoding when we're writing textual
81 * output.
82 */
83 if (!p->binary_output)
85 ctx->out->data, ctx->out->len,
86 false));
87
88 /* ick, but cstring_to_text_with_len works for bytea perfectly fine */
90
92 p->returned_rows++;
93}
94
95/*
96 * Helper function for the various SQL callable logical decoding functions.
97 */
98static Datum
100{
101 Name name;
106 MemoryContext oldcontext;
107 XLogRecPtr end_of_wal;
111 ArrayType *arr;
112 Size ndim;
113 List *options = NIL;
115
117
119
120 if (PG_ARGISNULL(0))
123 errmsg("slot name must not be null")));
124 name = PG_GETARG_NAME(0);
125
126 if (PG_ARGISNULL(1))
128 else
130
131 if (PG_ARGISNULL(2))
132 upto_nchanges = 0;
133 else
135
136 if (PG_ARGISNULL(3))
139 errmsg("options array must not be null")));
140 arr = PG_GETARG_ARRAYTYPE_P(3);
141
142 /* state to write output to */
144
145 p->binary_output = binary;
146
147 per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
149
150 /* Deconstruct options array */
151 ndim = ARR_NDIM(arr);
152 if (ndim > 1)
153 {
156 errmsg("array must be one-dimensional")));
157 }
158 else if (array_contains_nulls(arr))
159 {
162 errmsg("array must not contain nulls")));
163 }
164 else if (ndim == 1)
165 {
166 int nelems;
168 int i;
169
170 Assert(ARR_ELEMTYPE(arr) == TEXTOID);
171
173
174 if (nelems % 2 != 0)
177 errmsg("array must have even number of elements")));
178
179 for (i = 0; i < nelems; i += 2)
180 {
181 char *optname = TextDatumGetCString(datum_opts[i]);
182 char *opt = TextDatumGetCString(datum_opts[i + 1]);
183
184 options = lappend(options, makeDefElem(optname, (Node *) makeString(opt), -1));
185 }
186 }
187
188 InitMaterializedSRF(fcinfo, 0);
189 p->tupstore = rsinfo->setResult;
190 p->tupdesc = rsinfo->setDesc;
191
192 /*
193 * Compute the current end-of-wal.
194 */
195 if (!RecoveryInProgress())
196 end_of_wal = GetFlushRecPtr(NULL);
197 else
198 end_of_wal = GetXLogReplayRecPtr(NULL);
199
200 ReplicationSlotAcquire(NameStr(*name), true, true);
201
202 PG_TRY();
203 {
204 /* restart at slot's confirmed_flush */
206 options,
207 false,
208 XL_ROUTINE(.page_read = read_local_xlog_page,
209 .segment_open = wal_segment_open,
210 .segment_close = wal_segment_close),
213
214 MemoryContextSwitchTo(oldcontext);
215
216 /*
217 * Check whether the output plugin writes textual output if that's
218 * what we need.
219 */
220 if (!binary &&
224 errmsg("logical decoding output plugin \"%s\" produces binary output, but function \"%s\" expects textual data",
226 format_procedure(fcinfo->flinfo->fn_oid))));
227
228 /*
229 * Wait for specified streaming replication standby servers (if any)
230 * to confirm receipt of WAL up to wait_for_wal_lsn.
231 */
233 wait_for_wal_lsn = end_of_wal;
234 else
235 wait_for_wal_lsn = Min(upto_lsn, end_of_wal);
236
238
239 ctx->output_writer_private = p;
240
241 /*
242 * Decoding of WAL must start at restart_lsn so that the entirety of
243 * xacts that committed after the slot's confirmed_flush can be
244 * accumulated into reorder buffers.
245 */
247
248 /* invalidate non-timetravel entries */
250
251 /* Decode until we run out of records */
252 while (ctx->reader->EndRecPtr < end_of_wal)
253 {
254 XLogRecord *record;
255 char *errm = NULL;
256
257 record = XLogReadRecord(ctx->reader, &errm);
258 if (errm)
259 elog(ERROR, "could not find record for logical decoding: %s", errm);
260
261 /*
262 * The {begin_txn,change,commit_txn}_wrapper callbacks above will
263 * store the description into our tuplestore.
264 */
265 if (record != NULL)
266 {
268
269 /*
270 * We used to have bugs where logical decoding would fail to
271 * preserve the resource owner. Verify that that doesn't
272 * happen anymore. XXX this could be removed once it's been
273 * battle-tested.
274 */
276 }
277
278 /* check limits */
280 upto_lsn <= ctx->reader->EndRecPtr)
281 break;
282 if (upto_nchanges != 0 &&
283 upto_nchanges <= p->returned_rows)
284 break;
286 }
287
288 /*
289 * Next time, start where we left off. (Hunting things, the family
290 * business..)
291 */
293 {
295
296 /*
297 * If only the confirmed_flush_lsn has changed the slot won't get
298 * marked as dirty by the above. Callers on the walsender
299 * interface are expected to keep track of their own progress and
300 * don't need it written out. But SQL-interface users cannot
301 * specify their own start positions and it's harder for them to
302 * keep track of their progress, so we should make more of an
303 * effort to save it for them.
304 *
305 * Dirty the slot so it's written out at the next checkpoint.
306 * We'll still lose its position on crash, as documented, but it's
307 * better than always losing the position even on clean restart.
308 */
310 }
311
312 /* free context, call shutdown callback */
314
317 }
318 PG_CATCH();
319 {
320 /* clear all timetravel entries */
322
323 PG_RE_THROW();
324 }
325 PG_END_TRY();
326
327 return (Datum) 0;
328}
329
330/*
331 * SQL function returning the changestream as text, consuming the data.
332 */
333Datum
338
339/*
340 * SQL function returning the changestream as text, only peeking ahead.
341 */
342Datum
347
348/*
349 * SQL function returning the changestream in binary, consuming the data.
350 */
351Datum
356
357/*
358 * SQL function returning the changestream in binary, only peeking ahead.
359 */
360Datum
365
366
367/*
368 * SQL function for writing logical decoding message into WAL.
369 */
370Datum
372{
373 bool transactional = PG_GETARG_BOOL(0);
374 char *prefix = text_to_cstring(PG_GETARG_TEXT_PP(1));
376 bool flush = PG_GETARG_BOOL(3);
377 XLogRecPtr lsn;
378
380 transactional, flush);
381 PG_RETURN_LSN(lsn);
382}
383
384Datum
386{
387 /* bytea and text are compatible */
388 return pg_logical_emit_message_bytea(fcinfo);
389}
#define ARR_NDIM(a)
Definition array.h:290
#define PG_GETARG_ARRAYTYPE_P(n)
Definition array.h:263
#define ARR_ELEMTYPE(a)
Definition array.h:292
bool array_contains_nulls(const ArrayType *array)
void deconstruct_array_builtin(const ArrayType *array, Oid elmtype, Datum **elemsp, bool **nullsp, int *nelemsp)
static Datum values[MAXATTR]
Definition bootstrap.c:155
#define TextDatumGetCString(d)
Definition builtins.h:98
#define NameStr(name)
Definition c.h:775
#define Min(x, y)
Definition c.h:1007
#define PG_USED_FOR_ASSERTS_ONLY
Definition c.h:223
#define VARHDRSZ
Definition c.h:721
#define Assert(condition)
Definition c.h:883
int64_t int64
Definition c.h:553
int32_t int32
Definition c.h:552
uint32 TransactionId
Definition c.h:676
size_t Size
Definition c.h:629
void LogicalDecodingProcessRecord(LogicalDecodingContext *ctx, XLogReaderState *record)
Definition decode.c:88
int errcode(int sqlerrcode)
Definition elog.c:863
int errmsg(const char *fmt,...)
Definition elog.c:1080
#define PG_RE_THROW()
Definition elog.h:405
#define PG_TRY(...)
Definition elog.h:372
#define PG_END_TRY(...)
Definition elog.h:397
#define ERROR
Definition elog.h:39
#define PG_CATCH(...)
Definition elog.h:382
#define elog(elevel,...)
Definition elog.h:226
#define ereport(elevel,...)
Definition elog.h:150
#define MaxAllocSize
Definition fe_memutils.h:22
#define palloc0_object(type)
Definition fe_memutils.h:75
#define PG_GETARG_BYTEA_PP(n)
Definition fmgr.h:309
#define PG_GETARG_TEXT_PP(n)
Definition fmgr.h:310
#define PG_ARGISNULL(n)
Definition fmgr.h:209
#define PG_GETARG_NAME(n)
Definition fmgr.h:279
#define PG_GETARG_INT32(n)
Definition fmgr.h:269
#define PG_GETARG_BOOL(n)
Definition fmgr.h:274
#define PG_FUNCTION_ARGS
Definition fmgr.h:193
void InitMaterializedSRF(FunctionCallInfo fcinfo, bits32 flags)
Definition funcapi.c:76
void InvalidateSystemCaches(void)
Definition inval.c:916
int i
Definition isn.c:77
List * lappend(List *list, void *datum)
Definition list.c:339
void LogicalConfirmReceivedLocation(XLogRecPtr lsn)
Definition logical.c:1811
void FreeDecodingContext(LogicalDecodingContext *ctx)
Definition logical.c:668
LogicalDecodingContext * CreateDecodingContext(XLogRecPtr start_lsn, List *output_plugin_options, bool fast_forward, XLogReaderRoutine *xl_routine, LogicalOutputPluginWriterPrepareWrite prepare_write, LogicalOutputPluginWriterWrite do_write, LogicalOutputPluginWriterUpdateProgress update_progress)
Definition logical.c:489
void CheckLogicalDecodingRequirements(void)
Definition logical.c:111
static void LogicalOutputPrepareWrite(LogicalDecodingContext *ctx, XLogRecPtr lsn, TransactionId xid, bool last_write)
Datum pg_logical_slot_get_changes(PG_FUNCTION_ARGS)
static void LogicalOutputWrite(LogicalDecodingContext *ctx, XLogRecPtr lsn, TransactionId xid, bool last_write)
Datum pg_logical_emit_message_bytea(PG_FUNCTION_ARGS)
Datum pg_logical_slot_get_binary_changes(PG_FUNCTION_ARGS)
Datum pg_logical_emit_message_text(PG_FUNCTION_ARGS)
Datum pg_logical_slot_peek_binary_changes(PG_FUNCTION_ARGS)
Datum pg_logical_slot_peek_changes(PG_FUNCTION_ARGS)
static Datum pg_logical_slot_get_changes_guts(FunctionCallInfo fcinfo, bool confirm, bool binary)
DefElem * makeDefElem(char *name, Node *arg, int location)
Definition makefuncs.c:637
int GetDatabaseEncoding(void)
Definition mbutils.c:1264
bool pg_verify_mbstr(int encoding, const char *mbstr, int len, bool noError)
Definition mbutils.c:1569
XLogRecPtr LogLogicalMessage(const char *prefix, const char *message, size_t size, bool transactional, bool flush)
Definition message.c:43
#define CHECK_FOR_INTERRUPTS()
Definition miscadmin.h:123
@ OUTPUT_PLUGIN_TEXTUAL_OUTPUT
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:124
const void * data
#define NIL
Definition pg_list.h:68
#define PG_GETARG_LSN(n)
Definition pg_lsn.h:36
static Datum LSNGetDatum(XLogRecPtr X)
Definition pg_lsn.h:31
#define PG_RETURN_LSN(x)
Definition pg_lsn.h:37
static Datum PointerGetDatum(const void *X)
Definition postgres.h:352
static Datum TransactionIdGetDatum(TransactionId X)
Definition postgres.h:302
uint64_t Datum
Definition postgres.h:70
static int fb(int x)
char * format_procedure(Oid procedure_oid)
Definition regproc.c:305
ResourceOwner CurrentResourceOwner
Definition resowner.c:173
void ReplicationSlotAcquire(const char *name, bool nowait, bool error_if_invalid)
Definition slot.c:620
void ReplicationSlotMarkDirty(void)
Definition slot.c:1173
ReplicationSlot * MyReplicationSlot
Definition slot.c:148
void CheckSlotPermissions(void)
Definition slot.c:1676
void ReplicationSlotRelease(void)
Definition slot.c:758
void WaitForStandbyConfirmation(XLogRecPtr wait_for_lsn)
Definition slot.c:3231
void resetStringInfo(StringInfo str)
Definition stringinfo.c:126
Tuplestorestate * tupstore
Oid fn_oid
Definition fmgr.h:59
FmgrInfo * flinfo
Definition fmgr.h:87
Definition pg_list.h:54
OutputPluginOptions options
Definition logical.h:54
XLogReaderState * reader
Definition logical.h:42
void * output_writer_private
Definition logical.h:81
Definition nodes.h:135
OutputPluginOutputType output_type
ReplicationSlotPersistentData data
Definition slot.h:210
XLogRecPtr EndRecPtr
Definition xlogreader.h:206
Definition c.h:770
Definition c.h:716
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, const Datum *values, const bool *isnull)
Definition tuplestore.c:784
String * makeString(char *str)
Definition value.c:63
static Size VARSIZE_ANY_EXHDR(const void *PTR)
Definition varatt.h:472
static char * VARDATA_ANY(const void *PTR)
Definition varatt.h:486
text * cstring_to_text_with_len(const char *s, int len)
Definition varlena.c:193
char * text_to_cstring(const text *t)
Definition varlena.c:214
const char * name
bool RecoveryInProgress(void)
Definition xlog.c:6461
XLogRecPtr GetFlushRecPtr(TimeLineID *insertTLI)
Definition xlog.c:6626
#define XLogRecPtrIsValid(r)
Definition xlogdefs.h:29
uint64 XLogRecPtr
Definition xlogdefs.h:21
#define InvalidXLogRecPtr
Definition xlogdefs.h:28
XLogRecord * XLogReadRecord(XLogReaderState *state, char **errormsg)
Definition xlogreader.c:390
void XLogBeginRead(XLogReaderState *state, XLogRecPtr RecPtr)
Definition xlogreader.c:232
#define XL_ROUTINE(...)
Definition xlogreader.h:117
XLogRecPtr GetXLogReplayRecPtr(TimeLineID *replayTLI)
void wal_segment_close(XLogReaderState *state)
Definition xlogutils.c:831
void wal_segment_open(XLogReaderState *state, XLogSegNo nextSegNo, TimeLineID *tli_p)
Definition xlogutils.c:806
int read_local_xlog_page(XLogReaderState *state, XLogRecPtr targetPagePtr, int reqLen, XLogRecPtr targetRecPtr, char *cur_page)
Definition xlogutils.c:845