PostgreSQL Source Code git master
pgoutput.c File Reference
Include dependency graph for pgoutput.c:

Go to the source code of this file.

Data Structures

struct  RelationSyncEntry
 
struct  PGOutputTxnData
 

Macros

#define NUM_ROWFILTER_PUBACTIONS   (PUBACTION_DELETE+1)
 

Typedefs

typedef struct RelationSyncEntry RelationSyncEntry
 
typedef struct PGOutputTxnData PGOutputTxnData
 

Enumerations

enum  RowFilterPubAction { PUBACTION_INSERT , PUBACTION_UPDATE , PUBACTION_DELETE }
 

Functions

 PG_MODULE_MAGIC_EXT (.name="pgoutput",.version=PG_VERSION)
 
static void pgoutput_startup (LogicalDecodingContext *ctx, OutputPluginOptions *opt, bool is_init)
 
static void pgoutput_shutdown (LogicalDecodingContext *ctx)
 
static void pgoutput_begin_txn (LogicalDecodingContext *ctx, ReorderBufferTXN *txn)
 
static void pgoutput_commit_txn (LogicalDecodingContext *ctx, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
 
static void pgoutput_change (LogicalDecodingContext *ctx, ReorderBufferTXN *txn, Relation relation, ReorderBufferChange *change)
 
static void pgoutput_truncate (LogicalDecodingContext *ctx, ReorderBufferTXN *txn, int nrelations, Relation relations[], ReorderBufferChange *change)
 
static void pgoutput_message (LogicalDecodingContext *ctx, ReorderBufferTXN *txn, XLogRecPtr message_lsn, bool transactional, const char *prefix, Size sz, const char *message)
 
static bool pgoutput_origin_filter (LogicalDecodingContext *ctx, RepOriginId origin_id)
 
static void pgoutput_begin_prepare_txn (LogicalDecodingContext *ctx, ReorderBufferTXN *txn)
 
static void pgoutput_prepare_txn (LogicalDecodingContext *ctx, ReorderBufferTXN *txn, XLogRecPtr prepare_lsn)
 
static void pgoutput_commit_prepared_txn (LogicalDecodingContext *ctx, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
 
static void pgoutput_rollback_prepared_txn (LogicalDecodingContext *ctx, ReorderBufferTXN *txn, XLogRecPtr prepare_end_lsn, TimestampTz prepare_time)
 
static void pgoutput_stream_start (struct LogicalDecodingContext *ctx, ReorderBufferTXN *txn)
 
static void pgoutput_stream_stop (struct LogicalDecodingContext *ctx, ReorderBufferTXN *txn)
 
static void pgoutput_stream_abort (struct LogicalDecodingContext *ctx, ReorderBufferTXN *txn, XLogRecPtr abort_lsn)
 
static void pgoutput_stream_commit (struct LogicalDecodingContext *ctx, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
 
static void pgoutput_stream_prepare_txn (LogicalDecodingContext *ctx, ReorderBufferTXN *txn, XLogRecPtr prepare_lsn)
 
static ListLoadPublications (List *pubnames)
 
static void publication_invalidation_cb (Datum arg, int cacheid, uint32 hashvalue)
 
static void send_repl_origin (LogicalDecodingContext *ctx, RepOriginId origin_id, XLogRecPtr origin_lsn, bool send_origin)
 
static void init_rel_sync_cache (MemoryContext cachectx)
 
static void cleanup_rel_sync_cache (TransactionId xid, bool is_commit)
 
static RelationSyncEntryget_rel_sync_entry (PGOutputData *data, Relation relation)
 
static void send_relation_and_attrs (Relation relation, TransactionId xid, LogicalDecodingContext *ctx, RelationSyncEntry *relentry)
 
static void rel_sync_cache_relation_cb (Datum arg, Oid relid)
 
static void rel_sync_cache_publication_cb (Datum arg, int cacheid, uint32 hashvalue)
 
static void set_schema_sent_in_streamed_txn (RelationSyncEntry *entry, TransactionId xid)
 
static bool get_schema_sent_in_streamed_txn (RelationSyncEntry *entry, TransactionId xid)
 
static void init_tuple_slot (PGOutputData *data, Relation relation, RelationSyncEntry *entry)
 
static void pgoutput_memory_context_reset (void *arg)
 
static EStatecreate_estate_for_relation (Relation rel)
 
static void pgoutput_row_filter_init (PGOutputData *data, List *publications, RelationSyncEntry *entry)
 
static bool pgoutput_row_filter_exec_expr (ExprState *state, ExprContext *econtext)
 
static bool pgoutput_row_filter (Relation relation, TupleTableSlot *old_slot, TupleTableSlot **new_slot_ptr, RelationSyncEntry *entry, ReorderBufferChangeType *action)
 
static void pgoutput_column_list_init (PGOutputData *data, List *publications, RelationSyncEntry *entry)
 
void _PG_output_plugin_init (OutputPluginCallbacks *cb)
 
static void parse_output_parameters (List *options, PGOutputData *data)
 
static void pgoutput_send_begin (LogicalDecodingContext *ctx, ReorderBufferTXN *txn)
 
static void maybe_send_schema (LogicalDecodingContext *ctx, ReorderBufferChange *change, Relation relation, RelationSyncEntry *relentry)
 
static void pgoutput_ensure_entry_cxt (PGOutputData *data, RelationSyncEntry *entry)
 
static void check_and_init_gencol (PGOutputData *data, List *publications, RelationSyncEntry *entry)
 

Variables

static bool publications_valid
 
static HTABRelationSyncCache = NULL
 

Macro Definition Documentation

◆ NUM_ROWFILTER_PUBACTIONS

#define NUM_ROWFILTER_PUBACTIONS   (PUBACTION_DELETE+1)

Definition at line 106 of file pgoutput.c.

Typedef Documentation

◆ PGOutputTxnData

◆ RelationSyncEntry

Enumeration Type Documentation

◆ RowFilterPubAction

Enumerator
PUBACTION_INSERT 
PUBACTION_UPDATE 
PUBACTION_DELETE 

Definition at line 99 of file pgoutput.c.

100{
104};
@ PUBACTION_INSERT
Definition: pgoutput.c:101
@ PUBACTION_UPDATE
Definition: pgoutput.c:102
@ PUBACTION_DELETE
Definition: pgoutput.c:103

Function Documentation

◆ _PG_output_plugin_init()

void _PG_output_plugin_init ( OutputPluginCallbacks cb)

Definition at line 261 of file pgoutput.c.

262{
269
276
277 /* transaction streaming */
285 /* transaction streaming - two-phase commit */
287}
static void pgoutput_change(LogicalDecodingContext *ctx, ReorderBufferTXN *txn, Relation relation, ReorderBufferChange *change)
Definition: pgoutput.c:1484
static void pgoutput_begin_prepare_txn(LogicalDecodingContext *ctx, ReorderBufferTXN *txn)
Definition: pgoutput.c:664
static void pgoutput_startup(LogicalDecodingContext *ctx, OutputPluginOptions *opt, bool is_init)
Definition: pgoutput.c:451
static void pgoutput_truncate(LogicalDecodingContext *ctx, ReorderBufferTXN *txn, int nrelations, Relation relations[], ReorderBufferChange *change)
Definition: pgoutput.c:1656
static void pgoutput_prepare_txn(LogicalDecodingContext *ctx, ReorderBufferTXN *txn, XLogRecPtr prepare_lsn)
Definition: pgoutput.c:681
static bool pgoutput_origin_filter(LogicalDecodingContext *ctx, RepOriginId origin_id)
Definition: pgoutput.c:1769
static void pgoutput_rollback_prepared_txn(LogicalDecodingContext *ctx, ReorderBufferTXN *txn, XLogRecPtr prepare_end_lsn, TimestampTz prepare_time)
Definition: pgoutput.c:709
static void pgoutput_shutdown(LogicalDecodingContext *ctx)
Definition: pgoutput.c:1788
static void pgoutput_stream_abort(struct LogicalDecodingContext *ctx, ReorderBufferTXN *txn, XLogRecPtr abort_lsn)
Definition: pgoutput.c:1893
static void pgoutput_stream_prepare_txn(LogicalDecodingContext *ctx, ReorderBufferTXN *txn, XLogRecPtr prepare_lsn)
Definition: pgoutput.c:1954
static void pgoutput_begin_txn(LogicalDecodingContext *ctx, ReorderBufferTXN *txn)
Definition: pgoutput.c:596
static void pgoutput_stream_commit(struct LogicalDecodingContext *ctx, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: pgoutput.c:1926
static void pgoutput_commit_prepared_txn(LogicalDecodingContext *ctx, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: pgoutput.c:695
static void pgoutput_stream_stop(struct LogicalDecodingContext *ctx, ReorderBufferTXN *txn)
Definition: pgoutput.c:1872
static void pgoutput_stream_start(struct LogicalDecodingContext *ctx, ReorderBufferTXN *txn)
Definition: pgoutput.c:1840
static void pgoutput_message(LogicalDecodingContext *ctx, ReorderBufferTXN *txn, XLogRecPtr message_lsn, bool transactional, const char *prefix, Size sz, const char *message)
Definition: pgoutput.c:1724
static void pgoutput_commit_txn(LogicalDecodingContext *ctx, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: pgoutput.c:632
LogicalDecodeStreamChangeCB stream_change_cb
LogicalDecodeMessageCB message_cb
LogicalDecodeStreamTruncateCB stream_truncate_cb
LogicalDecodeStreamMessageCB stream_message_cb
LogicalDecodeFilterByOriginCB filter_by_origin_cb
LogicalDecodeTruncateCB truncate_cb
LogicalDecodeStreamStopCB stream_stop_cb
LogicalDecodeStreamCommitCB stream_commit_cb
LogicalDecodeRollbackPreparedCB rollback_prepared_cb
LogicalDecodeStreamPrepareCB stream_prepare_cb
LogicalDecodeCommitPreparedCB commit_prepared_cb
LogicalDecodeStreamStartCB stream_start_cb
LogicalDecodePrepareCB prepare_cb
LogicalDecodeStartupCB startup_cb
LogicalDecodeCommitCB commit_cb
LogicalDecodeBeginCB begin_cb
LogicalDecodeStreamAbortCB stream_abort_cb
LogicalDecodeBeginPrepareCB begin_prepare_cb
LogicalDecodeChangeCB change_cb
LogicalDecodeShutdownCB shutdown_cb

References OutputPluginCallbacks::begin_cb, OutputPluginCallbacks::begin_prepare_cb, OutputPluginCallbacks::change_cb, OutputPluginCallbacks::commit_cb, OutputPluginCallbacks::commit_prepared_cb, OutputPluginCallbacks::filter_by_origin_cb, OutputPluginCallbacks::message_cb, pgoutput_begin_prepare_txn(), pgoutput_begin_txn(), pgoutput_change(), pgoutput_commit_prepared_txn(), pgoutput_commit_txn(), pgoutput_message(), pgoutput_origin_filter(), pgoutput_prepare_txn(), pgoutput_rollback_prepared_txn(), pgoutput_shutdown(), pgoutput_startup(), pgoutput_stream_abort(), pgoutput_stream_commit(), pgoutput_stream_prepare_txn(), pgoutput_stream_start(), pgoutput_stream_stop(), pgoutput_truncate(), OutputPluginCallbacks::prepare_cb, OutputPluginCallbacks::rollback_prepared_cb, OutputPluginCallbacks::shutdown_cb, OutputPluginCallbacks::startup_cb, OutputPluginCallbacks::stream_abort_cb, OutputPluginCallbacks::stream_change_cb, OutputPluginCallbacks::stream_commit_cb, OutputPluginCallbacks::stream_message_cb, OutputPluginCallbacks::stream_prepare_cb, OutputPluginCallbacks::stream_start_cb, OutputPluginCallbacks::stream_stop_cb, OutputPluginCallbacks::stream_truncate_cb, and OutputPluginCallbacks::truncate_cb.

◆ check_and_init_gencol()

static void check_and_init_gencol ( PGOutputData data,
List publications,
RelationSyncEntry entry 
)
static

Definition at line 1065 of file pgoutput.c.

1067{
1069 TupleDesc desc = RelationGetDescr(relation);
1070 bool gencolpresent = false;
1071 bool first = true;
1072
1073 /* Check if there is any generated column present. */
1074 for (int i = 0; i < desc->natts; i++)
1075 {
1077
1078 if (att->attgenerated)
1079 {
1080 gencolpresent = true;
1081 break;
1082 }
1083 }
1084
1085 /* There are no generated columns to be published. */
1086 if (!gencolpresent)
1087 {
1088 entry->include_gencols_type = PUBLISH_GENCOLS_NONE;
1089 return;
1090 }
1091
1092 /*
1093 * There may be a conflicting value for 'publish_generated_columns'
1094 * parameter in the publications.
1095 */
1096 foreach_ptr(Publication, pub, publications)
1097 {
1098 /*
1099 * The column list takes precedence over the
1100 * 'publish_generated_columns' parameter. Those will be checked later,
1101 * see pgoutput_column_list_init.
1102 */
1103 if (check_and_fetch_column_list(pub, entry->publish_as_relid, NULL, NULL))
1104 continue;
1105
1106 if (first)
1107 {
1108 entry->include_gencols_type = pub->pubgencols_type;
1109 first = false;
1110 }
1111 else if (entry->include_gencols_type != pub->pubgencols_type)
1112 ereport(ERROR,
1113 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1114 errmsg("cannot use different values of publish_generated_columns for table \"%s.%s\" in different publications",
1116 RelationGetRelationName(relation)));
1117 }
1118}
int errcode(int sqlerrcode)
Definition: elog.c:863
int errmsg(const char *fmt,...)
Definition: elog.c:1080
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:150
int i
Definition: isn.c:77
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3516
#define foreach_ptr(type, var, lst)
Definition: pg_list.h:469
bool check_and_fetch_column_list(Publication *pub, Oid relid, MemoryContext mcxt, Bitmapset **cols)
#define RelationGetDescr(relation)
Definition: rel.h:541
#define RelationGetRelationName(relation)
Definition: rel.h:549
#define RelationGetNamespace(relation)
Definition: rel.h:556
Relation RelationIdGetRelation(Oid relationId)
Definition: relcache.c:2094
bool attgenerated
Definition: tupdesc.h:78
PublishGencolsType include_gencols_type
Definition: pgoutput.c:141
static CompactAttribute * TupleDescCompactAttr(TupleDesc tupdesc, int i)
Definition: tupdesc.h:175

References CompactAttribute::attgenerated, check_and_fetch_column_list(), ereport, errcode(), errmsg(), ERROR, foreach_ptr, get_namespace_name(), i, RelationSyncEntry::include_gencols_type, TupleDescData::natts, RelationSyncEntry::publish_as_relid, RelationGetDescr, RelationGetNamespace, RelationGetRelationName, RelationIdGetRelation(), and TupleDescCompactAttr().

Referenced by get_rel_sync_entry().

◆ cleanup_rel_sync_cache()

static void cleanup_rel_sync_cache ( TransactionId  xid,
bool  is_commit 
)
static

Definition at line 2349 of file pgoutput.c.

2350{
2351 HASH_SEQ_STATUS hash_seq;
2352 RelationSyncEntry *entry;
2353
2354 Assert(RelationSyncCache != NULL);
2355
2356 hash_seq_init(&hash_seq, RelationSyncCache);
2357 while ((entry = hash_seq_search(&hash_seq)) != NULL)
2358 {
2359 /*
2360 * We can set the schema_sent flag for an entry that has committed xid
2361 * in the list as that ensures that the subscriber would have the
2362 * corresponding schema and we don't need to send it unless there is
2363 * any invalidation for that relation.
2364 */
2365 foreach_xid(streamed_txn, entry->streamed_txns)
2366 {
2367 if (xid == streamed_txn)
2368 {
2369 if (is_commit)
2370 entry->schema_sent = true;
2371
2372 entry->streamed_txns =
2373 foreach_delete_current(entry->streamed_txns, streamed_txn);
2374 break;
2375 }
2376 }
2377 }
2378}
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1415
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1380
Assert(PointerIsAligned(start, uint64))
#define foreach_delete_current(lst, var_or_cell)
Definition: pg_list.h:391
#define foreach_xid(var, lst)
Definition: pg_list.h:472
static HTAB * RelationSyncCache
Definition: pgoutput.c:220
List * streamed_txns
Definition: pgoutput.c:142

References Assert(), foreach_delete_current, foreach_xid, hash_seq_init(), hash_seq_search(), RelationSyncCache, RelationSyncEntry::schema_sent, and RelationSyncEntry::streamed_txns.

Referenced by pgoutput_stream_abort(), and pgoutput_stream_commit().

◆ create_estate_for_relation()

static EState * create_estate_for_relation ( Relation  rel)
static

Definition at line 842 of file pgoutput.c.

843{
844 EState *estate;
845 RangeTblEntry *rte;
846 List *perminfos = NIL;
847
848 estate = CreateExecutorState();
849
850 rte = makeNode(RangeTblEntry);
851 rte->rtekind = RTE_RELATION;
852 rte->relid = RelationGetRelid(rel);
853 rte->relkind = rel->rd_rel->relkind;
854 rte->rellockmode = AccessShareLock;
855
856 addRTEPermissionInfo(&perminfos, rte);
857
858 ExecInitRangeTable(estate, list_make1(rte), perminfos,
860
861 estate->es_output_cid = GetCurrentCommandId(false);
862
863 return estate;
864}
Bitmapset * bms_make_singleton(int x)
Definition: bitmapset.c:216
void ExecInitRangeTable(EState *estate, List *rangeTable, List *permInfos, Bitmapset *unpruned_relids)
Definition: execUtils.c:773
EState * CreateExecutorState(void)
Definition: execUtils.c:88
#define AccessShareLock
Definition: lockdefs.h:36
#define makeNode(_type_)
Definition: nodes.h:161
RTEPermissionInfo * addRTEPermissionInfo(List **rteperminfos, RangeTblEntry *rte)
@ RTE_RELATION
Definition: parsenodes.h:1070
#define NIL
Definition: pg_list.h:68
#define list_make1(x1)
Definition: pg_list.h:212
#define RelationGetRelid(relation)
Definition: rel.h:515
CommandId es_output_cid
Definition: execnodes.h:682
Definition: pg_list.h:54
RTEKind rtekind
Definition: parsenodes.h:1105
Form_pg_class rd_rel
Definition: rel.h:111
CommandId GetCurrentCommandId(bool used)
Definition: xact.c:830

References AccessShareLock, addRTEPermissionInfo(), bms_make_singleton(), CreateExecutorState(), EState::es_output_cid, ExecInitRangeTable(), GetCurrentCommandId(), list_make1, makeNode, NIL, RelationData::rd_rel, RelationGetRelid, RTE_RELATION, and RangeTblEntry::rtekind.

Referenced by pgoutput_row_filter_init().

◆ get_rel_sync_entry()

static RelationSyncEntry * get_rel_sync_entry ( PGOutputData data,
Relation  relation 
)
static

Definition at line 2054 of file pgoutput.c.

2055{
2056 RelationSyncEntry *entry;
2057 bool found;
2058 MemoryContext oldctx;
2059 Oid relid = RelationGetRelid(relation);
2060
2061 Assert(RelationSyncCache != NULL);
2062
2063 /* Find cached relation info, creating if not found */
2065 &relid,
2066 HASH_ENTER, &found);
2067 Assert(entry != NULL);
2068
2069 /* initialize entry, if it's new */
2070 if (!found)
2071 {
2072 entry->replicate_valid = false;
2073 entry->schema_sent = false;
2074 entry->include_gencols_type = PUBLISH_GENCOLS_NONE;
2075 entry->streamed_txns = NIL;
2076 entry->pubactions.pubinsert = entry->pubactions.pubupdate =
2077 entry->pubactions.pubdelete = entry->pubactions.pubtruncate = false;
2078 entry->new_slot = NULL;
2079 entry->old_slot = NULL;
2080 memset(entry->exprstate, 0, sizeof(entry->exprstate));
2081 entry->entry_cxt = NULL;
2083 entry->columns = NULL;
2084 entry->attrmap = NULL;
2085 }
2086
2087 /* Validate the entry */
2088 if (!entry->replicate_valid)
2089 {
2090 Oid schemaId = get_rel_namespace(relid);
2091 List *pubids = GetRelationPublications(relid);
2092
2093 /*
2094 * We don't acquire a lock on the namespace system table as we build
2095 * the cache entry using a historic snapshot and all the later changes
2096 * are absorbed while decoding WAL.
2097 */
2098 List *schemaPubids = GetSchemaPublications(schemaId);
2099 ListCell *lc;
2100 Oid publish_as_relid = relid;
2101 int publish_ancestor_level = 0;
2102 bool am_partition = get_rel_relispartition(relid);
2103 char relkind = get_rel_relkind(relid);
2104 List *rel_publications = NIL;
2105
2106 /* Reload publications if needed before use. */
2107 if (!publications_valid)
2108 {
2109 MemoryContextReset(data->pubctx);
2110
2111 oldctx = MemoryContextSwitchTo(data->pubctx);
2112 data->publications = LoadPublications(data->publication_names);
2113 MemoryContextSwitchTo(oldctx);
2114 publications_valid = true;
2115 }
2116
2117 /*
2118 * Reset schema_sent status as the relation definition may have
2119 * changed. Also reset pubactions to empty in case rel was dropped
2120 * from a publication. Also free any objects that depended on the
2121 * earlier definition.
2122 */
2123 entry->schema_sent = false;
2124 entry->include_gencols_type = PUBLISH_GENCOLS_NONE;
2125 list_free(entry->streamed_txns);
2126 entry->streamed_txns = NIL;
2127 bms_free(entry->columns);
2128 entry->columns = NULL;
2129 entry->pubactions.pubinsert = false;
2130 entry->pubactions.pubupdate = false;
2131 entry->pubactions.pubdelete = false;
2132 entry->pubactions.pubtruncate = false;
2133
2134 /*
2135 * Tuple slots cleanups. (Will be rebuilt later if needed).
2136 */
2137 if (entry->old_slot)
2138 {
2139 TupleDesc desc = entry->old_slot->tts_tupleDescriptor;
2140
2141 Assert(desc->tdrefcount == -1);
2142
2144
2145 /*
2146 * ExecDropSingleTupleTableSlot() would not free the TupleDesc, so
2147 * do it now to avoid any leaks.
2148 */
2149 FreeTupleDesc(desc);
2150 }
2151 if (entry->new_slot)
2152 {
2153 TupleDesc desc = entry->new_slot->tts_tupleDescriptor;
2154
2155 Assert(desc->tdrefcount == -1);
2156
2158
2159 /*
2160 * ExecDropSingleTupleTableSlot() would not free the TupleDesc, so
2161 * do it now to avoid any leaks.
2162 */
2163 FreeTupleDesc(desc);
2164 }
2165
2166 entry->old_slot = NULL;
2167 entry->new_slot = NULL;
2168
2169 if (entry->attrmap)
2170 free_attrmap(entry->attrmap);
2171 entry->attrmap = NULL;
2172
2173 /*
2174 * Row filter cache cleanups.
2175 */
2176 if (entry->entry_cxt)
2178
2179 entry->entry_cxt = NULL;
2180 entry->estate = NULL;
2181 memset(entry->exprstate, 0, sizeof(entry->exprstate));
2182
2183 /*
2184 * Build publication cache. We can't use one provided by relcache as
2185 * relcache considers all publications that the given relation is in,
2186 * but here we only need to consider ones that the subscriber
2187 * requested.
2188 */
2189 foreach(lc, data->publications)
2190 {
2191 Publication *pub = lfirst(lc);
2192 bool publish = false;
2193
2194 /*
2195 * Under what relid should we publish changes in this publication?
2196 * We'll use the top-most relid across all publications. Also
2197 * track the ancestor level for this publication.
2198 */
2199 Oid pub_relid = relid;
2200 int ancestor_level = 0;
2201
2202 /*
2203 * If this is a FOR ALL TABLES publication, pick the partition
2204 * root and set the ancestor level accordingly.
2205 */
2206 if (pub->alltables)
2207 {
2208 publish = true;
2209 if (pub->pubviaroot && am_partition)
2210 {
2211 List *ancestors = get_partition_ancestors(relid);
2212
2213 pub_relid = llast_oid(ancestors);
2214 ancestor_level = list_length(ancestors);
2215 }
2216 }
2217
2218 if (!publish)
2219 {
2220 bool ancestor_published = false;
2221
2222 /*
2223 * For a partition, check if any of the ancestors are
2224 * published. If so, note down the topmost ancestor that is
2225 * published via this publication, which will be used as the
2226 * relation via which to publish the partition's changes.
2227 */
2228 if (am_partition)
2229 {
2230 Oid ancestor;
2231 int level;
2232 List *ancestors = get_partition_ancestors(relid);
2233
2234 ancestor = GetTopMostAncestorInPublication(pub->oid,
2235 ancestors,
2236 &level);
2237
2238 if (ancestor != InvalidOid)
2239 {
2240 ancestor_published = true;
2241 if (pub->pubviaroot)
2242 {
2243 pub_relid = ancestor;
2244 ancestor_level = level;
2245 }
2246 }
2247 }
2248
2249 if (list_member_oid(pubids, pub->oid) ||
2250 list_member_oid(schemaPubids, pub->oid) ||
2251 ancestor_published)
2252 publish = true;
2253 }
2254
2255 /*
2256 * If the relation is to be published, determine actions to
2257 * publish, and list of columns, if appropriate.
2258 *
2259 * Don't publish changes for partitioned tables, because
2260 * publishing those of its partitions suffices, unless partition
2261 * changes won't be published due to pubviaroot being set.
2262 */
2263 if (publish &&
2264 (relkind != RELKIND_PARTITIONED_TABLE || pub->pubviaroot))
2265 {
2270
2271 /*
2272 * We want to publish the changes as the top-most ancestor
2273 * across all publications. So we need to check if the already
2274 * calculated level is higher than the new one. If yes, we can
2275 * ignore the new value (as it's a child). Otherwise the new
2276 * value is an ancestor, so we keep it.
2277 */
2278 if (publish_ancestor_level > ancestor_level)
2279 continue;
2280
2281 /*
2282 * If we found an ancestor higher up in the tree, discard the
2283 * list of publications through which we replicate it, and use
2284 * the new ancestor.
2285 */
2286 if (publish_ancestor_level < ancestor_level)
2287 {
2288 publish_as_relid = pub_relid;
2289 publish_ancestor_level = ancestor_level;
2290
2291 /* reset the publication list for this relation */
2292 rel_publications = NIL;
2293 }
2294 else
2295 {
2296 /* Same ancestor level, has to be the same OID. */
2297 Assert(publish_as_relid == pub_relid);
2298 }
2299
2300 /* Track publications for this ancestor. */
2301 rel_publications = lappend(rel_publications, pub);
2302 }
2303 }
2304
2305 entry->publish_as_relid = publish_as_relid;
2306
2307 /*
2308 * Initialize the tuple slot, map, and row filter. These are only used
2309 * when publishing inserts, updates, or deletes.
2310 */
2311 if (entry->pubactions.pubinsert || entry->pubactions.pubupdate ||
2312 entry->pubactions.pubdelete)
2313 {
2314 /* Initialize the tuple slot and map */
2315 init_tuple_slot(data, relation, entry);
2316
2317 /* Initialize the row filter */
2318 pgoutput_row_filter_init(data, rel_publications, entry);
2319
2320 /* Check whether to publish generated columns. */
2321 check_and_init_gencol(data, rel_publications, entry);
2322
2323 /* Initialize the column list */
2324 pgoutput_column_list_init(data, rel_publications, entry);
2325 }
2326
2327 list_free(pubids);
2328 list_free(schemaPubids);
2329 list_free(rel_publications);
2330
2331 entry->replicate_valid = true;
2332 }
2333
2334 return entry;
2335}
void free_attrmap(AttrMap *map)
Definition: attmap.c:56
void bms_free(Bitmapset *a)
Definition: bitmapset.c:239
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:952
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
Definition: execTuples.c:1443
@ HASH_ENTER
Definition: hsearch.h:114
List * lappend(List *list, void *datum)
Definition: list.c:339
void list_free(List *list)
Definition: list.c:1546
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:722
bool get_rel_relispartition(Oid relid)
Definition: lsyscache.c:2177
char get_rel_relkind(Oid relid)
Definition: lsyscache.c:2153
Oid get_rel_namespace(Oid relid)
Definition: lsyscache.c:2102
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:403
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:472
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
List * get_partition_ancestors(Oid relid)
Definition: partition.c:134
const void * data
#define lfirst(lc)
Definition: pg_list.h:172
static int list_length(const List *l)
Definition: pg_list.h:152
#define llast_oid(l)
Definition: pg_list.h:200
List * GetRelationPublications(Oid relid)
List * GetSchemaPublications(Oid schemaid)
Oid GetTopMostAncestorInPublication(Oid puboid, List *ancestors, int *ancestor_level)
static List * LoadPublications(List *pubnames)
Definition: pgoutput.c:1802
static void init_tuple_slot(PGOutputData *data, Relation relation, RelationSyncEntry *entry)
Definition: pgoutput.c:1211
static void pgoutput_row_filter_init(PGOutputData *data, List *publications, RelationSyncEntry *entry)
Definition: pgoutput.c:918
static void check_and_init_gencol(PGOutputData *data, List *publications, RelationSyncEntry *entry)
Definition: pgoutput.c:1065
static void pgoutput_column_list_init(PGOutputData *data, List *publications, RelationSyncEntry *entry)
Definition: pgoutput.c:1124
static bool publications_valid
Definition: pgoutput.c:86
#define InvalidOid
Definition: postgres_ext.h:37
unsigned int Oid
Definition: postgres_ext.h:32
PublicationActions pubactions
ExprState * exprstate[NUM_ROWFILTER_PUBACTIONS]
Definition: pgoutput.c:155
Bitmapset * columns
Definition: pgoutput.c:181
PublicationActions pubactions
Definition: pgoutput.c:146
TupleTableSlot * old_slot
Definition: pgoutput.c:158
bool replicate_valid
Definition: pgoutput.c:130
MemoryContext entry_cxt
Definition: pgoutput.c:187
EState * estate
Definition: pgoutput.c:156
TupleTableSlot * new_slot
Definition: pgoutput.c:157
AttrMap * attrmap
Definition: pgoutput.c:174
int tdrefcount
Definition: tupdesc.h:140
TupleDesc tts_tupleDescriptor
Definition: tuptable.h:122
void FreeTupleDesc(TupleDesc tupdesc)
Definition: tupdesc.c:502

References Publication::alltables, Assert(), RelationSyncEntry::attrmap, bms_free(), check_and_init_gencol(), RelationSyncEntry::columns, data, RelationSyncEntry::entry_cxt, RelationSyncEntry::estate, ExecDropSingleTupleTableSlot(), RelationSyncEntry::exprstate, free_attrmap(), FreeTupleDesc(), get_partition_ancestors(), get_rel_namespace(), get_rel_relispartition(), get_rel_relkind(), GetRelationPublications(), GetSchemaPublications(), GetTopMostAncestorInPublication(), HASH_ENTER, hash_search(), RelationSyncEntry::include_gencols_type, init_tuple_slot(), InvalidOid, lappend(), lfirst, list_free(), list_length(), list_member_oid(), llast_oid, LoadPublications(), MemoryContextDelete(), MemoryContextReset(), MemoryContextSwitchTo(), RelationSyncEntry::new_slot, NIL, Publication::oid, RelationSyncEntry::old_slot, pgoutput_column_list_init(), pgoutput_row_filter_init(), RelationSyncEntry::pubactions, Publication::pubactions, PublicationActions::pubdelete, PublicationActions::pubinsert, publications_valid, RelationSyncEntry::publish_as_relid, PublicationActions::pubtruncate, PublicationActions::pubupdate, Publication::pubviaroot, RelationGetRelid, RelationSyncCache, RelationSyncEntry::replicate_valid, RelationSyncEntry::schema_sent, RelationSyncEntry::streamed_txns, TupleDescData::tdrefcount, and TupleTableSlot::tts_tupleDescriptor.

Referenced by pgoutput_change(), and pgoutput_truncate().

◆ get_schema_sent_in_streamed_txn()

static bool get_schema_sent_in_streamed_txn ( RelationSyncEntry entry,
TransactionId  xid 
)
static

Definition at line 2023 of file pgoutput.c.

2024{
2025 return list_member_xid(entry->streamed_txns, xid);
2026}
bool list_member_xid(const List *list, TransactionId datum)
Definition: list.c:742

References list_member_xid(), and RelationSyncEntry::streamed_txns.

Referenced by maybe_send_schema().

◆ init_rel_sync_cache()

static void init_rel_sync_cache ( MemoryContext  cachectx)
static

Definition at line 1974 of file pgoutput.c.

1975{
1976 HASHCTL ctl;
1977 static bool relation_callbacks_registered = false;
1978
1979 /* Nothing to do if hash table already exists */
1980 if (RelationSyncCache != NULL)
1981 return;
1982
1983 /* Make a new hash table for the cache */
1984 ctl.keysize = sizeof(Oid);
1985 ctl.entrysize = sizeof(RelationSyncEntry);
1986 ctl.hcxt = cachectx;
1987
1988 RelationSyncCache = hash_create("logical replication output relation cache",
1989 128, &ctl,
1991
1992 Assert(RelationSyncCache != NULL);
1993
1994 /* No more to do if we already registered callbacks */
1995 if (relation_callbacks_registered)
1996 return;
1997
1998 /* We must update the cache entry for a relation after a relcache flush */
2000
2001 /*
2002 * Flush all cache entries after a pg_namespace change, in case it was a
2003 * schema rename affecting a relation being replicated.
2004 *
2005 * XXX: It is not a good idea to invalidate all the relation entries in
2006 * RelationSyncCache on schema rename. We can optimize it to invalidate
2007 * only the required relations by either having a specific invalidation
2008 * message containing impacted relations or by having schema information
2009 * in each RelationSyncCache entry and using hashvalue of pg_namespace.oid
2010 * passed to the callback.
2011 */
2012 CacheRegisterSyscacheCallback(NAMESPACEOID,
2014 (Datum) 0);
2015
2016 relation_callbacks_registered = true;
2017}
HTAB * hash_create(const char *tabname, int64 nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:358
#define HASH_CONTEXT
Definition: hsearch.h:102
#define HASH_ELEM
Definition: hsearch.h:95
#define HASH_BLOBS
Definition: hsearch.h:97
void CacheRegisterRelcacheCallback(RelcacheCallbackFunction func, Datum arg)
Definition: inval.c:1858
void CacheRegisterSyscacheCallback(int cacheid, SyscacheCallbackFunction func, Datum arg)
Definition: inval.c:1816
static void rel_sync_cache_publication_cb(Datum arg, int cacheid, uint32 hashvalue)
Definition: pgoutput.c:2434
struct RelationSyncEntry RelationSyncEntry
static void rel_sync_cache_relation_cb(Datum arg, Oid relid)
Definition: pgoutput.c:2384
uint64_t Datum
Definition: postgres.h:70
tree ctl
Definition: radixtree.h:1838

References Assert(), CacheRegisterRelcacheCallback(), CacheRegisterSyscacheCallback(), ctl, HASH_BLOBS, HASH_CONTEXT, hash_create(), HASH_ELEM, rel_sync_cache_publication_cb(), rel_sync_cache_relation_cb(), and RelationSyncCache.

Referenced by pgoutput_startup().

◆ init_tuple_slot()

static void init_tuple_slot ( PGOutputData data,
Relation  relation,
RelationSyncEntry entry 
)
static

Definition at line 1211 of file pgoutput.c.

1213{
1214 MemoryContext oldctx;
1215 TupleDesc oldtupdesc;
1216 TupleDesc newtupdesc;
1217
1218 oldctx = MemoryContextSwitchTo(data->cachectx);
1219
1220 /*
1221 * Create tuple table slots. Create a copy of the TupleDesc as it needs to
1222 * live as long as the cache remains.
1223 */
1224 oldtupdesc = CreateTupleDescCopyConstr(RelationGetDescr(relation));
1225 newtupdesc = CreateTupleDescCopyConstr(RelationGetDescr(relation));
1226
1227 entry->old_slot = MakeSingleTupleTableSlot(oldtupdesc, &TTSOpsHeapTuple);
1228 entry->new_slot = MakeSingleTupleTableSlot(newtupdesc, &TTSOpsHeapTuple);
1229
1230 MemoryContextSwitchTo(oldctx);
1231
1232 /*
1233 * Cache the map that will be used to convert the relation's tuples into
1234 * the ancestor's format, if needed.
1235 */
1236 if (entry->publish_as_relid != RelationGetRelid(relation))
1237 {
1239 TupleDesc indesc = RelationGetDescr(relation);
1240 TupleDesc outdesc = RelationGetDescr(ancestor);
1241
1242 /* Map must live as long as the logical decoding context. */
1243 oldctx = MemoryContextSwitchTo(data->cachectx);
1244
1245 entry->attrmap = build_attrmap_by_name_if_req(indesc, outdesc, false);
1246
1247 MemoryContextSwitchTo(oldctx);
1248 RelationClose(ancestor);
1249 }
1250}
AttrMap * build_attrmap_by_name_if_req(TupleDesc indesc, TupleDesc outdesc, bool missing_ok)
Definition: attmap.c:261
TupleTableSlot * MakeSingleTupleTableSlot(TupleDesc tupdesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1427
const TupleTableSlotOps TTSOpsHeapTuple
Definition: execTuples.c:85
void RelationClose(Relation relation)
Definition: relcache.c:2215
TupleDesc CreateTupleDescCopyConstr(TupleDesc tupdesc)
Definition: tupdesc.c:340

References RelationSyncEntry::attrmap, build_attrmap_by_name_if_req(), CreateTupleDescCopyConstr(), data, MakeSingleTupleTableSlot(), MemoryContextSwitchTo(), RelationSyncEntry::new_slot, RelationSyncEntry::old_slot, RelationSyncEntry::publish_as_relid, RelationClose(), RelationGetDescr, RelationGetRelid, RelationIdGetRelation(), and TTSOpsHeapTuple.

Referenced by get_rel_sync_entry().

◆ LoadPublications()

static List * LoadPublications ( List pubnames)
static

Definition at line 1802 of file pgoutput.c.

1803{
1804 List *result = NIL;
1805 ListCell *lc;
1806
1807 foreach(lc, pubnames)
1808 {
1809 char *pubname = (char *) lfirst(lc);
1810 Publication *pub = GetPublicationByName(pubname, true);
1811
1812 if (pub)
1813 result = lappend(result, pub);
1814 else
1816 errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1817 errmsg("skipped loading publication \"%s\"", pubname),
1818 errdetail("The publication does not exist at this point in the WAL."),
1819 errhint("Create the publication if it does not exist."));
1820 }
1821
1822 return result;
1823}
int errdetail(const char *fmt,...)
Definition: elog.c:1216
int errhint(const char *fmt,...)
Definition: elog.c:1330
#define WARNING
Definition: elog.h:36
Publication * GetPublicationByName(const char *pubname, bool missing_ok)

References ereport, errcode(), errdetail(), errhint(), errmsg(), GetPublicationByName(), lappend(), lfirst, NIL, and WARNING.

Referenced by get_rel_sync_entry().

◆ maybe_send_schema()

static void maybe_send_schema ( LogicalDecodingContext ctx,
ReorderBufferChange change,
Relation  relation,
RelationSyncEntry relentry 
)
static

Definition at line 727 of file pgoutput.c.

730{
732 bool schema_sent;
735
736 /*
737 * Remember XID of the (sub)transaction for the change. We don't care if
738 * it's top-level transaction or not (we have already sent that XID in
739 * start of the current streaming block).
740 *
741 * If we're not in a streaming block, just use InvalidTransactionId and
742 * the write methods will not include it.
743 */
744 if (data->in_streaming)
745 xid = change->txn->xid;
746
747 if (rbtxn_is_subtxn(change->txn))
748 topxid = rbtxn_get_toptxn(change->txn)->xid;
749 else
750 topxid = xid;
751
752 /*
753 * Do we need to send the schema? We do track streamed transactions
754 * separately, because those may be applied later (and the regular
755 * transactions won't see their effects until then) and in an order that
756 * we don't know at this point.
757 *
758 * XXX There is a scope of optimization here. Currently, we always send
759 * the schema first time in a streaming transaction but we can probably
760 * avoid that by checking 'relentry->schema_sent' flag. However, before
761 * doing that we need to study its impact on the case where we have a mix
762 * of streaming and non-streaming transactions.
763 */
764 if (data->in_streaming)
765 schema_sent = get_schema_sent_in_streamed_txn(relentry, topxid);
766 else
767 schema_sent = relentry->schema_sent;
768
769 /* Nothing to do if we already sent the schema. */
770 if (schema_sent)
771 return;
772
773 /*
774 * Send the schema. If the changes will be published using an ancestor's
775 * schema, not the relation's own, send that ancestor's schema before
776 * sending relation's own (XXX - maybe sending only the former suffices?).
777 */
778 if (relentry->publish_as_relid != RelationGetRelid(relation))
779 {
780 Relation ancestor = RelationIdGetRelation(relentry->publish_as_relid);
781
782 send_relation_and_attrs(ancestor, xid, ctx, relentry);
783 RelationClose(ancestor);
784 }
785
786 send_relation_and_attrs(relation, xid, ctx, relentry);
787
788 if (data->in_streaming)
789 set_schema_sent_in_streamed_txn(relentry, topxid);
790 else
791 relentry->schema_sent = true;
792}
uint32 TransactionId
Definition: c.h:672
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:81
static void send_relation_and_attrs(Relation relation, TransactionId xid, LogicalDecodingContext *ctx, RelationSyncEntry *relentry)
Definition: pgoutput.c:798
static void set_schema_sent_in_streamed_txn(RelationSyncEntry *entry, TransactionId xid)
Definition: pgoutput.c:2033
static bool get_schema_sent_in_streamed_txn(RelationSyncEntry *entry, TransactionId xid)
Definition: pgoutput.c:2023
#define rbtxn_get_toptxn(txn)
#define rbtxn_is_subtxn(txn)
void * output_plugin_private
Definition: logical.h:76
struct ReorderBufferTXN * txn
Definition: reorderbuffer.h:84
TransactionId xid
#define InvalidTransactionId
Definition: transam.h:31

References data, get_schema_sent_in_streamed_txn(), if(), InvalidTransactionId, LogicalDecodingContext::output_plugin_private, RelationSyncEntry::publish_as_relid, rbtxn_get_toptxn, rbtxn_is_subtxn, RelationClose(), RelationGetRelid, RelationIdGetRelation(), RelationSyncEntry::schema_sent, send_relation_and_attrs(), set_schema_sent_in_streamed_txn(), ReorderBufferChange::txn, and ReorderBufferTXN::xid.

Referenced by pgoutput_change(), and pgoutput_truncate().

◆ parse_output_parameters()

static void parse_output_parameters ( List options,
PGOutputData data 
)
static

Definition at line 290 of file pgoutput.c.

291{
292 ListCell *lc;
293 bool protocol_version_given = false;
294 bool publication_names_given = false;
295 bool binary_option_given = false;
296 bool messages_option_given = false;
297 bool streaming_given = false;
298 bool two_phase_option_given = false;
299 bool origin_option_given = false;
300
301 /* Initialize optional parameters to defaults */
302 data->binary = false;
303 data->streaming = LOGICALREP_STREAM_OFF;
304 data->messages = false;
305 data->two_phase = false;
306 data->publish_no_origin = false;
307
308 foreach(lc, options)
309 {
310 DefElem *defel = (DefElem *) lfirst(lc);
311
312 Assert(defel->arg == NULL || IsA(defel->arg, String));
313
314 /* Check each param, whether or not we recognize it */
315 if (strcmp(defel->defname, "proto_version") == 0)
316 {
317 unsigned long parsed;
318 char *endptr;
319
320 if (protocol_version_given)
322 (errcode(ERRCODE_SYNTAX_ERROR),
323 errmsg("conflicting or redundant options")));
324 protocol_version_given = true;
325
326 errno = 0;
327 parsed = strtoul(strVal(defel->arg), &endptr, 10);
328 if (errno != 0 || *endptr != '\0')
330 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
331 errmsg("invalid proto_version")));
332
333 if (parsed > PG_UINT32_MAX)
335 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
336 errmsg("proto_version \"%s\" out of range",
337 strVal(defel->arg))));
338
339 data->protocol_version = (uint32) parsed;
340 }
341 else if (strcmp(defel->defname, "publication_names") == 0)
342 {
343 if (publication_names_given)
345 (errcode(ERRCODE_SYNTAX_ERROR),
346 errmsg("conflicting or redundant options")));
347 publication_names_given = true;
348
349 /*
350 * Pass a copy of the DefElem->arg since SplitIdentifierString
351 * modifies its input.
352 */
353 if (!SplitIdentifierString(pstrdup(strVal(defel->arg)), ',',
354 &data->publication_names))
356 (errcode(ERRCODE_INVALID_NAME),
357 errmsg("invalid publication_names syntax")));
358 }
359 else if (strcmp(defel->defname, "binary") == 0)
360 {
361 if (binary_option_given)
363 (errcode(ERRCODE_SYNTAX_ERROR),
364 errmsg("conflicting or redundant options")));
365 binary_option_given = true;
366
367 data->binary = defGetBoolean(defel);
368 }
369 else if (strcmp(defel->defname, "messages") == 0)
370 {
371 if (messages_option_given)
373 (errcode(ERRCODE_SYNTAX_ERROR),
374 errmsg("conflicting or redundant options")));
375 messages_option_given = true;
376
377 data->messages = defGetBoolean(defel);
378 }
379 else if (strcmp(defel->defname, "streaming") == 0)
380 {
381 if (streaming_given)
383 (errcode(ERRCODE_SYNTAX_ERROR),
384 errmsg("conflicting or redundant options")));
385 streaming_given = true;
386
387 data->streaming = defGetStreamingMode(defel);
388 }
389 else if (strcmp(defel->defname, "two_phase") == 0)
390 {
391 if (two_phase_option_given)
393 (errcode(ERRCODE_SYNTAX_ERROR),
394 errmsg("conflicting or redundant options")));
395 two_phase_option_given = true;
396
397 data->two_phase = defGetBoolean(defel);
398 }
399 else if (strcmp(defel->defname, "origin") == 0)
400 {
401 char *origin;
402
403 if (origin_option_given)
405 errcode(ERRCODE_SYNTAX_ERROR),
406 errmsg("conflicting or redundant options"));
407 origin_option_given = true;
408
409 origin = defGetString(defel);
410 if (pg_strcasecmp(origin, LOGICALREP_ORIGIN_NONE) == 0)
411 data->publish_no_origin = true;
412 else if (pg_strcasecmp(origin, LOGICALREP_ORIGIN_ANY) == 0)
413 data->publish_no_origin = false;
414 else
416 errcode(ERRCODE_INVALID_PARAMETER_VALUE),
417 errmsg("unrecognized origin value: \"%s\"", origin));
418 }
419 else
420 elog(ERROR, "unrecognized pgoutput option: %s", defel->defname);
421 }
422
423 /* Check required options */
424 if (!protocol_version_given)
426 errcode(ERRCODE_INVALID_PARAMETER_VALUE),
427 errmsg("option \"%s\" missing", "proto_version"));
428 if (!publication_names_given)
430 errcode(ERRCODE_INVALID_PARAMETER_VALUE),
431 errmsg("option \"%s\" missing", "publication_names"));
432}
#define PG_UINT32_MAX
Definition: c.h:610
uint32_t uint32
Definition: c.h:552
char * defGetString(DefElem *def)
Definition: define.c:35
bool defGetBoolean(DefElem *def)
Definition: define.c:94
#define elog(elevel,...)
Definition: elog.h:226
char * pstrdup(const char *in)
Definition: mcxt.c:1781
#define IsA(nodeptr, _type_)
Definition: nodes.h:164
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:32
char * defname
Definition: parsenodes.h:844
Node * arg
Definition: parsenodes.h:845
Definition: value.h:64
char defGetStreamingMode(DefElem *def)
#define strVal(v)
Definition: value.h:82
bool SplitIdentifierString(char *rawstring, char separator, List **namelist)
Definition: varlena.c:2728

References DefElem::arg, Assert(), data, defGetBoolean(), defGetStreamingMode(), defGetString(), DefElem::defname, elog, ereport, errcode(), errmsg(), ERROR, IsA, lfirst, pg_strcasecmp(), PG_UINT32_MAX, pstrdup(), SplitIdentifierString(), and strVal.

Referenced by pgoutput_startup().

◆ PG_MODULE_MAGIC_EXT()

PG_MODULE_MAGIC_EXT ( name = "pgoutput",
version = PG_VERSION 
)

◆ pgoutput_begin_prepare_txn()

static void pgoutput_begin_prepare_txn ( LogicalDecodingContext ctx,
ReorderBufferTXN txn 
)
static

Definition at line 664 of file pgoutput.c.

665{
666 bool send_replication_origin = txn->origin_id != InvalidRepOriginId;
667
668 OutputPluginPrepareWrite(ctx, !send_replication_origin);
670
671 send_repl_origin(ctx, txn->origin_id, txn->origin_lsn,
672 send_replication_origin);
673
674 OutputPluginWrite(ctx, true);
675}
void OutputPluginWrite(struct LogicalDecodingContext *ctx, bool last_write)
Definition: logical.c:696
void OutputPluginPrepareWrite(struct LogicalDecodingContext *ctx, bool last_write)
Definition: logical.c:683
#define InvalidRepOriginId
Definition: origin.h:33
static void send_repl_origin(LogicalDecodingContext *ctx, RepOriginId origin_id, XLogRecPtr origin_lsn, bool send_origin)
Definition: pgoutput.c:2460
void logicalrep_write_begin_prepare(StringInfo out, ReorderBufferTXN *txn)
Definition: proto.c:116
StringInfo out
Definition: logical.h:71
RepOriginId origin_id
XLogRecPtr origin_lsn

References InvalidRepOriginId, logicalrep_write_begin_prepare(), ReorderBufferTXN::origin_id, ReorderBufferTXN::origin_lsn, LogicalDecodingContext::out, OutputPluginPrepareWrite(), OutputPluginWrite(), and send_repl_origin().

Referenced by _PG_output_plugin_init().

◆ pgoutput_begin_txn()

static void pgoutput_begin_txn ( LogicalDecodingContext ctx,
ReorderBufferTXN txn 
)
static

Definition at line 596 of file pgoutput.c.

597{
599 sizeof(PGOutputTxnData));
600
601 txn->output_plugin_private = txndata;
602}
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:1266
MemoryContext context
Definition: logical.h:36
void * output_plugin_private

References LogicalDecodingContext::context, MemoryContextAllocZero(), and ReorderBufferTXN::output_plugin_private.

Referenced by _PG_output_plugin_init().

◆ pgoutput_change()

static void pgoutput_change ( LogicalDecodingContext ctx,
ReorderBufferTXN txn,
Relation  relation,
ReorderBufferChange change 
)
static

Definition at line 1484 of file pgoutput.c.

1486{
1489 MemoryContext old;
1490 RelationSyncEntry *relentry;
1492 Relation ancestor = NULL;
1493 Relation targetrel = relation;
1495 TupleTableSlot *old_slot = NULL;
1496 TupleTableSlot *new_slot = NULL;
1497
1498 if (!is_publishable_relation(relation))
1499 return;
1500
1501 /*
1502 * Remember the xid for the change in streaming mode. We need to send xid
1503 * with each change in the streaming mode so that subscriber can make
1504 * their association and on aborts, it can discard the corresponding
1505 * changes.
1506 */
1507 if (data->in_streaming)
1508 xid = change->txn->xid;
1509
1510 relentry = get_rel_sync_entry(data, relation);
1511
1512 /* First check the table filter */
1513 switch (action)
1514 {
1516 if (!relentry->pubactions.pubinsert)
1517 return;
1518 break;
1520 if (!relentry->pubactions.pubupdate)
1521 return;
1522 break;
1524 if (!relentry->pubactions.pubdelete)
1525 return;
1526
1527 /*
1528 * This is only possible if deletes are allowed even when replica
1529 * identity is not defined for a table. Since the DELETE action
1530 * can't be published, we simply return.
1531 */
1532 if (!change->data.tp.oldtuple)
1533 {
1534 elog(DEBUG1, "didn't send DELETE change because of missing oldtuple");
1535 return;
1536 }
1537 break;
1538 default:
1539 Assert(false);
1540 }
1541
1542 /* Avoid leaking memory by using and resetting our own context */
1543 old = MemoryContextSwitchTo(data->context);
1544
1545 /* Switch relation if publishing via root. */
1546 if (relentry->publish_as_relid != RelationGetRelid(relation))
1547 {
1548 Assert(relation->rd_rel->relispartition);
1549 ancestor = RelationIdGetRelation(relentry->publish_as_relid);
1550 targetrel = ancestor;
1551 }
1552
1553 if (change->data.tp.oldtuple)
1554 {
1555 old_slot = relentry->old_slot;
1556 ExecStoreHeapTuple(change->data.tp.oldtuple, old_slot, false);
1557
1558 /* Convert tuple if needed. */
1559 if (relentry->attrmap)
1560 {
1562 &TTSOpsVirtual);
1563
1564 old_slot = execute_attr_map_slot(relentry->attrmap, old_slot, slot);
1565 }
1566 }
1567
1568 if (change->data.tp.newtuple)
1569 {
1570 new_slot = relentry->new_slot;
1571 ExecStoreHeapTuple(change->data.tp.newtuple, new_slot, false);
1572
1573 /* Convert tuple if needed. */
1574 if (relentry->attrmap)
1575 {
1577 &TTSOpsVirtual);
1578
1579 new_slot = execute_attr_map_slot(relentry->attrmap, new_slot, slot);
1580 }
1581 }
1582
1583 /*
1584 * Check row filter.
1585 *
1586 * Updates could be transformed to inserts or deletes based on the results
1587 * of the row filter for old and new tuple.
1588 */
1589 if (!pgoutput_row_filter(targetrel, old_slot, &new_slot, relentry, &action))
1590 goto cleanup;
1591
1592 /*
1593 * Send BEGIN if we haven't yet.
1594 *
1595 * We send the BEGIN message after ensuring that we will actually send the
1596 * change. This avoids sending a pair of BEGIN/COMMIT messages for empty
1597 * transactions.
1598 */
1599 if (txndata && !txndata->sent_begin_txn)
1600 pgoutput_send_begin(ctx, txn);
1601
1602 /*
1603 * Schema should be sent using the original relation because it also sends
1604 * the ancestor's relation.
1605 */
1606 maybe_send_schema(ctx, change, relation, relentry);
1607
1608 OutputPluginPrepareWrite(ctx, true);
1609
1610 /* Send the data */
1611 switch (action)
1612 {
1614 logicalrep_write_insert(ctx->out, xid, targetrel, new_slot,
1615 data->binary, relentry->columns,
1616 relentry->include_gencols_type);
1617 break;
1619 logicalrep_write_update(ctx->out, xid, targetrel, old_slot,
1620 new_slot, data->binary, relentry->columns,
1621 relentry->include_gencols_type);
1622 break;
1624 logicalrep_write_delete(ctx->out, xid, targetrel, old_slot,
1625 data->binary, relentry->columns,
1626 relentry->include_gencols_type);
1627 break;
1628 default:
1629 Assert(false);
1630 }
1631
1632 OutputPluginWrite(ctx, true);
1633
1634cleanup:
1635 if (RelationIsValid(ancestor))
1636 {
1637 RelationClose(ancestor);
1638 ancestor = NULL;
1639 }
1640
1641 /* Drop the new slots that were used to store the converted tuples. */
1642 if (relentry->attrmap)
1643 {
1644 if (old_slot)
1646
1647 if (new_slot)
1649 }
1650
1652 MemoryContextReset(data->context);
1653}
static void cleanup(void)
Definition: bootstrap.c:717
#define DEBUG1
Definition: elog.h:30
const TupleTableSlotOps TTSOpsVirtual
Definition: execTuples.c:84
TupleTableSlot * MakeTupleTableSlot(TupleDesc tupleDesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1301
TupleTableSlot * ExecStoreHeapTuple(HeapTuple tuple, TupleTableSlot *slot, bool shouldFree)
Definition: execTuples.c:1541
bool is_publishable_relation(Relation rel)
static void pgoutput_send_begin(LogicalDecodingContext *ctx, ReorderBufferTXN *txn)
Definition: pgoutput.c:610
static RelationSyncEntry * get_rel_sync_entry(PGOutputData *data, Relation relation)
Definition: pgoutput.c:2054
static void maybe_send_schema(LogicalDecodingContext *ctx, ReorderBufferChange *change, Relation relation, RelationSyncEntry *relentry)
Definition: pgoutput.c:727
static bool pgoutput_row_filter(Relation relation, TupleTableSlot *old_slot, TupleTableSlot **new_slot_ptr, RelationSyncEntry *entry, ReorderBufferChangeType *action)
Definition: pgoutput.c:1303
void logicalrep_write_insert(StringInfo out, TransactionId xid, Relation rel, TupleTableSlot *newslot, bool binary, Bitmapset *columns, PublishGencolsType include_gencols_type)
Definition: proto.c:403
void logicalrep_write_delete(StringInfo out, TransactionId xid, Relation rel, TupleTableSlot *oldslot, bool binary, Bitmapset *columns, PublishGencolsType include_gencols_type)
Definition: proto.c:528
void logicalrep_write_update(StringInfo out, TransactionId xid, Relation rel, TupleTableSlot *oldslot, TupleTableSlot *newslot, bool binary, Bitmapset *columns, PublishGencolsType include_gencols_type)
Definition: proto.c:450
#define RelationIsValid(relation)
Definition: rel.h:490
ReorderBufferChangeType
Definition: reorderbuffer.h:51
@ REORDER_BUFFER_CHANGE_INSERT
Definition: reorderbuffer.h:52
@ REORDER_BUFFER_CHANGE_DELETE
Definition: reorderbuffer.h:54
@ REORDER_BUFFER_CHANGE_UPDATE
Definition: reorderbuffer.h:53
ReorderBufferChangeType action
Definition: reorderbuffer.h:81
struct ReorderBufferChange::@114::@115 tp
union ReorderBufferChange::@114 data
TupleTableSlot * execute_attr_map_slot(AttrMap *attrMap, TupleTableSlot *in_slot, TupleTableSlot *out_slot)
Definition: tupconvert.c:193

References generate_unaccent_rules::action, ReorderBufferChange::action, Assert(), RelationSyncEntry::attrmap, cleanup(), RelationSyncEntry::columns, ReorderBufferChange::data, data, DEBUG1, elog, ExecDropSingleTupleTableSlot(), ExecStoreHeapTuple(), execute_attr_map_slot(), get_rel_sync_entry(), RelationSyncEntry::include_gencols_type, InvalidTransactionId, is_publishable_relation(), logicalrep_write_delete(), logicalrep_write_insert(), logicalrep_write_update(), MakeTupleTableSlot(), maybe_send_schema(), MemoryContextReset(), MemoryContextSwitchTo(), RelationSyncEntry::new_slot, ReorderBufferChange::newtuple, RelationSyncEntry::old_slot, ReorderBufferChange::oldtuple, LogicalDecodingContext::out, LogicalDecodingContext::output_plugin_private, ReorderBufferTXN::output_plugin_private, OutputPluginPrepareWrite(), OutputPluginWrite(), pgoutput_row_filter(), pgoutput_send_begin(), RelationSyncEntry::pubactions, PublicationActions::pubdelete, PublicationActions::pubinsert, RelationSyncEntry::publish_as_relid, PublicationActions::pubupdate, RelationData::rd_rel, RelationClose(), RelationGetDescr, RelationGetRelid, RelationIdGetRelation(), RelationIsValid, REORDER_BUFFER_CHANGE_DELETE, REORDER_BUFFER_CHANGE_INSERT, REORDER_BUFFER_CHANGE_UPDATE, ReorderBufferChange::tp, TTSOpsVirtual, ReorderBufferChange::txn, and ReorderBufferTXN::xid.

Referenced by _PG_output_plugin_init().

◆ pgoutput_column_list_init()

static void pgoutput_column_list_init ( PGOutputData data,
List publications,
RelationSyncEntry entry 
)
static

Definition at line 1124 of file pgoutput.c.

1126{
1127 ListCell *lc;
1128 bool first = true;
1130 bool found_pub_collist = false;
1131 Bitmapset *relcols = NULL;
1132
1134
1135 /*
1136 * Find if there are any column lists for this relation. If there are,
1137 * build a bitmap using the column lists.
1138 *
1139 * Multiple publications might have multiple column lists for this
1140 * relation.
1141 *
1142 * Note that we don't support the case where the column list is different
1143 * for the same table when combining publications. See comments atop
1144 * fetch_relation_list. But one can later change the publication so we
1145 * still need to check all the given publication-table mappings and report
1146 * an error if any publications have a different column list.
1147 */
1148 foreach(lc, publications)
1149 {
1150 Publication *pub = lfirst(lc);
1151 Bitmapset *cols = NULL;
1152
1153 /* Retrieve the bitmap of columns for a column list publication. */
1154 found_pub_collist |= check_and_fetch_column_list(pub,
1155 entry->publish_as_relid,
1156 entry->entry_cxt, &cols);
1157
1158 /*
1159 * For non-column list publications — e.g. TABLE (without a column
1160 * list), ALL TABLES, or ALL TABLES IN SCHEMA, we consider all columns
1161 * of the table (including generated columns when
1162 * 'publish_generated_columns' parameter is true).
1163 */
1164 if (!cols)
1165 {
1166 /*
1167 * Cache the table columns for the first publication with no
1168 * specified column list to detect publication with a different
1169 * column list.
1170 */
1171 if (!relcols && (list_length(publications) > 1))
1172 {
1174
1175 relcols = pub_form_cols_map(relation,
1176 entry->include_gencols_type);
1177 MemoryContextSwitchTo(oldcxt);
1178 }
1179
1180 cols = relcols;
1181 }
1182
1183 if (first)
1184 {
1185 entry->columns = cols;
1186 first = false;
1187 }
1188 else if (!bms_equal(entry->columns, cols))
1189 ereport(ERROR,
1190 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1191 errmsg("cannot use different column lists for table \"%s.%s\" in different publications",
1193 RelationGetRelationName(relation)));
1194 } /* loop all subscribed publications */
1195
1196 /*
1197 * If no column list publications exist, columns to be published will be
1198 * computed later according to the 'publish_generated_columns' parameter.
1199 */
1200 if (!found_pub_collist)
1201 entry->columns = NULL;
1202
1203 RelationClose(relation);
1204}
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:142
Bitmapset * pub_form_cols_map(Relation relation, PublishGencolsType include_gencols_type)
static void pgoutput_ensure_entry_cxt(PGOutputData *data, RelationSyncEntry *entry)
Definition: pgoutput.c:896

References bms_equal(), check_and_fetch_column_list(), RelationSyncEntry::columns, data, RelationSyncEntry::entry_cxt, ereport, errcode(), errmsg(), ERROR, get_namespace_name(), RelationSyncEntry::include_gencols_type, lfirst, list_length(), MemoryContextSwitchTo(), pgoutput_ensure_entry_cxt(), pub_form_cols_map(), RelationSyncEntry::publish_as_relid, RelationClose(), RelationGetNamespace, RelationGetRelationName, and RelationIdGetRelation().

Referenced by get_rel_sync_entry().

◆ pgoutput_commit_prepared_txn()

static void pgoutput_commit_prepared_txn ( LogicalDecodingContext ctx,
ReorderBufferTXN txn,
XLogRecPtr  commit_lsn 
)
static

Definition at line 695 of file pgoutput.c.

697{
698 OutputPluginUpdateProgress(ctx, false);
699
700 OutputPluginPrepareWrite(ctx, true);
701 logicalrep_write_commit_prepared(ctx->out, txn, commit_lsn);
702 OutputPluginWrite(ctx, true);
703}
void OutputPluginUpdateProgress(struct LogicalDecodingContext *ctx, bool skipped_xact)
Definition: logical.c:709
void logicalrep_write_commit_prepared(StringInfo out, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: proto.c:237

References logicalrep_write_commit_prepared(), LogicalDecodingContext::out, OutputPluginPrepareWrite(), OutputPluginUpdateProgress(), and OutputPluginWrite().

Referenced by _PG_output_plugin_init().

◆ pgoutput_commit_txn()

static void pgoutput_commit_txn ( LogicalDecodingContext ctx,
ReorderBufferTXN txn,
XLogRecPtr  commit_lsn 
)
static

Definition at line 632 of file pgoutput.c.

634{
636 bool sent_begin_txn;
637
638 Assert(txndata);
639
640 /*
641 * We don't need to send the commit message unless some relevant change
642 * from this transaction has been sent to the downstream.
643 */
644 sent_begin_txn = txndata->sent_begin_txn;
645 OutputPluginUpdateProgress(ctx, !sent_begin_txn);
646 pfree(txndata);
647 txn->output_plugin_private = NULL;
648
649 if (!sent_begin_txn)
650 {
651 elog(DEBUG1, "skipped replication of an empty transaction with XID: %u", txn->xid);
652 return;
653 }
654
655 OutputPluginPrepareWrite(ctx, true);
656 logicalrep_write_commit(ctx->out, txn, commit_lsn);
657 OutputPluginWrite(ctx, true);
658}
void pfree(void *pointer)
Definition: mcxt.c:1616
void logicalrep_write_commit(StringInfo out, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: proto.c:78
bool sent_begin_txn
Definition: pgoutput.c:216

References Assert(), DEBUG1, elog, logicalrep_write_commit(), LogicalDecodingContext::out, ReorderBufferTXN::output_plugin_private, OutputPluginPrepareWrite(), OutputPluginUpdateProgress(), OutputPluginWrite(), pfree(), PGOutputTxnData::sent_begin_txn, and ReorderBufferTXN::xid.

Referenced by _PG_output_plugin_init().

◆ pgoutput_ensure_entry_cxt()

static void pgoutput_ensure_entry_cxt ( PGOutputData data,
RelationSyncEntry entry 
)
static

Definition at line 896 of file pgoutput.c.

897{
898 Relation relation;
899
900 /* The context may already exist, in which case bail out. */
901 if (entry->entry_cxt)
902 return;
903
904 relation = RelationIdGetRelation(entry->publish_as_relid);
905
906 entry->entry_cxt = AllocSetContextCreate(data->cachectx,
907 "entry private context",
909
911 RelationGetRelationName(relation));
912}
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:170
#define MemoryContextCopyAndSetIdentifier(cxt, id)
Definition: memutils.h:101

References ALLOCSET_SMALL_SIZES, AllocSetContextCreate, data, RelationSyncEntry::entry_cxt, MemoryContextCopyAndSetIdentifier, RelationSyncEntry::publish_as_relid, RelationGetRelationName, and RelationIdGetRelation().

Referenced by pgoutput_column_list_init(), and pgoutput_row_filter_init().

◆ pgoutput_memory_context_reset()

static void pgoutput_memory_context_reset ( void *  arg)
static

Definition at line 438 of file pgoutput.c.

439{
441 {
443 RelationSyncCache = NULL;
444 }
445}
void hash_destroy(HTAB *hashp)
Definition: dynahash.c:865

References hash_destroy(), and RelationSyncCache.

Referenced by pgoutput_shutdown(), and pgoutput_startup().

◆ pgoutput_message()

static void pgoutput_message ( LogicalDecodingContext ctx,
ReorderBufferTXN txn,
XLogRecPtr  message_lsn,
bool  transactional,
const char *  prefix,
Size  sz,
const char *  message 
)
static

Definition at line 1724 of file pgoutput.c.

1727{
1730
1731 if (!data->messages)
1732 return;
1733
1734 /*
1735 * Remember the xid for the message in streaming mode. See
1736 * pgoutput_change.
1737 */
1738 if (data->in_streaming)
1739 xid = txn->xid;
1740
1741 /*
1742 * Output BEGIN if we haven't yet. Avoid for non-transactional messages.
1743 */
1744 if (transactional)
1745 {
1747
1748 /* Send BEGIN if we haven't yet */
1749 if (txndata && !txndata->sent_begin_txn)
1750 pgoutput_send_begin(ctx, txn);
1751 }
1752
1753 OutputPluginPrepareWrite(ctx, true);
1755 xid,
1756 message_lsn,
1757 transactional,
1758 prefix,
1759 sz,
1760 message);
1761 OutputPluginWrite(ctx, true);
1762}
void logicalrep_write_message(StringInfo out, TransactionId xid, XLogRecPtr lsn, bool transactional, const char *prefix, Size sz, const char *message)
Definition: proto.c:640

References data, if(), InvalidTransactionId, logicalrep_write_message(), LogicalDecodingContext::out, LogicalDecodingContext::output_plugin_private, ReorderBufferTXN::output_plugin_private, OutputPluginPrepareWrite(), OutputPluginWrite(), pgoutput_send_begin(), PGOutputTxnData::sent_begin_txn, and ReorderBufferTXN::xid.

Referenced by _PG_output_plugin_init().

◆ pgoutput_origin_filter()

static bool pgoutput_origin_filter ( LogicalDecodingContext ctx,
RepOriginId  origin_id 
)
static

Definition at line 1769 of file pgoutput.c.

1771{
1773
1774 if (data->publish_no_origin && origin_id != InvalidRepOriginId)
1775 return true;
1776
1777 return false;
1778}

References data, if(), InvalidRepOriginId, and LogicalDecodingContext::output_plugin_private.

Referenced by _PG_output_plugin_init().

◆ pgoutput_prepare_txn()

static void pgoutput_prepare_txn ( LogicalDecodingContext ctx,
ReorderBufferTXN txn,
XLogRecPtr  prepare_lsn 
)
static

Definition at line 681 of file pgoutput.c.

683{
684 OutputPluginUpdateProgress(ctx, false);
685
686 OutputPluginPrepareWrite(ctx, true);
687 logicalrep_write_prepare(ctx->out, txn, prepare_lsn);
688 OutputPluginWrite(ctx, true);
689}
void logicalrep_write_prepare(StringInfo out, ReorderBufferTXN *txn, XLogRecPtr prepare_lsn)
Definition: proto.c:187

References logicalrep_write_prepare(), LogicalDecodingContext::out, OutputPluginPrepareWrite(), OutputPluginUpdateProgress(), and OutputPluginWrite().

Referenced by _PG_output_plugin_init().

◆ pgoutput_rollback_prepared_txn()

static void pgoutput_rollback_prepared_txn ( LogicalDecodingContext ctx,
ReorderBufferTXN txn,
XLogRecPtr  prepare_end_lsn,
TimestampTz  prepare_time 
)
static

Definition at line 709 of file pgoutput.c.

713{
714 OutputPluginUpdateProgress(ctx, false);
715
716 OutputPluginPrepareWrite(ctx, true);
717 logicalrep_write_rollback_prepared(ctx->out, txn, prepare_end_lsn,
718 prepare_time);
719 OutputPluginWrite(ctx, true);
720}
void logicalrep_write_rollback_prepared(StringInfo out, ReorderBufferTXN *txn, XLogRecPtr prepare_end_lsn, TimestampTz prepare_time)
Definition: proto.c:293

References logicalrep_write_rollback_prepared(), LogicalDecodingContext::out, OutputPluginPrepareWrite(), OutputPluginUpdateProgress(), and OutputPluginWrite().

Referenced by _PG_output_plugin_init().

◆ pgoutput_row_filter()

static bool pgoutput_row_filter ( Relation  relation,
TupleTableSlot old_slot,
TupleTableSlot **  new_slot_ptr,
RelationSyncEntry entry,
ReorderBufferChangeType action 
)
static

Definition at line 1303 of file pgoutput.c.

1306{
1307 TupleDesc desc;
1308 int i;
1309 bool old_matched,
1310 new_matched,
1311 result;
1312 TupleTableSlot *tmp_new_slot;
1313 TupleTableSlot *new_slot = *new_slot_ptr;
1314 ExprContext *ecxt;
1315 ExprState *filter_exprstate;
1316
1317 /*
1318 * We need this map to avoid relying on ReorderBufferChangeType enums
1319 * having specific values.
1320 */
1321 static const int map_changetype_pubaction[] = {
1325 };
1326
1330
1331 Assert(new_slot || old_slot);
1332
1333 /* Get the corresponding row filter */
1334 filter_exprstate = entry->exprstate[map_changetype_pubaction[*action]];
1335
1336 /* Bail out if there is no row filter */
1337 if (!filter_exprstate)
1338 return true;
1339
1340 elog(DEBUG3, "table \"%s.%s\" has row filter",
1342 RelationGetRelationName(relation));
1343
1345
1346 ecxt = GetPerTupleExprContext(entry->estate);
1347
1348 /*
1349 * For the following occasions where there is only one tuple, we can
1350 * evaluate the row filter for that tuple and return.
1351 *
1352 * For inserts, we only have the new tuple.
1353 *
1354 * For updates, we can have only a new tuple when none of the replica
1355 * identity columns changed and none of those columns have external data
1356 * but we still need to evaluate the row filter for the new tuple as the
1357 * existing values of those columns might not match the filter. Also,
1358 * users can use constant expressions in the row filter, so we anyway need
1359 * to evaluate it for the new tuple.
1360 *
1361 * For deletes, we only have the old tuple.
1362 */
1363 if (!new_slot || !old_slot)
1364 {
1365 ecxt->ecxt_scantuple = new_slot ? new_slot : old_slot;
1366 result = pgoutput_row_filter_exec_expr(filter_exprstate, ecxt);
1367
1368 return result;
1369 }
1370
1371 /*
1372 * Both the old and new tuples must be valid only for updates and need to
1373 * be checked against the row filter.
1374 */
1375 Assert(map_changetype_pubaction[*action] == PUBACTION_UPDATE);
1376
1377 slot_getallattrs(new_slot);
1378 slot_getallattrs(old_slot);
1379
1380 tmp_new_slot = NULL;
1381 desc = RelationGetDescr(relation);
1382
1383 /*
1384 * The new tuple might not have all the replica identity columns, in which
1385 * case it needs to be copied over from the old tuple.
1386 */
1387 for (i = 0; i < desc->natts; i++)
1388 {
1390
1391 /*
1392 * if the column in the new tuple or old tuple is null, nothing to do
1393 */
1394 if (new_slot->tts_isnull[i] || old_slot->tts_isnull[i])
1395 continue;
1396
1397 /*
1398 * Unchanged toasted replica identity columns are only logged in the
1399 * old tuple. Copy this over to the new tuple. The changed (or WAL
1400 * Logged) toast values are always assembled in memory and set as
1401 * VARTAG_INDIRECT. See ReorderBufferToastReplace.
1402 */
1403 if (att->attlen == -1 &&
1406 {
1407 if (!tmp_new_slot)
1408 {
1409 tmp_new_slot = MakeSingleTupleTableSlot(desc, &TTSOpsVirtual);
1410 ExecClearTuple(tmp_new_slot);
1411
1412 memcpy(tmp_new_slot->tts_values, new_slot->tts_values,
1413 desc->natts * sizeof(Datum));
1414 memcpy(tmp_new_slot->tts_isnull, new_slot->tts_isnull,
1415 desc->natts * sizeof(bool));
1416 }
1417
1418 tmp_new_slot->tts_values[i] = old_slot->tts_values[i];
1419 tmp_new_slot->tts_isnull[i] = old_slot->tts_isnull[i];
1420 }
1421 }
1422
1423 ecxt->ecxt_scantuple = old_slot;
1424 old_matched = pgoutput_row_filter_exec_expr(filter_exprstate, ecxt);
1425
1426 if (tmp_new_slot)
1427 {
1428 ExecStoreVirtualTuple(tmp_new_slot);
1429 ecxt->ecxt_scantuple = tmp_new_slot;
1430 }
1431 else
1432 ecxt->ecxt_scantuple = new_slot;
1433
1434 new_matched = pgoutput_row_filter_exec_expr(filter_exprstate, ecxt);
1435
1436 /*
1437 * Case 1: if both tuples don't match the row filter, bailout. Send
1438 * nothing.
1439 */
1440 if (!old_matched && !new_matched)
1441 return false;
1442
1443 /*
1444 * Case 2: if the old tuple doesn't satisfy the row filter but the new
1445 * tuple does, transform the UPDATE into INSERT.
1446 *
1447 * Use the newly transformed tuple that must contain the column values for
1448 * all the replica identity columns. This is required to ensure that the
1449 * while inserting the tuple in the downstream node, we have all the
1450 * required column values.
1451 */
1452 if (!old_matched && new_matched)
1453 {
1455
1456 if (tmp_new_slot)
1457 *new_slot_ptr = tmp_new_slot;
1458 }
1459
1460 /*
1461 * Case 3: if the old tuple satisfies the row filter but the new tuple
1462 * doesn't, transform the UPDATE into DELETE.
1463 *
1464 * This transformation does not require another tuple. The Old tuple will
1465 * be used for DELETE.
1466 */
1467 else if (old_matched && !new_matched)
1469
1470 /*
1471 * Case 4: if both tuples match the row filter, transformation isn't
1472 * required. (*action is default UPDATE).
1473 */
1474
1475 return true;
1476}
#define DEBUG3
Definition: elog.h:28
TupleTableSlot * ExecStoreVirtualTuple(TupleTableSlot *slot)
Definition: execTuples.c:1741
#define ResetPerTupleExprContext(estate)
Definition: executor.h:665
#define GetPerTupleExprContext(estate)
Definition: executor.h:656
static bool pgoutput_row_filter_exec_expr(ExprState *state, ExprContext *econtext)
Definition: pgoutput.c:873
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:342
int16 attlen
Definition: tupdesc.h:71
TupleTableSlot * ecxt_scantuple
Definition: execnodes.h:273
bool * tts_isnull
Definition: tuptable.h:126
Datum * tts_values
Definition: tuptable.h:124
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:457
static void slot_getallattrs(TupleTableSlot *slot)
Definition: tuptable.h:371
static bool VARATT_IS_EXTERNAL_ONDISK(const void *PTR)
Definition: varatt.h:361

References generate_unaccent_rules::action, Assert(), CompactAttribute::attlen, DatumGetPointer(), DEBUG3, ExprContext::ecxt_scantuple, elog, RelationSyncEntry::estate, ExecClearTuple(), ExecStoreVirtualTuple(), RelationSyncEntry::exprstate, get_namespace_name(), GetPerTupleExprContext, i, MakeSingleTupleTableSlot(), TupleDescData::natts, pgoutput_row_filter_exec_expr(), PUBACTION_DELETE, PUBACTION_INSERT, PUBACTION_UPDATE, RelationGetDescr, RelationGetNamespace, RelationGetRelationName, REORDER_BUFFER_CHANGE_DELETE, REORDER_BUFFER_CHANGE_INSERT, REORDER_BUFFER_CHANGE_UPDATE, ResetPerTupleExprContext, slot_getallattrs(), TupleTableSlot::tts_isnull, TupleTableSlot::tts_values, TTSOpsVirtual, TupleDescCompactAttr(), and VARATT_IS_EXTERNAL_ONDISK().

Referenced by pgoutput_change().

◆ pgoutput_row_filter_exec_expr()

static bool pgoutput_row_filter_exec_expr ( ExprState state,
ExprContext econtext 
)
static

Definition at line 873 of file pgoutput.c.

874{
875 Datum ret;
876 bool isnull;
877
878 Assert(state != NULL);
879
880 ret = ExecEvalExprSwitchContext(state, econtext, &isnull);
881
882 elog(DEBUG3, "row filter evaluates to %s (isnull: %s)",
883 isnull ? "false" : DatumGetBool(ret) ? "true" : "false",
884 isnull ? "true" : "false");
885
886 if (isnull)
887 return false;
888
889 return DatumGetBool(ret);
890}
static Datum ExecEvalExprSwitchContext(ExprState *state, ExprContext *econtext, bool *isNull)
Definition: executor.h:436
static bool DatumGetBool(Datum X)
Definition: postgres.h:100
Definition: regguts.h:323

References Assert(), DatumGetBool(), DEBUG3, elog, and ExecEvalExprSwitchContext().

Referenced by pgoutput_row_filter().

◆ pgoutput_row_filter_init()

static void pgoutput_row_filter_init ( PGOutputData data,
List publications,
RelationSyncEntry entry 
)
static

Definition at line 918 of file pgoutput.c.

920{
921 ListCell *lc;
922 List *rfnodes[] = {NIL, NIL, NIL}; /* One per pubaction */
923 bool no_filter[] = {false, false, false}; /* One per pubaction */
924 MemoryContext oldctx;
925 int idx;
926 bool has_filter = true;
927 Oid schemaid = get_rel_namespace(entry->publish_as_relid);
928
929 /*
930 * Find if there are any row filters for this relation. If there are, then
931 * prepare the necessary ExprState and cache it in entry->exprstate. To
932 * build an expression state, we need to ensure the following:
933 *
934 * All the given publication-table mappings must be checked.
935 *
936 * Multiple publications might have multiple row filters for this
937 * relation. Since row filter usage depends on the DML operation, there
938 * are multiple lists (one for each operation) to which row filters will
939 * be appended.
940 *
941 * FOR ALL TABLES and FOR TABLES IN SCHEMA implies "don't use row filter
942 * expression" so it takes precedence.
943 */
944 foreach(lc, publications)
945 {
946 Publication *pub = lfirst(lc);
947 HeapTuple rftuple = NULL;
948 Datum rfdatum = 0;
949 bool pub_no_filter = true;
950
951 /*
952 * If the publication is FOR ALL TABLES, or the publication includes a
953 * FOR TABLES IN SCHEMA where the table belongs to the referred
954 * schema, then it is treated the same as if there are no row filters
955 * (even if other publications have a row filter).
956 */
957 if (!pub->alltables &&
958 !SearchSysCacheExists2(PUBLICATIONNAMESPACEMAP,
959 ObjectIdGetDatum(schemaid),
960 ObjectIdGetDatum(pub->oid)))
961 {
962 /*
963 * Check for the presence of a row filter in this publication.
964 */
965 rftuple = SearchSysCache2(PUBLICATIONRELMAP,
967 ObjectIdGetDatum(pub->oid));
968
969 if (HeapTupleIsValid(rftuple))
970 {
971 /* Null indicates no filter. */
972 rfdatum = SysCacheGetAttr(PUBLICATIONRELMAP, rftuple,
973 Anum_pg_publication_rel_prqual,
974 &pub_no_filter);
975 }
976 }
977
978 if (pub_no_filter)
979 {
980 if (rftuple)
981 ReleaseSysCache(rftuple);
982
983 no_filter[PUBACTION_INSERT] |= pub->pubactions.pubinsert;
984 no_filter[PUBACTION_UPDATE] |= pub->pubactions.pubupdate;
985 no_filter[PUBACTION_DELETE] |= pub->pubactions.pubdelete;
986
987 /*
988 * Quick exit if all the DML actions are publicized via this
989 * publication.
990 */
991 if (no_filter[PUBACTION_INSERT] &&
992 no_filter[PUBACTION_UPDATE] &&
993 no_filter[PUBACTION_DELETE])
994 {
995 has_filter = false;
996 break;
997 }
998
999 /* No additional work for this publication. Next one. */
1000 continue;
1001 }
1002
1003 /* Form the per pubaction row filter lists. */
1004 if (pub->pubactions.pubinsert && !no_filter[PUBACTION_INSERT])
1005 rfnodes[PUBACTION_INSERT] = lappend(rfnodes[PUBACTION_INSERT],
1006 TextDatumGetCString(rfdatum));
1007 if (pub->pubactions.pubupdate && !no_filter[PUBACTION_UPDATE])
1008 rfnodes[PUBACTION_UPDATE] = lappend(rfnodes[PUBACTION_UPDATE],
1009 TextDatumGetCString(rfdatum));
1010 if (pub->pubactions.pubdelete && !no_filter[PUBACTION_DELETE])
1011 rfnodes[PUBACTION_DELETE] = lappend(rfnodes[PUBACTION_DELETE],
1012 TextDatumGetCString(rfdatum));
1013
1014 ReleaseSysCache(rftuple);
1015 } /* loop all subscribed publications */
1016
1017 /* Clean the row filter */
1018 for (idx = 0; idx < NUM_ROWFILTER_PUBACTIONS; idx++)
1019 {
1020 if (no_filter[idx])
1021 {
1022 list_free_deep(rfnodes[idx]);
1023 rfnodes[idx] = NIL;
1024 }
1025 }
1026
1027 if (has_filter)
1028 {
1030
1032
1033 /*
1034 * Now all the filters for all pubactions are known. Combine them when
1035 * their pubactions are the same.
1036 */
1037 oldctx = MemoryContextSwitchTo(entry->entry_cxt);
1038 entry->estate = create_estate_for_relation(relation);
1039 for (idx = 0; idx < NUM_ROWFILTER_PUBACTIONS; idx++)
1040 {
1041 List *filters = NIL;
1042 Expr *rfnode;
1043
1044 if (rfnodes[idx] == NIL)
1045 continue;
1046
1047 foreach(lc, rfnodes[idx])
1048 filters = lappend(filters, expand_generated_columns_in_expr(stringToNode((char *) lfirst(lc)), relation, 1));
1049
1050 /* combine the row filter and cache the ExprState */
1051 rfnode = make_orclause(filters);
1052 entry->exprstate[idx] = ExecPrepareExpr(rfnode, entry->estate);
1053 } /* for each pubaction */
1054 MemoryContextSwitchTo(oldctx);
1055
1056 RelationClose(relation);
1057 }
1058}
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:262
#define TextDatumGetCString(d)
Definition: builtins.h:98
ExprState * ExecPrepareExpr(Expr *node, EState *estate)
Definition: execExpr.c:765
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void list_free_deep(List *list)
Definition: list.c:1560
Expr * make_orclause(List *orclauses)
Definition: makefuncs.c:743
#define NUM_ROWFILTER_PUBACTIONS
Definition: pgoutput.c:106
static EState * create_estate_for_relation(Relation rel)
Definition: pgoutput.c:842
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:262
void * stringToNode(const char *str)
Definition: read.c:90
Node * expand_generated_columns_in_expr(Node *node, Relation rel, int rt_index)
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:264
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:595
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:230
#define SearchSysCacheExists2(cacheId, key1, key2)
Definition: syscache.h:102

References Publication::alltables, create_estate_for_relation(), data, RelationSyncEntry::entry_cxt, RelationSyncEntry::estate, ExecPrepareExpr(), expand_generated_columns_in_expr(), RelationSyncEntry::exprstate, get_rel_namespace(), HeapTupleIsValid, idx(), lappend(), lfirst, list_free_deep(), make_orclause(), MemoryContextSwitchTo(), NIL, NUM_ROWFILTER_PUBACTIONS, ObjectIdGetDatum(), Publication::oid, pgoutput_ensure_entry_cxt(), PUBACTION_DELETE, PUBACTION_INSERT, PUBACTION_UPDATE, Publication::pubactions, PublicationActions::pubdelete, PublicationActions::pubinsert, RelationSyncEntry::publish_as_relid, PublicationActions::pubupdate, RelationClose(), RelationIdGetRelation(), ReleaseSysCache(), SearchSysCache2(), SearchSysCacheExists2, stringToNode(), SysCacheGetAttr(), and TextDatumGetCString.

Referenced by get_rel_sync_entry().

◆ pgoutput_send_begin()

static void pgoutput_send_begin ( LogicalDecodingContext ctx,
ReorderBufferTXN txn 
)
static

Definition at line 610 of file pgoutput.c.

611{
612 bool send_replication_origin = txn->origin_id != InvalidRepOriginId;
614
615 Assert(txndata);
616 Assert(!txndata->sent_begin_txn);
617
618 OutputPluginPrepareWrite(ctx, !send_replication_origin);
619 logicalrep_write_begin(ctx->out, txn);
620 txndata->sent_begin_txn = true;
621
622 send_repl_origin(ctx, txn->origin_id, txn->origin_lsn,
623 send_replication_origin);
624
625 OutputPluginWrite(ctx, true);
626}
void logicalrep_write_begin(StringInfo out, ReorderBufferTXN *txn)
Definition: proto.c:49

References Assert(), InvalidRepOriginId, logicalrep_write_begin(), ReorderBufferTXN::origin_id, ReorderBufferTXN::origin_lsn, LogicalDecodingContext::out, ReorderBufferTXN::output_plugin_private, OutputPluginPrepareWrite(), OutputPluginWrite(), send_repl_origin(), and PGOutputTxnData::sent_begin_txn.

Referenced by pgoutput_change(), pgoutput_message(), and pgoutput_truncate().

◆ pgoutput_shutdown()

static void pgoutput_shutdown ( LogicalDecodingContext ctx)
static

Definition at line 1788 of file pgoutput.c.

1789{
1791}
static void pgoutput_memory_context_reset(void *arg)
Definition: pgoutput.c:438

References pgoutput_memory_context_reset().

Referenced by _PG_output_plugin_init().

◆ pgoutput_startup()

static void pgoutput_startup ( LogicalDecodingContext ctx,
OutputPluginOptions opt,
bool  is_init 
)
static

Definition at line 451 of file pgoutput.c.

453{
455 static bool publication_callback_registered = false;
456 MemoryContextCallback *mcallback;
457
458 /* Create our memory context for private allocations. */
459 data->context = AllocSetContextCreate(ctx->context,
460 "logical replication output context",
462
463 data->cachectx = AllocSetContextCreate(ctx->context,
464 "logical replication cache context",
466
467 data->pubctx = AllocSetContextCreate(ctx->context,
468 "logical replication publication list context",
470
471 /*
472 * Ensure to cleanup RelationSyncCache even when logical decoding invoked
473 * via SQL interface ends up with an error.
474 */
478
480
481 /* This plugin uses binary protocol. */
483
484 /*
485 * This is replication start and not slot initialization.
486 *
487 * Parse and validate options passed by the client.
488 */
489 if (!is_init)
490 {
491 /* Parse the params and ERROR if we see any we don't recognize */
493
494 /* Check if we support requested protocol */
495 if (data->protocol_version > LOGICALREP_PROTO_MAX_VERSION_NUM)
497 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
498 errmsg("client sent proto_version=%d but server only supports protocol %d or lower",
499 data->protocol_version, LOGICALREP_PROTO_MAX_VERSION_NUM)));
500
501 if (data->protocol_version < LOGICALREP_PROTO_MIN_VERSION_NUM)
503 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
504 errmsg("client sent proto_version=%d but server only supports protocol %d or higher",
505 data->protocol_version, LOGICALREP_PROTO_MIN_VERSION_NUM)));
506
507 /*
508 * Decide whether to enable streaming. It is disabled by default, in
509 * which case we just update the flag in decoding context. Otherwise
510 * we only allow it with sufficient version of the protocol, and when
511 * the output plugin supports it.
512 */
513 if (data->streaming == LOGICALREP_STREAM_OFF)
514 ctx->streaming = false;
515 else if (data->streaming == LOGICALREP_STREAM_ON &&
516 data->protocol_version < LOGICALREP_PROTO_STREAM_VERSION_NUM)
518 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
519 errmsg("requested proto_version=%d does not support streaming, need %d or higher",
520 data->protocol_version, LOGICALREP_PROTO_STREAM_VERSION_NUM)));
521 else if (data->streaming == LOGICALREP_STREAM_PARALLEL &&
524 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
525 errmsg("requested proto_version=%d does not support parallel streaming, need %d or higher",
527 else if (!ctx->streaming)
529 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
530 errmsg("streaming requested, but not supported by output plugin")));
531
532 /*
533 * Here, we just check whether the two-phase option is passed by
534 * plugin and decide whether to enable it at later point of time. It
535 * remains enabled if the previous start-up has done so. But we only
536 * allow the option to be passed in with sufficient version of the
537 * protocol, and when the output plugin supports it.
538 */
539 if (!data->two_phase)
540 ctx->twophase_opt_given = false;
541 else if (data->protocol_version < LOGICALREP_PROTO_TWOPHASE_VERSION_NUM)
543 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
544 errmsg("requested proto_version=%d does not support two-phase commit, need %d or higher",
545 data->protocol_version, LOGICALREP_PROTO_TWOPHASE_VERSION_NUM)));
546 else if (!ctx->twophase)
548 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
549 errmsg("two-phase commit requested, but not supported by output plugin")));
550 else
551 ctx->twophase_opt_given = true;
552
553 /* Init publication state. */
554 data->publications = NIL;
555 publications_valid = false;
556
557 /*
558 * Register callback for pg_publication if we didn't already do that
559 * during some previous call in this process.
560 */
561 if (!publication_callback_registered)
562 {
563 CacheRegisterSyscacheCallback(PUBLICATIONOID,
565 (Datum) 0);
567 (Datum) 0);
568 publication_callback_registered = true;
569 }
570
571 /* Initialize relation schema cache. */
573 }
574 else
575 {
576 /*
577 * Disable the streaming and prepared transactions during the slot
578 * initialization mode.
579 */
580 ctx->streaming = false;
581 ctx->twophase = false;
582 }
583}
#define palloc0_object(type)
Definition: fe_memutils.h:75
void CacheRegisterRelSyncCallback(RelSyncCallbackFunction func, Datum arg)
Definition: inval.c:1879
#define LOGICALREP_PROTO_STREAM_PARALLEL_VERSION_NUM
Definition: logicalproto.h:44
#define LOGICALREP_PROTO_MIN_VERSION_NUM
Definition: logicalproto.h:40
#define LOGICALREP_PROTO_STREAM_VERSION_NUM
Definition: logicalproto.h:42
#define LOGICALREP_PROTO_TWOPHASE_VERSION_NUM
Definition: logicalproto.h:43
#define LOGICALREP_PROTO_MAX_VERSION_NUM
Definition: logicalproto.h:45
void MemoryContextRegisterResetCallback(MemoryContext context, MemoryContextCallback *cb)
Definition: mcxt.c:582
MemoryContext CacheMemoryContext
Definition: mcxt.c:169
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:160
@ OUTPUT_PLUGIN_BINARY_OUTPUT
Definition: output_plugin.h:19
static void parse_output_parameters(List *options, PGOutputData *data)
Definition: pgoutput.c:290
static void init_rel_sync_cache(MemoryContext cachectx)
Definition: pgoutput.c:1974
static void publication_invalidation_cb(Datum arg, int cacheid, uint32 hashvalue)
Definition: pgoutput.c:1831
List * output_plugin_options
Definition: logical.h:59
MemoryContextCallbackFunction func
Definition: palloc.h:49
OutputPluginOutputType output_type
Definition: output_plugin.h:28

References ALLOCSET_DEFAULT_SIZES, ALLOCSET_SMALL_SIZES, AllocSetContextCreate, CacheMemoryContext, CacheRegisterRelSyncCallback(), CacheRegisterSyscacheCallback(), LogicalDecodingContext::context, data, ereport, errcode(), errmsg(), ERROR, MemoryContextCallback::func, init_rel_sync_cache(), LOGICALREP_PROTO_MAX_VERSION_NUM, LOGICALREP_PROTO_MIN_VERSION_NUM, LOGICALREP_PROTO_STREAM_PARALLEL_VERSION_NUM, LOGICALREP_PROTO_STREAM_VERSION_NUM, LOGICALREP_PROTO_TWOPHASE_VERSION_NUM, MemoryContextRegisterResetCallback(), NIL, OUTPUT_PLUGIN_BINARY_OUTPUT, LogicalDecodingContext::output_plugin_options, LogicalDecodingContext::output_plugin_private, OutputPluginOptions::output_type, palloc0_object, parse_output_parameters(), pgoutput_memory_context_reset(), publication_invalidation_cb(), publications_valid, rel_sync_cache_relation_cb(), LogicalDecodingContext::streaming, LogicalDecodingContext::twophase, and LogicalDecodingContext::twophase_opt_given.

Referenced by _PG_output_plugin_init().

◆ pgoutput_stream_abort()

static void pgoutput_stream_abort ( struct LogicalDecodingContext ctx,
ReorderBufferTXN txn,
XLogRecPtr  abort_lsn 
)
static

Definition at line 1893 of file pgoutput.c.

1896{
1897 ReorderBufferTXN *toptxn;
1899 bool write_abort_info = (data->streaming == LOGICALREP_STREAM_PARALLEL);
1900
1901 /*
1902 * The abort should happen outside streaming block, even for streamed
1903 * transactions. The transaction has to be marked as streamed, though.
1904 */
1905 Assert(!data->in_streaming);
1906
1907 /* determine the toplevel transaction */
1908 toptxn = rbtxn_get_toptxn(txn);
1909
1910 Assert(rbtxn_is_streamed(toptxn));
1911
1912 OutputPluginPrepareWrite(ctx, true);
1913 logicalrep_write_stream_abort(ctx->out, toptxn->xid, txn->xid, abort_lsn,
1914 txn->abort_time, write_abort_info);
1915
1916 OutputPluginWrite(ctx, true);
1917
1918 cleanup_rel_sync_cache(toptxn->xid, false);
1919}
static void cleanup_rel_sync_cache(TransactionId xid, bool is_commit)
Definition: pgoutput.c:2349
void logicalrep_write_stream_abort(StringInfo out, TransactionId xid, TransactionId subxid, XLogRecPtr abort_lsn, TimestampTz abort_time, bool write_abort_info)
Definition: proto.c:1161
#define rbtxn_is_streamed(txn)
TimestampTz abort_time

References ReorderBufferTXN::abort_time, Assert(), cleanup_rel_sync_cache(), data, logicalrep_write_stream_abort(), LogicalDecodingContext::out, LogicalDecodingContext::output_plugin_private, OutputPluginPrepareWrite(), OutputPluginWrite(), rbtxn_get_toptxn, rbtxn_is_streamed, and ReorderBufferTXN::xid.

Referenced by _PG_output_plugin_init().

◆ pgoutput_stream_commit()

static void pgoutput_stream_commit ( struct LogicalDecodingContext ctx,
ReorderBufferTXN txn,
XLogRecPtr  commit_lsn 
)
static

Definition at line 1926 of file pgoutput.c.

1929{
1931
1932 /*
1933 * The commit should happen outside streaming block, even for streamed
1934 * transactions. The transaction has to be marked as streamed, though.
1935 */
1936 Assert(!data->in_streaming);
1938
1939 OutputPluginUpdateProgress(ctx, false);
1940
1941 OutputPluginPrepareWrite(ctx, true);
1942 logicalrep_write_stream_commit(ctx->out, txn, commit_lsn);
1943 OutputPluginWrite(ctx, true);
1944
1945 cleanup_rel_sync_cache(txn->xid, true);
1946}
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:229
void logicalrep_write_stream_commit(StringInfo out, ReorderBufferTXN *txn, XLogRecPtr commit_lsn)
Definition: proto.c:1107

References Assert(), cleanup_rel_sync_cache(), data, logicalrep_write_stream_commit(), LogicalDecodingContext::out, LogicalDecodingContext::output_plugin_private, OutputPluginPrepareWrite(), OutputPluginUpdateProgress(), OutputPluginWrite(), PG_USED_FOR_ASSERTS_ONLY, rbtxn_is_streamed, and ReorderBufferTXN::xid.

Referenced by _PG_output_plugin_init().

◆ pgoutput_stream_prepare_txn()

static void pgoutput_stream_prepare_txn ( LogicalDecodingContext ctx,
ReorderBufferTXN txn,
XLogRecPtr  prepare_lsn 
)
static

Definition at line 1954 of file pgoutput.c.

1957{
1959
1960 OutputPluginUpdateProgress(ctx, false);
1961 OutputPluginPrepareWrite(ctx, true);
1962 logicalrep_write_stream_prepare(ctx->out, txn, prepare_lsn);
1963 OutputPluginWrite(ctx, true);
1964}
void logicalrep_write_stream_prepare(StringInfo out, ReorderBufferTXN *txn, XLogRecPtr prepare_lsn)
Definition: proto.c:353

References Assert(), logicalrep_write_stream_prepare(), LogicalDecodingContext::out, OutputPluginPrepareWrite(), OutputPluginUpdateProgress(), OutputPluginWrite(), and rbtxn_is_streamed.

Referenced by _PG_output_plugin_init().

◆ pgoutput_stream_start()

static void pgoutput_stream_start ( struct LogicalDecodingContext ctx,
ReorderBufferTXN txn 
)
static

Definition at line 1840 of file pgoutput.c.

1842{
1844 bool send_replication_origin = txn->origin_id != InvalidRepOriginId;
1845
1846 /* we can't nest streaming of transactions */
1847 Assert(!data->in_streaming);
1848
1849 /*
1850 * If we already sent the first stream for this transaction then don't
1851 * send the origin id in the subsequent streams.
1852 */
1853 if (rbtxn_is_streamed(txn))
1854 send_replication_origin = false;
1855
1856 OutputPluginPrepareWrite(ctx, !send_replication_origin);
1858
1860 send_replication_origin);
1861
1862 OutputPluginWrite(ctx, true);
1863
1864 /* we're streaming a chunk of transaction now */
1865 data->in_streaming = true;
1866}
void logicalrep_write_stream_start(StringInfo out, TransactionId xid, bool first_segment)
Definition: proto.c:1064
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28

References Assert(), data, InvalidRepOriginId, InvalidXLogRecPtr, logicalrep_write_stream_start(), ReorderBufferTXN::origin_id, LogicalDecodingContext::out, LogicalDecodingContext::output_plugin_private, OutputPluginPrepareWrite(), OutputPluginWrite(), rbtxn_is_streamed, send_repl_origin(), and ReorderBufferTXN::xid.

Referenced by _PG_output_plugin_init().

◆ pgoutput_stream_stop()

static void pgoutput_stream_stop ( struct LogicalDecodingContext ctx,
ReorderBufferTXN txn 
)
static

Definition at line 1872 of file pgoutput.c.

1874{
1876
1877 /* we should be streaming a transaction */
1878 Assert(data->in_streaming);
1879
1880 OutputPluginPrepareWrite(ctx, true);
1882 OutputPluginWrite(ctx, true);
1883
1884 /* we've stopped streaming a transaction */
1885 data->in_streaming = false;
1886}
void logicalrep_write_stream_stop(StringInfo out)
Definition: proto.c:1098

References Assert(), data, logicalrep_write_stream_stop(), LogicalDecodingContext::out, LogicalDecodingContext::output_plugin_private, OutputPluginPrepareWrite(), and OutputPluginWrite().

Referenced by _PG_output_plugin_init().

◆ pgoutput_truncate()

static void pgoutput_truncate ( LogicalDecodingContext ctx,
ReorderBufferTXN txn,
int  nrelations,
Relation  relations[],
ReorderBufferChange change 
)
static

Definition at line 1656 of file pgoutput.c.

1658{
1661 MemoryContext old;
1662 RelationSyncEntry *relentry;
1663 int i;
1664 int nrelids;
1665 Oid *relids;
1667
1668 /* Remember the xid for the change in streaming mode. See pgoutput_change. */
1669 if (data->in_streaming)
1670 xid = change->txn->xid;
1671
1672 old = MemoryContextSwitchTo(data->context);
1673
1674 relids = palloc0(nrelations * sizeof(Oid));
1675 nrelids = 0;
1676
1677 for (i = 0; i < nrelations; i++)
1678 {
1679 Relation relation = relations[i];
1680 Oid relid = RelationGetRelid(relation);
1681
1682 if (!is_publishable_relation(relation))
1683 continue;
1684
1685 relentry = get_rel_sync_entry(data, relation);
1686
1687 if (!relentry->pubactions.pubtruncate)
1688 continue;
1689
1690 /*
1691 * Don't send partitions if the publication wants to send only the
1692 * root tables through it.
1693 */
1694 if (relation->rd_rel->relispartition &&
1695 relentry->publish_as_relid != relid)
1696 continue;
1697
1698 relids[nrelids++] = relid;
1699
1700 /* Send BEGIN if we haven't yet */
1701 if (txndata && !txndata->sent_begin_txn)
1702 pgoutput_send_begin(ctx, txn);
1703
1704 maybe_send_schema(ctx, change, relation, relentry);
1705 }
1706
1707 if (nrelids > 0)
1708 {
1709 OutputPluginPrepareWrite(ctx, true);
1711 xid,
1712 nrelids,
1713 relids,
1714 change->data.truncate.cascade,
1715 change->data.truncate.restart_seqs);
1716 OutputPluginWrite(ctx, true);
1717 }
1718
1720 MemoryContextReset(data->context);
1721}
void * palloc0(Size size)
Definition: mcxt.c:1417
void logicalrep_write_truncate(StringInfo out, TransactionId xid, int nrelids, Oid relids[], bool cascade, bool restart_seqs)
Definition: proto.c:583
struct ReorderBufferChange::@114::@116 truncate

References ReorderBufferChange::cascade, ReorderBufferChange::data, data, get_rel_sync_entry(), i, InvalidTransactionId, is_publishable_relation(), logicalrep_write_truncate(), maybe_send_schema(), MemoryContextReset(), MemoryContextSwitchTo(), LogicalDecodingContext::out, LogicalDecodingContext::output_plugin_private, ReorderBufferTXN::output_plugin_private, OutputPluginPrepareWrite(), OutputPluginWrite(), palloc0(), pgoutput_send_begin(), RelationSyncEntry::pubactions, RelationSyncEntry::publish_as_relid, PublicationActions::pubtruncate, RelationData::rd_rel, RelationGetRelid, ReorderBufferChange::restart_seqs, ReorderBufferChange::truncate, ReorderBufferChange::txn, and ReorderBufferTXN::xid.

Referenced by _PG_output_plugin_init().

◆ publication_invalidation_cb()

static void publication_invalidation_cb ( Datum  arg,
int  cacheid,
uint32  hashvalue 
)
static

Definition at line 1831 of file pgoutput.c.

1832{
1833 publications_valid = false;
1834}

References publications_valid.

Referenced by pgoutput_startup().

◆ rel_sync_cache_publication_cb()

static void rel_sync_cache_publication_cb ( Datum  arg,
int  cacheid,
uint32  hashvalue 
)
static

Definition at line 2434 of file pgoutput.c.

2435{
2436 HASH_SEQ_STATUS status;
2437 RelationSyncEntry *entry;
2438
2439 /*
2440 * We can get here if the plugin was used in SQL interface as the
2441 * RelationSyncCache is destroyed when the decoding finishes, but there is
2442 * no way to unregister the invalidation callbacks.
2443 */
2444 if (RelationSyncCache == NULL)
2445 return;
2446
2447 /*
2448 * We have no easy way to identify which cache entries this invalidation
2449 * event might have affected, so just mark them all invalid.
2450 */
2452 while ((entry = (RelationSyncEntry *) hash_seq_search(&status)) != NULL)
2453 {
2454 entry->replicate_valid = false;
2455 }
2456}

References hash_seq_init(), hash_seq_search(), RelationSyncCache, and RelationSyncEntry::replicate_valid.

Referenced by init_rel_sync_cache().

◆ rel_sync_cache_relation_cb()

static void rel_sync_cache_relation_cb ( Datum  arg,
Oid  relid 
)
static

Definition at line 2384 of file pgoutput.c.

2385{
2386 RelationSyncEntry *entry;
2387
2388 /*
2389 * We can get here if the plugin was used in SQL interface as the
2390 * RelationSyncCache is destroyed when the decoding finishes, but there is
2391 * no way to unregister the relcache invalidation callback.
2392 */
2393 if (RelationSyncCache == NULL)
2394 return;
2395
2396 /*
2397 * Nobody keeps pointers to entries in this hash table around outside
2398 * logical decoding callback calls - but invalidation events can come in
2399 * *during* a callback if we do any syscache access in the callback.
2400 * Because of that we must mark the cache entry as invalid but not damage
2401 * any of its substructure here. The next get_rel_sync_entry() call will
2402 * rebuild it all.
2403 */
2404 if (OidIsValid(relid))
2405 {
2406 /*
2407 * Getting invalidations for relations that aren't in the table is
2408 * entirely normal. So we don't care if it's found or not.
2409 */
2411 HASH_FIND, NULL);
2412 if (entry != NULL)
2413 entry->replicate_valid = false;
2414 }
2415 else
2416 {
2417 /* Whole cache must be flushed. */
2418 HASH_SEQ_STATUS status;
2419
2421 while ((entry = (RelationSyncEntry *) hash_seq_search(&status)) != NULL)
2422 {
2423 entry->replicate_valid = false;
2424 }
2425 }
2426}
#define OidIsValid(objectId)
Definition: c.h:794
@ HASH_FIND
Definition: hsearch.h:113

References HASH_FIND, hash_search(), hash_seq_init(), hash_seq_search(), OidIsValid, RelationSyncCache, and RelationSyncEntry::replicate_valid.

Referenced by init_rel_sync_cache(), and pgoutput_startup().

◆ send_relation_and_attrs()

static void send_relation_and_attrs ( Relation  relation,
TransactionId  xid,
LogicalDecodingContext ctx,
RelationSyncEntry relentry 
)
static

Definition at line 798 of file pgoutput.c.

801{
802 TupleDesc desc = RelationGetDescr(relation);
803 Bitmapset *columns = relentry->columns;
804 PublishGencolsType include_gencols_type = relentry->include_gencols_type;
805 int i;
806
807 /*
808 * Write out type info if needed. We do that only for user-created types.
809 * We use FirstGenbkiObjectId as the cutoff, so that we only consider
810 * objects with hand-assigned OIDs to be "built in", not for instance any
811 * function or type defined in the information_schema. This is important
812 * because only hand-assigned OIDs can be expected to remain stable across
813 * major versions.
814 */
815 for (i = 0; i < desc->natts; i++)
816 {
817 Form_pg_attribute att = TupleDescAttr(desc, i);
818
819 if (!logicalrep_should_publish_column(att, columns,
820 include_gencols_type))
821 continue;
822
823 if (att->atttypid < FirstGenbkiObjectId)
824 continue;
825
826 OutputPluginPrepareWrite(ctx, false);
827 logicalrep_write_typ(ctx->out, xid, att->atttypid);
828 OutputPluginWrite(ctx, false);
829 }
830
831 OutputPluginPrepareWrite(ctx, false);
832 logicalrep_write_rel(ctx->out, xid, relation, columns,
833 include_gencols_type);
834 OutputPluginWrite(ctx, false);
835}
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:202
void logicalrep_write_rel(StringInfo out, TransactionId xid, Relation rel, Bitmapset *columns, PublishGencolsType include_gencols_type)
Definition: proto.c:667
void logicalrep_write_typ(StringInfo out, TransactionId xid, Oid typoid)
Definition: proto.c:726
bool logicalrep_should_publish_column(Form_pg_attribute att, Bitmapset *columns, PublishGencolsType include_gencols_type)
Definition: proto.c:1282
#define FirstGenbkiObjectId
Definition: transam.h:195
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition: tupdesc.h:160

References RelationSyncEntry::columns, FirstGenbkiObjectId, i, RelationSyncEntry::include_gencols_type, logicalrep_should_publish_column(), logicalrep_write_rel(), logicalrep_write_typ(), TupleDescData::natts, LogicalDecodingContext::out, OutputPluginPrepareWrite(), OutputPluginWrite(), RelationGetDescr, and TupleDescAttr().

Referenced by maybe_send_schema().

◆ send_repl_origin()

static void send_repl_origin ( LogicalDecodingContext ctx,
RepOriginId  origin_id,
XLogRecPtr  origin_lsn,
bool  send_origin 
)
static

Definition at line 2460 of file pgoutput.c.

2462{
2463 if (send_origin)
2464 {
2465 char *origin;
2466
2467 /*----------
2468 * XXX: which behaviour do we want here?
2469 *
2470 * Alternatives:
2471 * - don't send origin message if origin name not found
2472 * (that's what we do now)
2473 * - throw error - that will break replication, not good
2474 * - send some special "unknown" origin
2475 *----------
2476 */
2477 if (replorigin_by_oid(origin_id, true, &origin))
2478 {
2479 /* Message boundary */
2480 OutputPluginWrite(ctx, false);
2481 OutputPluginPrepareWrite(ctx, true);
2482
2483 logicalrep_write_origin(ctx->out, origin, origin_lsn);
2484 }
2485 }
2486}
bool replorigin_by_oid(RepOriginId roident, bool missing_ok, char **roname)
Definition: origin.c:493
void logicalrep_write_origin(StringInfo out, const char *origin, XLogRecPtr origin_lsn)
Definition: proto.c:374

References logicalrep_write_origin(), LogicalDecodingContext::out, OutputPluginPrepareWrite(), OutputPluginWrite(), and replorigin_by_oid().

Referenced by pgoutput_begin_prepare_txn(), pgoutput_send_begin(), and pgoutput_stream_start().

◆ set_schema_sent_in_streamed_txn()

static void set_schema_sent_in_streamed_txn ( RelationSyncEntry entry,
TransactionId  xid 
)
static

Definition at line 2033 of file pgoutput.c.

2034{
2035 MemoryContext oldctx;
2036
2038
2039 entry->streamed_txns = lappend_xid(entry->streamed_txns, xid);
2040
2041 MemoryContextSwitchTo(oldctx);
2042}
List * lappend_xid(List *list, TransactionId datum)
Definition: list.c:393

References CacheMemoryContext, lappend_xid(), MemoryContextSwitchTo(), and RelationSyncEntry::streamed_txns.

Referenced by maybe_send_schema().

Variable Documentation

◆ publications_valid

bool publications_valid
static

Definition at line 86 of file pgoutput.c.

Referenced by get_rel_sync_entry(), pgoutput_startup(), and publication_invalidation_cb().

◆ RelationSyncCache