Text of doxygen.postgresql.org › logicalctl_8c_source.html as Styx crawled it on Nov 18, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

PostgreSQL Source Code: src/backend/replication/logical/logicalctl.c Source File

PostgreSQL Source Code git master Loading... Searching... No Matches logicalctl.c Go to the documentation of this file. 1 /*------------------------------------------------------------------------- 2 * logicalctl.c 3 * Functionality to control logical decoding status online. 4 * 5 * This module enables dynamic control of logical decoding availability. 6 * Logical decoding becomes active under two conditions: when the wal_level 7 * parameter is set to 'logical', or when at least one valid logical replication 8 * slot exists with wal_level set to 'replica'.

The system disables logical 9 * decoding when neither condition is met. Therefore, the dynamic control 10 * of logical decoding availability is required only when wal_level is set 11 * to 'replica'. Logical decoding is always enabled when wal_level='logical' 12 * and always disabled when wal_level='minimal'. 13 * 14 * The core concept of dynamically enabling and disabling logical decoding 15 * is to separately control two aspects: writing information required for 16 * logical decoding to WAL records, and using logical decoding itself.

During 17 * activation, we first enable logical WAL writing while keeping logical 18 * decoding disabled. This change is reflected in the read-only 19 * effective_wal_level GUC parameter. Once we ensure that all processes have 20 * updated to the latest effective_wal_level value, we then enable logical 21 * decoding. Deactivation follows a similar careful, multi-step process 22 * in reverse order. 23 * 24 * While activation occurs synchronously right after creating the first 25 * logical slot, deactivation happens asynchronously through the checkpointer 26 * process.

This design avoids a race condition at the end of recovery; see 27 * the comments in UpdateLogicalDecodingStatusEndOfRecovery() for details. 28 * Asynchronous deactivation also avoids excessive toggling of the logical 29 * decoding status in workloads that repeatedly create and drop a single 30 * logical slot. On the other hand, this lazy approach can delay changes 31 * to effective_wal_level and the disabling logical decoding, especially 32 * when the checkpointer is busy with other tasks.

We chose this lazy approach 33 * in all deactivation paths to keep the implementation simple, even though 34 * laziness is strictly required only for end-of-recovery cases. Future work 35 * might address this limitation either by using a dedicated worker instead 36 * of the checkpointer, or by implementing synchronous waiting during slot 37 * drops if workloads are significantly affected by the lazy deactivation 38 * of logical decoding.

39 * 40 * Standby servers use the primary server's effective_wal_level and logical 41 * decoding status. Unlike normal activation and deactivation, these 42 * are updated simultaneously without status change coordination, solely by 43 * replaying XLOG_LOGICAL_DECODING_STATUS_CHANGE records. The local wal_level 44 * setting has no effect during this time. Upon promotion, we update the 45 * logical decoding status based on local conditions: the wal_level value and 46 * the presence of logical slots.

47 * 48 * In the future, we could extend support to include automatic transitions 49 * of effective_wal_level between 'minimal' and 'logical' WAL levels. However, 50 * this enhancement would require additional coordination mechanisms and 51 * careful implementation of operations such as terminating walsenders and 52 * archiver processes while carefully considering the sequence of operations 53 * to ensure system stability during these transitions.

54 * 55 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group 56 * Portions Copyright (c) 1994, Regents of the University of California 57 * 58 * IDENTIFICATION 59 * src/backend/replication/logical/logicalctl.c 60 * 61 *------------------------------------------------------------------------- 62 */ 63 64 #include " postgres.h " 65 66 #include " access/xloginsert.h " 67 #include " catalog/pg_control.h " 68 #include " miscadmin.h " 69 #include " replication/slot.h " 70 #include " storage/ipc.h " 71 #include " storage/lmgr.h " 72 #include " storage/proc.h " 73 #include " storage/procarray.h " 74 #include " storage/procsignal.h " 75 #include " storage/subsystems.h " 76 #include " utils/injection_point.h " 77 78 /* 79 * Struct for controlling the logical decoding status.

80 * 81 * This struct is protected by LogicalDecodingControlLock. 82 */ 83 typedef struct LogicalDecodingCtlData 84 { 85 /* 86 * This is the authoritative value used by all processes to determine 87 * whether to write additional information required by logical decoding to 88 * WAL. Since this information could be checked frequently, each process 89 * caches this value in XLogLogicalInfo for better performance. 90 */ 91 bool xlog_logical_info ; 92 93 /* True if logical decoding is available in the system */ 94 bool logical_decoding_enabled ; 95 96 /* True if logical decoding might need to be disabled */ 97 bool pending_disable ; 98 } LogicalDecodingCtlData ; 99 100 static LogicalDecodingCtlData * LogicalDecodingCtl = NULL ; 101 102 static void LogicalDecodingCtlShmemRequest ( void * arg ); 103 104 const ShmemCallbacks LogicalDecodingCtlShmemCallbacks = { 105 . request_fn = LogicalDecodingCtlShmemRequest , 106 }; 107 108 /* 109 * A process-local cache of LogicalDecodingCtl->xlog_logical_info.

This is 110 * initialized at process startup, and updated when processing the process 111 * barrier signal in ProcessBarrierUpdateXLogLogicalInfo(). If the process 112 * is in an XID-assigned transaction, the cache update is delayed until the 113 * transaction ends. See the comments for XLogLogicalInfoUpdatePending for details. 114 */ 115 bool XLogLogicalInfo = false ; 116 117 /* 118 * When receiving the PROCSIGNAL_BARRIER_UPDATE_XLOG_LOGICAL_INFO signal, if 119 * an XID is assigned to the current transaction, the process sets this flag and 120 * delays the XLogLogicalInfo update until the transaction ends.

This ensures 121 * that the XLogLogicalInfo value (typically accessed via XLogLogicalInfoActive) 122 * remains consistent throughout the transaction. 123 */ 124 static bool XLogLogicalInfoUpdatePending = false ; 125 126 static void update_xlog_logical_info ( void ); 127 static void abort_logical_decoding_activation ( int code, Datum arg ); 128 static void write_logical_decoding_status_update_record ( bool status); 129 130 static void 131 LogicalDecodingCtlShmemRequest ( void * arg ) 132 { 133 ShmemRequestStruct (. name = "Logical decoding control" , 134 .size = sizeof ( LogicalDecodingCtlData ), 135 .ptr = ( void **) & LogicalDecodingCtl , 136 ); 137 } 138 139 /* 140 * Initialize the logical decoding status in shmem at server startup.

This 141 * must be called ONCE during postmaster or standalone-backend startup. 142 */ 143 void 144 StartupLogicalDecodingStatus ( bool last_status ) 145 { 146 /* Logical decoding is always disabled when 'minimal' WAL level */ 147 if ( wal_level == WAL_LEVEL_MINIMAL ) 148 return ; 149 150 /* 151 * Set the initial logical decoding status based on the last status. If 152 * logical decoding was enabled before the last shutdown, it remains 153 * enabled as we might have set wal_level='logical' or have at least one 154 * logical slot.

155 */ 156 LogicalDecodingCtl -> xlog_logical_info = last_status ; 157 LogicalDecodingCtl -> logical_decoding_enabled = last_status ; 158 } 159 160 /* 161 * Update the XLogLogicalInfo cache. 162 */ 163 static inline void 164 update_xlog_logical_info ( void ) 165 { 166 XLogLogicalInfo = IsXLogLogicalInfoEnabled (); 167 } 168 169 /* 170 * Initialize XLogLogicalInfo backend-private cache. This routine is called 171 * during process initialization.

172 */ 173 void 174 InitializeProcessXLogLogicalInfo ( void ) 175 { 176 update_xlog_logical_info (); 177 } 178 179 /* 180 * This routine is called when we are told to update XLogLogicalInfo 181 * by a ProcSignalBarrier. 182 */ 183 bool 184 ProcessBarrierUpdateXLogLogicalInfo ( void )

The rest of this page is on the live site.