PostgreSQL Source Code git master
Loading...
Searching...
No Matches
pg_upgrade_support.c
Go to the documentation of this file.
1/*
2 * pg_upgrade_support.c
3 *
4 * server-side functions to set backend global variables
5 * to control oid and relfilenumber assignment, and do other special
6 * hacks needed for pg_upgrade.
7 *
8 * Copyright (c) 2010-2026, PostgreSQL Global Development Group
9 * src/backend/utils/adt/pg_upgrade_support.c
10 */
11
12#include "postgres.h"
13
14#include "access/relation.h"
15#include "access/table.h"
17#include "catalog/heap.h"
18#include "catalog/namespace.h"
20#include "catalog/pg_type.h"
21#include "commands/extension.h"
22#include "miscadmin.h"
23#include "replication/logical.h"
25#include "replication/origin.h"
27#include "storage/lmgr.h"
28#include "utils/array.h"
29#include "utils/builtins.h"
30#include "utils/lsyscache.h"
31#include "utils/pg_lsn.h"
32
33
34#define CHECK_IS_BINARY_UPGRADE \
35do { \
36 if (!IsBinaryUpgrade) \
37 ereport(ERROR, \
38 (errcode(ERRCODE_CANT_CHANGE_RUNTIME_PARAM), \
39 errmsg("function can only be called when server is in binary upgrade mode"))); \
40} while (0)
41
52
63
74
85
96
107
108Datum
118
119Datum
129
130Datum
140
141Datum
151
152Datum
162
163Datum
173
174Datum
183
184Datum
186{
187 text *extName;
189 bool relocatable;
194
196
197 /* We must check these things before dereferencing the arguments */
198 if (PG_ARGISNULL(0) ||
199 PG_ARGISNULL(1) ||
200 PG_ARGISNULL(2) ||
201 PG_ARGISNULL(3))
202 elog(ERROR, "null argument to binary_upgrade_create_empty_extension is not allowed");
203
206 relocatable = PG_GETARG_BOOL(2);
208
209 if (PG_ARGISNULL(4))
211 else
213
214 if (PG_ARGISNULL(5))
216 else
218
220 if (!PG_ARGISNULL(6))
221 {
224 int ndatums;
225 int i;
226
228 for (i = 0; i < ndatums; i++)
229 {
232
234 }
235 }
236
238 GetUserId(),
240 relocatable,
242 extConfig,
245
247}
248
249Datum
259
260Datum
274
275/*
276 * Verify the given slot has already consumed all the WAL changes.
277 *
278 * Returns true if there are no decodable WAL records after the
279 * confirmed_flush_lsn. Otherwise false.
280 *
281 * This is a special purpose function to ensure that the given slot can be
282 * upgraded without data loss.
283 */
284Datum
286{
287 Name slot_name;
288 XLogRecPtr end_of_wal;
290
292
293 /*
294 * Binary upgrades only allowed super-user connections so we must have
295 * permission to use replication slots.
296 */
298
299 slot_name = PG_GETARG_NAME(0);
300
301 /* Acquire the given slot */
302 ReplicationSlotAcquire(NameStr(*slot_name), true, true);
303
305
306 /* Slots must be valid as otherwise we won't be able to scan the WAL */
308
309 end_of_wal = GetFlushRecPtr(NULL);
311
312 /* Clean up */
314
316}
317
318/*
319 * binary_upgrade_add_sub_rel_state
320 *
321 * Add the relation with the specified relation state to pg_subscription_rel
322 * catalog.
323 */
324Datum
326{
328 Relation rel;
329 Oid subid;
330 char *subname;
331 Oid relid;
332 char relstate;
334
336
337 /* We must check these things before dereferencing the arguments */
338 if (PG_ARGISNULL(0) || PG_ARGISNULL(1) || PG_ARGISNULL(2))
339 elog(ERROR, "null argument to binary_upgrade_add_sub_rel_state is not allowed");
340
342 relid = PG_GETARG_OID(1);
343 relstate = PG_GETARG_CHAR(2);
345
347 subid = get_subscription_oid(subname, false);
348 rel = relation_open(relid, AccessShareLock);
349
350 /*
351 * Since there are no concurrent ALTER/DROP SUBSCRIPTION commands during
352 * the upgrade process, and the apply worker (which builds cache based on
353 * the subscription catalog) is not running, the locks can be released
354 * immediately.
355 */
356 AddSubscriptionRelState(subid, relid, relstate, sublsn, false);
359
361}
362
363/*
364 * binary_upgrade_replorigin_advance
365 *
366 * Update the remote_lsn for the subscriber's replication origin.
367 */
368Datum
370{
371 Relation rel;
372 Oid subid;
373 char *subname;
375 ReplOriginId node;
377
379
380 /*
381 * We must ensure a non-NULL subscription name before dereferencing the
382 * arguments.
383 */
384 if (PG_ARGISNULL(0))
385 elog(ERROR, "null argument to binary_upgrade_replorigin_advance is not allowed");
386
389
391 subid = get_subscription_oid(subname, false);
392
394
395 /* Lock to prevent the replication origin from vanishing */
397 node = replorigin_by_name(originname, false);
398
399 /*
400 * The server will be stopped after setting up the objects in the new
401 * cluster and the origins will be flushed during the shutdown checkpoint.
402 * This will ensure that the latest LSN values for origin will be
403 * available after the upgrade.
404 */
406 false /* backward */ ,
407 false /* WAL log */ );
408
411
413}
414
415/*
416 * binary_upgrade_create_conflict_detection_slot
417 *
418 * Create a replication slot to retain information necessary for conflict
419 * detection such as dead tuples, commit timestamps, and origins.
420 */
421Datum
bool binary_upgrade_record_init_privs
Definition aclchk.c:109
#define PG_GETARG_ARRAYTYPE_P(n)
Definition array.h:263
void deconstruct_array_builtin(const ArrayType *array, Oid elmtype, Datum **elemsp, bool **nullsp, int *nelemsp)
Oid binary_upgrade_next_pg_tablespace_oid
Definition tablespace.c:87
void ReplicationOriginNameForLogicalRep(Oid suboid, Oid relid, char *originname, Size szoriginname)
Definition worker.c:641
#define TextDatumGetCString(d)
Definition builtins.h:98
#define NameStr(name)
Definition c.h:765
#define Assert(condition)
Definition c.h:873
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
ObjectAddress InsertExtensionTuple(const char *extName, Oid extOwner, Oid schemaOid, bool relocatable, const char *extVersion, Datum extConfig, Datum extCondition, List *requiredExtensions)
Definition extension.c:2111
Oid get_extension_oid(const char *extname, bool missing_ok)
Definition extension.c:206
#define PG_RETURN_VOID()
Definition fmgr.h:350
#define PG_GETARG_OID(n)
Definition fmgr.h:275
#define PG_GETARG_TEXT_PP(n)
Definition fmgr.h:310
#define PG_GETARG_CHAR(n)
Definition fmgr.h:273
#define PG_ARGISNULL(n)
Definition fmgr.h:209
#define PG_GETARG_DATUM(n)
Definition fmgr.h:268
#define PG_GETARG_NAME(n)
Definition fmgr.h:279
#define PG_GETARG_BOOL(n)
Definition fmgr.h:274
#define PG_FUNCTION_ARGS
Definition fmgr.h:193
#define PG_RETURN_BOOL(x)
Definition fmgr.h:360
#define PG_GETARG_TEXT_P(n)
Definition fmgr.h:337
void SetAttrMissing(Oid relid, char *attname, char *value)
Definition heap.c:2086
RelFileNumber binary_upgrade_next_heap_pg_class_relfilenumber
Definition heap.c:83
RelFileNumber binary_upgrade_next_toast_pg_class_relfilenumber
Definition heap.c:84
Oid binary_upgrade_next_toast_pg_class_oid
Definition heap.c:82
Oid binary_upgrade_next_heap_pg_class_oid
Definition heap.c:81
RelFileNumber binary_upgrade_next_index_pg_class_relfilenumber
Definition index.c:86
Oid binary_upgrade_next_index_pg_class_oid
Definition index.c:85
static struct @172 value
int i
Definition isn.c:77
void CreateConflictDetectionSlot(void)
Definition launcher.c:1568
List * lappend_oid(List *list, Oid datum)
Definition list.c:375
void UnlockRelationOid(Oid relid, LOCKMODE lockmode)
Definition lmgr.c:229
void LockRelationOid(Oid relid, LOCKMODE lockmode)
Definition lmgr.c:107
#define AccessShareLock
Definition lockdefs.h:36
#define RowExclusiveLock
Definition lockdefs.h:38
bool LogicalReplicationSlotHasPendingWal(XLogRecPtr end_of_wal)
Definition logical.c:1994
Oid get_subscription_oid(const char *subname, bool missing_ok)
Definition lsyscache.c:3825
Oid GetUserId(void)
Definition miscinit.c:469
bool has_rolreplication(Oid roleid)
Definition miscinit.c:688
Oid get_namespace_oid(const char *nspname, bool missing_ok)
Definition namespace.c:3605
ReplOriginId replorigin_by_name(const char *roname, bool missing_ok)
Definition origin.c:231
void replorigin_advance(ReplOriginId node, XLogRecPtr remote_commit, XLogRecPtr local_commit, bool go_backward, bool wal_log)
Definition origin.c:918
NameData attname
#define NAMEDATALEN
Oid binary_upgrade_next_pg_enum_oid
Definition pg_enum.c:36
#define NIL
Definition pg_list.h:68
#define PG_GETARG_LSN(n)
Definition pg_lsn.h:36
void AddSubscriptionRelState(Oid subid, Oid relid, char state, XLogRecPtr sublsn, bool retain_lock)
NameData subname
Oid binary_upgrade_next_pg_type_oid
Definition pg_type.c:41
Datum binary_upgrade_set_next_heap_pg_class_oid(PG_FUNCTION_ARGS)
Datum binary_upgrade_set_next_multirange_pg_type_oid(PG_FUNCTION_ARGS)
Datum binary_upgrade_set_next_pg_enum_oid(PG_FUNCTION_ARGS)
Datum binary_upgrade_set_next_index_pg_class_oid(PG_FUNCTION_ARGS)
Datum binary_upgrade_set_next_array_pg_type_oid(PG_FUNCTION_ARGS)
Datum binary_upgrade_set_next_toast_relfilenode(PG_FUNCTION_ARGS)
Datum binary_upgrade_set_next_pg_authid_oid(PG_FUNCTION_ARGS)
Datum binary_upgrade_set_record_init_privs(PG_FUNCTION_ARGS)
#define CHECK_IS_BINARY_UPGRADE
Datum binary_upgrade_add_sub_rel_state(PG_FUNCTION_ARGS)
Datum binary_upgrade_set_next_multirange_array_pg_type_oid(PG_FUNCTION_ARGS)
Datum binary_upgrade_logical_slot_has_caught_up(PG_FUNCTION_ARGS)
Datum binary_upgrade_set_next_pg_tablespace_oid(PG_FUNCTION_ARGS)
Datum binary_upgrade_set_next_heap_relfilenode(PG_FUNCTION_ARGS)
Datum binary_upgrade_set_next_index_relfilenode(PG_FUNCTION_ARGS)
Datum binary_upgrade_set_missing_value(PG_FUNCTION_ARGS)
Datum binary_upgrade_create_empty_extension(PG_FUNCTION_ARGS)
Datum binary_upgrade_create_conflict_detection_slot(PG_FUNCTION_ARGS)
Datum binary_upgrade_set_next_pg_type_oid(PG_FUNCTION_ARGS)
Datum binary_upgrade_replorigin_advance(PG_FUNCTION_ARGS)
Datum binary_upgrade_set_next_toast_pg_class_oid(PG_FUNCTION_ARGS)
static Datum PointerGetDatum(const void *X)
Definition postgres.h:352
uint64_t Datum
Definition postgres.h:70
#define InvalidOid
unsigned int Oid
static int fb(int x)
Oid RelFileNumber
Definition relpath.h:25
void ReplicationSlotAcquire(const char *name, bool nowait, bool error_if_invalid)
Definition slot.c:620
ReplicationSlot * MyReplicationSlot
Definition slot.c:148
void ReplicationSlotRelease(void)
Definition slot.c:758
@ RS_INVAL_NONE
Definition slot.h:60
#define SlotIsLogical(slot)
Definition slot.h:285
void relation_close(Relation relation, LOCKMODE lockmode)
Definition relation.c:205
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition relation.c:47
Definition pg_list.h:54
ReplicationSlotInvalidationCause invalidated
Definition slot.h:128
ReplicationSlotPersistentData data
Definition slot.h:210
Definition c.h:760
Definition c.h:706
void table_close(Relation relation, LOCKMODE lockmode)
Definition table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition table.c:40
Oid binary_upgrade_next_mrng_array_pg_type_oid
Definition typecmds.c:111
Oid binary_upgrade_next_mrng_pg_type_oid
Definition typecmds.c:110
Oid binary_upgrade_next_array_pg_type_oid
Definition typecmds.c:109
Oid binary_upgrade_next_pg_authid_oid
Definition user.c:71
char * text_to_cstring(const text *t)
Definition varlena.c:214
XLogRecPtr GetFlushRecPtr(TimeLineID *insertTLI)
Definition xlog.c:6625
uint16 ReplOriginId
Definition xlogdefs.h:69
uint64 XLogRecPtr
Definition xlogdefs.h:21
#define InvalidXLogRecPtr
Definition xlogdefs.h:28