Branch data Line data Source code
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"
16 : : #include "catalog/binary_upgrade.h"
17 : : #include "catalog/heap.h"
18 : : #include "catalog/namespace.h"
19 : : #include "catalog/pg_subscription_rel.h"
20 : : #include "catalog/pg_type.h"
21 : : #include "commands/extension.h"
22 : : #include "miscadmin.h"
23 : : #include "replication/logical.h"
24 : : #include "replication/logicallauncher.h"
25 : : #include "replication/origin.h"
26 : : #include "replication/worker_internal.h"
27 : : #include "storage/lmgr.h"
28 : : #include "utils/array.h"
29 : : #include "utils/builtins.h"
30 : : #include "utils/lsyscache.h"
31 : : #include "utils/memutils.h"
32 : : #include "utils/pg_lsn.h"
33 : :
34 : :
35 : : #define CHECK_IS_BINARY_UPGRADE \
36 : : do { \
37 : : if (!IsBinaryUpgrade) \
38 : : ereport(ERROR, \
39 : : (errcode(ERRCODE_CANT_CHANGE_RUNTIME_PARAM), \
40 : : errmsg("function can only be called when server is in binary upgrade mode"))); \
41 : : } while (0)
42 : :
43 : : Datum
44 : 4 : binary_upgrade_set_next_pg_tablespace_oid(PG_FUNCTION_ARGS)
45 : : {
46 : 4 : Oid tbspoid = PG_GETARG_OID(0);
47 : :
48 [ - + - - ]: 4 : CHECK_IS_BINARY_UPGRADE;
49 : 4 : binary_upgrade_next_pg_tablespace_oid = tbspoid;
50 : :
51 : 4 : PG_RETURN_VOID();
52 : : }
53 : :
54 : : Datum
55 : 933 : binary_upgrade_set_next_pg_type_oid(PG_FUNCTION_ARGS)
56 : : {
57 : 933 : Oid typoid = PG_GETARG_OID(0);
58 : :
59 [ - + - - ]: 933 : CHECK_IS_BINARY_UPGRADE;
60 : 933 : binary_upgrade_next_pg_type_oid = typoid;
61 : :
62 : 933 : PG_RETURN_VOID();
63 : : }
64 : :
65 : : Datum
66 : 932 : binary_upgrade_set_next_array_pg_type_oid(PG_FUNCTION_ARGS)
67 : : {
68 : 932 : Oid typoid = PG_GETARG_OID(0);
69 : :
70 [ - + - - ]: 932 : CHECK_IS_BINARY_UPGRADE;
71 : 932 : binary_upgrade_next_array_pg_type_oid = typoid;
72 : :
73 : 932 : PG_RETURN_VOID();
74 : : }
75 : :
76 : : Datum
77 : 7 : binary_upgrade_set_next_multirange_pg_type_oid(PG_FUNCTION_ARGS)
78 : : {
79 : 7 : Oid typoid = PG_GETARG_OID(0);
80 : :
81 [ - + - - ]: 7 : CHECK_IS_BINARY_UPGRADE;
82 : 7 : binary_upgrade_next_mrng_pg_type_oid = typoid;
83 : :
84 : 7 : PG_RETURN_VOID();
85 : : }
86 : :
87 : : Datum
88 : 7 : binary_upgrade_set_next_multirange_array_pg_type_oid(PG_FUNCTION_ARGS)
89 : : {
90 : 7 : Oid typoid = PG_GETARG_OID(0);
91 : :
92 [ - + - - ]: 7 : CHECK_IS_BINARY_UPGRADE;
93 : 7 : binary_upgrade_next_mrng_array_pg_type_oid = typoid;
94 : :
95 : 7 : PG_RETURN_VOID();
96 : : }
97 : :
98 : : Datum
99 : 938 : binary_upgrade_set_next_heap_pg_class_oid(PG_FUNCTION_ARGS)
100 : : {
101 : 938 : Oid reloid = PG_GETARG_OID(0);
102 : :
103 [ - + - - ]: 938 : CHECK_IS_BINARY_UPGRADE;
104 : 938 : binary_upgrade_next_heap_pg_class_oid = reloid;
105 : :
106 : 938 : PG_RETURN_VOID();
107 : : }
108 : :
109 : : Datum
110 : 834 : binary_upgrade_set_next_heap_relfilenode(PG_FUNCTION_ARGS)
111 : : {
112 : 834 : RelFileNumber relfilenumber = PG_GETARG_OID(0);
113 : :
114 [ - + - - ]: 834 : CHECK_IS_BINARY_UPGRADE;
115 : 834 : binary_upgrade_next_heap_pg_class_relfilenumber = relfilenumber;
116 : :
117 : 834 : PG_RETURN_VOID();
118 : : }
119 : :
120 : : Datum
121 : 594 : binary_upgrade_set_next_index_pg_class_oid(PG_FUNCTION_ARGS)
122 : : {
123 : 594 : Oid reloid = PG_GETARG_OID(0);
124 : :
125 [ - + - - ]: 594 : CHECK_IS_BINARY_UPGRADE;
126 : 594 : binary_upgrade_next_index_pg_class_oid = reloid;
127 : :
128 : 594 : PG_RETURN_VOID();
129 : : }
130 : :
131 : : Datum
132 : 662 : binary_upgrade_set_next_index_relfilenode(PG_FUNCTION_ARGS)
133 : : {
134 : 662 : RelFileNumber relfilenumber = PG_GETARG_OID(0);
135 : :
136 [ - + - - ]: 662 : CHECK_IS_BINARY_UPGRADE;
137 : 662 : binary_upgrade_next_index_pg_class_relfilenumber = relfilenumber;
138 : :
139 : 662 : PG_RETURN_VOID();
140 : : }
141 : :
142 : : Datum
143 : 290 : binary_upgrade_set_next_toast_pg_class_oid(PG_FUNCTION_ARGS)
144 : : {
145 : 290 : Oid reloid = PG_GETARG_OID(0);
146 : :
147 [ - + - - ]: 290 : CHECK_IS_BINARY_UPGRADE;
148 : 290 : binary_upgrade_next_toast_pg_class_oid = reloid;
149 : :
150 : 290 : PG_RETURN_VOID();
151 : : }
152 : :
153 : : Datum
154 : 290 : binary_upgrade_set_next_toast_chunk_id_typoid(PG_FUNCTION_ARGS)
155 : : {
156 : 290 : Oid typoid = PG_GETARG_OID(0);
157 : :
158 [ - + - - ]: 290 : CHECK_IS_BINARY_UPGRADE;
159 : 290 : binary_upgrade_next_toast_chunk_id_typoid = typoid;
160 : :
161 : 290 : PG_RETURN_VOID();
162 : : }
163 : :
164 : : Datum
165 : 290 : binary_upgrade_set_next_toast_relfilenode(PG_FUNCTION_ARGS)
166 : : {
167 : 290 : RelFileNumber relfilenumber = PG_GETARG_OID(0);
168 : :
169 [ - + - - ]: 290 : CHECK_IS_BINARY_UPGRADE;
170 : 290 : binary_upgrade_next_toast_pg_class_relfilenumber = relfilenumber;
171 : :
172 : 290 : PG_RETURN_VOID();
173 : : }
174 : :
175 : : /*
176 : : * Supply the attribute versions, for the next relation to be created, from
177 : : * the old cluster. See SetBinaryUpgradeAttributeVersions().
178 : : */
179 : : Datum
180 : 881 : binary_upgrade_set_next_attribute_versions(PG_FUNCTION_ARGS)
181 : : {
182 : 881 : ArrayType *array = PG_GETARG_ARRAYTYPE_P(0);
183 : : int16 elmlen;
184 : : bool elmbyval;
185 : : char elmalign;
186 : : Datum *elems;
187 : : int nelems;
188 : : BinaryUpgradeAttributeVersions *versions;
189 : :
190 [ - + - - ]: 881 : CHECK_IS_BINARY_UPGRADE;
191 : :
192 [ - + ]: 881 : if (array_contains_nulls(array))
193 [ # # ]: 0 : elog(ERROR, "null attribute version value is not allowed");
194 : :
195 : 881 : get_typlenbyvalalign(LSNOID, &elmlen, &elmbyval, &elmalign);
196 : 881 : deconstruct_array(array, LSNOID, elmlen, elmbyval, elmalign,
197 : : &elems, NULL, &nelems);
198 : :
199 : : /*
200 : : * The array has to outlive this statement, as it is consumed by the next
201 : : * one. Should that statement fail, the versions stay pending and would
202 : : * be applied to the next relation created instead. That cannot happen in
203 : : * practice: pg_upgrade restores with pg_restore --exit-on-error, so a
204 : : * failed CREATE ends the restore.
205 : : */
206 : 881 : versions = MemoryContextAlloc(TopMemoryContext,
207 : 881 : offsetof(BinaryUpgradeAttributeVersions, versions) +
208 : : nelems * sizeof(XLogRecPtr));
209 : 881 : versions->natts = nelems;
210 [ + + ]: 4258 : for (int i = 0; i < nelems; i++)
211 : 3377 : versions->versions[i] = DatumGetLSN(elems[i]);
212 : :
213 [ - + ]: 881 : if (binary_upgrade_next_attribute_versions != NULL)
214 : 0 : pfree(binary_upgrade_next_attribute_versions);
215 : 881 : binary_upgrade_next_attribute_versions = versions;
216 : :
217 : 881 : PG_RETURN_VOID();
218 : : }
219 : :
220 : : Datum
221 : 50 : binary_upgrade_set_next_pg_enum_oid(PG_FUNCTION_ARGS)
222 : : {
223 : 50 : Oid enumoid = PG_GETARG_OID(0);
224 : :
225 [ - + - - ]: 50 : CHECK_IS_BINARY_UPGRADE;
226 : 50 : binary_upgrade_next_pg_enum_oid = enumoid;
227 : :
228 : 50 : PG_RETURN_VOID();
229 : : }
230 : :
231 : : Datum
232 : 19 : binary_upgrade_set_next_pg_authid_oid(PG_FUNCTION_ARGS)
233 : : {
234 : 19 : Oid authoid = PG_GETARG_OID(0);
235 : :
236 [ - + - - ]: 19 : CHECK_IS_BINARY_UPGRADE;
237 : 19 : binary_upgrade_next_pg_authid_oid = authoid;
238 : 19 : PG_RETURN_VOID();
239 : : }
240 : :
241 : : Datum
242 : 5 : binary_upgrade_create_empty_extension(PG_FUNCTION_ARGS)
243 : : {
244 : : text *extName;
245 : : text *schemaName;
246 : : bool relocatable;
247 : : text *extVersion;
248 : : Datum extConfig;
249 : : Datum extCondition;
250 : : List *requiredExtensions;
251 : :
252 [ - + - - ]: 5 : CHECK_IS_BINARY_UPGRADE;
253 : :
254 : : /* We must check these things before dereferencing the arguments */
255 [ + - ]: 5 : if (PG_ARGISNULL(0) ||
256 [ + - ]: 5 : PG_ARGISNULL(1) ||
257 [ + - ]: 5 : PG_ARGISNULL(2) ||
258 [ - + ]: 5 : PG_ARGISNULL(3))
259 [ # # ]: 0 : elog(ERROR, "null argument to binary_upgrade_create_empty_extension is not allowed");
260 : :
261 : 5 : extName = PG_GETARG_TEXT_PP(0);
262 : 5 : schemaName = PG_GETARG_TEXT_PP(1);
263 : 5 : relocatable = PG_GETARG_BOOL(2);
264 : 5 : extVersion = PG_GETARG_TEXT_PP(3);
265 : :
266 [ + - ]: 5 : if (PG_ARGISNULL(4))
267 : 5 : extConfig = PointerGetDatum(NULL);
268 : : else
269 : 0 : extConfig = PG_GETARG_DATUM(4);
270 : :
271 [ + - ]: 5 : if (PG_ARGISNULL(5))
272 : 5 : extCondition = PointerGetDatum(NULL);
273 : : else
274 : 0 : extCondition = PG_GETARG_DATUM(5);
275 : :
276 : 5 : requiredExtensions = NIL;
277 [ + - ]: 5 : if (!PG_ARGISNULL(6))
278 : : {
279 : 5 : ArrayType *textArray = PG_GETARG_ARRAYTYPE_P(6);
280 : : Datum *textDatums;
281 : : int ndatums;
282 : : int i;
283 : :
284 : 5 : deconstruct_array_builtin(textArray, TEXTOID, &textDatums, NULL, &ndatums);
285 [ - + ]: 5 : for (i = 0; i < ndatums; i++)
286 : : {
287 : 0 : char *reqExtName = TextDatumGetCString(textDatums[i]);
288 : 0 : Oid reqExtOid = get_extension_oid(reqExtName, false);
289 : :
290 : 0 : requiredExtensions = lappend_oid(requiredExtensions, reqExtOid);
291 : : }
292 : : }
293 : :
294 : 5 : InsertExtensionTuple(text_to_cstring(extName),
295 : : GetUserId(),
296 : 5 : get_namespace_oid(text_to_cstring(schemaName), false),
297 : : relocatable,
298 : 5 : text_to_cstring(extVersion),
299 : : extConfig,
300 : : extCondition,
301 : : requiredExtensions);
302 : :
303 : 5 : PG_RETURN_VOID();
304 : : }
305 : :
306 : : Datum
307 : 0 : binary_upgrade_set_record_init_privs(PG_FUNCTION_ARGS)
308 : : {
309 : 0 : bool record_init_privs = PG_GETARG_BOOL(0);
310 : :
311 [ # # # # ]: 0 : CHECK_IS_BINARY_UPGRADE;
312 : 0 : binary_upgrade_record_init_privs = record_init_privs;
313 : :
314 : 0 : PG_RETURN_VOID();
315 : : }
316 : :
317 : : Datum
318 : 3 : binary_upgrade_set_missing_value(PG_FUNCTION_ARGS)
319 : : {
320 : 3 : Oid table_id = PG_GETARG_OID(0);
321 : 3 : text *attname = PG_GETARG_TEXT_P(1);
322 : 3 : text *value = PG_GETARG_TEXT_P(2);
323 : 3 : char *cattname = text_to_cstring(attname);
324 : 3 : char *cvalue = text_to_cstring(value);
325 : :
326 [ - + - - ]: 3 : CHECK_IS_BINARY_UPGRADE;
327 : 3 : SetAttrMissing(table_id, cattname, cvalue);
328 : :
329 : 3 : PG_RETURN_VOID();
330 : : }
331 : :
332 : : /*
333 : : * Verify the given slot has already consumed all the WAL changes.
334 : : *
335 : : * Returns true if there are no decodable WAL records after the
336 : : * confirmed_flush_lsn. Otherwise false.
337 : : *
338 : : * This is a special purpose function to ensure that the given slot can be
339 : : * upgraded without data loss.
340 : : */
341 : : Datum
342 : 4 : binary_upgrade_check_logical_slot_pending_wal(PG_FUNCTION_ARGS)
343 : : {
344 : : Name slot_name;
345 : : XLogRecPtr end_of_wal;
346 : : XLogRecPtr scan_cutoff_lsn;
347 : : XLogRecPtr last_pending_wal;
348 : :
349 [ - + - - ]: 4 : CHECK_IS_BINARY_UPGRADE;
350 : :
351 : : /*
352 : : * Binary upgrades only allowed super-user connections so we must have
353 : : * permission to use replication slots.
354 : : */
355 : : Assert(has_rolreplication(GetUserId()));
356 : :
357 : 4 : slot_name = PG_GETARG_NAME(0);
358 : 4 : scan_cutoff_lsn = PG_GETARG_LSN(1);
359 : :
360 : : /* Acquire the given slot */
361 : 4 : ReplicationSlotAcquire(NameStr(*slot_name), true, true);
362 : :
363 : : Assert(SlotIsLogical(MyReplicationSlot));
364 : :
365 : : /* Slots must be valid as otherwise we won't be able to scan the WAL */
366 : : Assert(MyReplicationSlot->data.invalidated == RS_INVAL_NONE);
367 : :
368 : 4 : end_of_wal = GetFlushRecPtr(NULL);
369 : 4 : last_pending_wal = LogicalReplicationSlotCheckPendingWal(end_of_wal,
370 : : scan_cutoff_lsn);
371 : :
372 : : /* Clean up */
373 : 4 : ReplicationSlotRelease();
374 : :
375 [ + + ]: 4 : if (XLogRecPtrIsValid(last_pending_wal))
376 : 1 : PG_RETURN_LSN(last_pending_wal);
377 : : else
378 : 3 : PG_RETURN_NULL();
379 : : }
380 : :
381 : : /*
382 : : * binary_upgrade_add_sub_rel_state
383 : : *
384 : : * Add the relation with the specified relation state to pg_subscription_rel
385 : : * catalog.
386 : : */
387 : : Datum
388 : 3 : binary_upgrade_add_sub_rel_state(PG_FUNCTION_ARGS)
389 : : {
390 : : Relation subrel;
391 : : Relation rel;
392 : : Oid subid;
393 : : char *subname;
394 : : Oid relid;
395 : : char relstate;
396 : : XLogRecPtr sublsn;
397 : :
398 [ - + - - ]: 3 : CHECK_IS_BINARY_UPGRADE;
399 : :
400 : : /* We must check these things before dereferencing the arguments */
401 [ + - + - : 3 : if (PG_ARGISNULL(0) || PG_ARGISNULL(1) || PG_ARGISNULL(2))
- + ]
402 [ # # ]: 0 : elog(ERROR, "null argument to binary_upgrade_add_sub_rel_state is not allowed");
403 : :
404 : 3 : subname = text_to_cstring(PG_GETARG_TEXT_PP(0));
405 : 3 : relid = PG_GETARG_OID(1);
406 : 3 : relstate = PG_GETARG_CHAR(2);
407 [ + + ]: 3 : sublsn = PG_ARGISNULL(3) ? InvalidXLogRecPtr : PG_GETARG_LSN(3);
408 : :
409 : 3 : subrel = table_open(SubscriptionRelationId, RowExclusiveLock);
410 : 3 : subid = get_subscription_oid(subname, false);
411 : 3 : rel = relation_open(relid, AccessShareLock);
412 : :
413 : : /*
414 : : * Since there are no concurrent ALTER/DROP SUBSCRIPTION commands during
415 : : * the upgrade process, and the apply worker (which builds cache based on
416 : : * the subscription catalog) is not running, the locks can be released
417 : : * immediately.
418 : : */
419 : 3 : AddSubscriptionRelState(subid, relid, relstate, sublsn, false);
420 : 3 : relation_close(rel, AccessShareLock);
421 : 3 : table_close(subrel, RowExclusiveLock);
422 : :
423 : 3 : PG_RETURN_VOID();
424 : : }
425 : :
426 : : /*
427 : : * binary_upgrade_replorigin_advance
428 : : *
429 : : * Update the remote_lsn for the subscriber's replication origin.
430 : : */
431 : : Datum
432 : 2 : binary_upgrade_replorigin_advance(PG_FUNCTION_ARGS)
433 : : {
434 : : Relation rel;
435 : : Oid subid;
436 : : char *subname;
437 : : char originname[NAMEDATALEN];
438 : : ReplOriginId node;
439 : : XLogRecPtr remote_commit;
440 : :
441 [ - + - - ]: 2 : CHECK_IS_BINARY_UPGRADE;
442 : :
443 : : /*
444 : : * We must ensure a non-NULL subscription name before dereferencing the
445 : : * arguments.
446 : : */
447 [ - + ]: 2 : if (PG_ARGISNULL(0))
448 [ # # ]: 0 : elog(ERROR, "null argument to binary_upgrade_replorigin_advance is not allowed");
449 : :
450 : 2 : subname = text_to_cstring(PG_GETARG_TEXT_PP(0));
451 [ + - ]: 2 : remote_commit = PG_ARGISNULL(1) ? InvalidXLogRecPtr : PG_GETARG_LSN(1);
452 : :
453 : 2 : rel = table_open(SubscriptionRelationId, RowExclusiveLock);
454 : 2 : subid = get_subscription_oid(subname, false);
455 : :
456 : 2 : ReplicationOriginNameForLogicalRep(subid, InvalidOid, originname, sizeof(originname));
457 : :
458 : : /* Lock to prevent the replication origin from vanishing */
459 : 2 : LockRelationOid(ReplicationOriginRelationId, RowExclusiveLock);
460 : 2 : node = replorigin_by_name(originname, false);
461 : :
462 : : /*
463 : : * The server will be stopped after setting up the objects in the new
464 : : * cluster and the origins will be flushed during the shutdown checkpoint.
465 : : * This will ensure that the latest LSN values for origin will be
466 : : * available after the upgrade.
467 : : */
468 : 2 : replorigin_advance(node, remote_commit, InvalidXLogRecPtr,
469 : : false /* backward */ ,
470 : : false /* WAL log */ );
471 : :
472 : 2 : UnlockRelationOid(ReplicationOriginRelationId, RowExclusiveLock);
473 : 2 : table_close(rel, RowExclusiveLock);
474 : :
475 : 2 : PG_RETURN_VOID();
476 : : }
477 : :
478 : : /*
479 : : * binary_upgrade_create_conflict_detection_slot
480 : : *
481 : : * Create a replication slot to retain information necessary for conflict
482 : : * detection such as dead tuples, commit timestamps, and origins.
483 : : */
484 : : Datum
485 : 1 : binary_upgrade_create_conflict_detection_slot(PG_FUNCTION_ARGS)
486 : : {
487 [ - + - - ]: 1 : CHECK_IS_BINARY_UPGRADE;
488 : :
489 : 1 : CreateConflictDetectionSlot();
490 : :
491 : 1 : ReplicationSlotRelease();
492 : :
493 : 1 : PG_RETURN_VOID();
494 : : }
|