4 * server-side functions to set backend global variables
5 * to control oid and relfilenode assignment, and do other special
6 * hacks needed for pg_upgrade.
8 * Copyright (c) 2010-2017, PostgreSQL Global Development Group
9 * src/backend/utils/adt/pg_upgrade_support.c
14 #include "catalog/binary_upgrade.h"
15 #include "catalog/namespace.h"
16 #include "catalog/pg_type.h"
17 #include "commands/extension.h"
18 #include "miscadmin.h"
19 #include "utils/array.h"
20 #include "utils/builtins.h"
23 Datum binary_upgrade_set_next_pg_type_oid(PG_FUNCTION_ARGS);
24 Datum binary_upgrade_set_next_array_pg_type_oid(PG_FUNCTION_ARGS);
25 Datum binary_upgrade_set_next_toast_pg_type_oid(PG_FUNCTION_ARGS);
26 Datum binary_upgrade_set_next_heap_pg_class_oid(PG_FUNCTION_ARGS);
27 Datum binary_upgrade_set_next_index_pg_class_oid(PG_FUNCTION_ARGS);
28 Datum binary_upgrade_set_next_toast_pg_class_oid(PG_FUNCTION_ARGS);
29 Datum binary_upgrade_set_next_pg_enum_oid(PG_FUNCTION_ARGS);
30 Datum binary_upgrade_set_next_pg_authid_oid(PG_FUNCTION_ARGS);
31 Datum binary_upgrade_create_empty_extension(PG_FUNCTION_ARGS);
32 Datum binary_upgrade_set_record_init_privs(PG_FUNCTION_ARGS);
35 #define CHECK_IS_BINARY_UPGRADE \
37 if (!IsBinaryUpgrade) \
39 (errcode(ERRCODE_CANT_CHANGE_RUNTIME_PARAM), \
40 (errmsg("function can only be called when server is in binary upgrade mode")))); \
44 binary_upgrade_set_next_pg_type_oid(PG_FUNCTION_ARGS)
46 Oid typoid = PG_GETARG_OID(0);
48 CHECK_IS_BINARY_UPGRADE;
49 binary_upgrade_next_pg_type_oid = typoid;
55 binary_upgrade_set_next_array_pg_type_oid(PG_FUNCTION_ARGS)
57 Oid typoid = PG_GETARG_OID(0);
59 CHECK_IS_BINARY_UPGRADE;
60 binary_upgrade_next_array_pg_type_oid = typoid;
66 binary_upgrade_set_next_toast_pg_type_oid(PG_FUNCTION_ARGS)
68 Oid typoid = PG_GETARG_OID(0);
70 CHECK_IS_BINARY_UPGRADE;
71 binary_upgrade_next_toast_pg_type_oid = typoid;
77 binary_upgrade_set_next_heap_pg_class_oid(PG_FUNCTION_ARGS)
79 Oid reloid = PG_GETARG_OID(0);
81 CHECK_IS_BINARY_UPGRADE;
82 binary_upgrade_next_heap_pg_class_oid = reloid;
88 binary_upgrade_set_next_index_pg_class_oid(PG_FUNCTION_ARGS)
90 Oid reloid = PG_GETARG_OID(0);
92 CHECK_IS_BINARY_UPGRADE;
93 binary_upgrade_next_index_pg_class_oid = reloid;
99 binary_upgrade_set_next_toast_pg_class_oid(PG_FUNCTION_ARGS)
101 Oid reloid = PG_GETARG_OID(0);
103 CHECK_IS_BINARY_UPGRADE;
104 binary_upgrade_next_toast_pg_class_oid = reloid;
110 binary_upgrade_set_next_pg_enum_oid(PG_FUNCTION_ARGS)
112 Oid enumoid = PG_GETARG_OID(0);
114 CHECK_IS_BINARY_UPGRADE;
115 binary_upgrade_next_pg_enum_oid = enumoid;
121 binary_upgrade_set_next_pg_authid_oid(PG_FUNCTION_ARGS)
123 Oid authoid = PG_GETARG_OID(0);
125 CHECK_IS_BINARY_UPGRADE;
126 binary_upgrade_next_pg_authid_oid = authoid;
131 binary_upgrade_create_empty_extension(PG_FUNCTION_ARGS)
139 List *requiredExtensions;
141 CHECK_IS_BINARY_UPGRADE;
143 /* We must check these things before dereferencing the arguments */
144 if (PG_ARGISNULL(0) ||
148 elog(ERROR, "null argument to binary_upgrade_create_empty_extension is not allowed");
150 extName = PG_GETARG_TEXT_PP(0);
151 schemaName = PG_GETARG_TEXT_PP(1);
152 relocatable = PG_GETARG_BOOL(2);
153 extVersion = PG_GETARG_TEXT_PP(3);
156 extConfig = PointerGetDatum(NULL);
158 extConfig = PG_GETARG_DATUM(4);
161 extCondition = PointerGetDatum(NULL);
163 extCondition = PG_GETARG_DATUM(5);
165 requiredExtensions = NIL;
166 if (!PG_ARGISNULL(6))
168 ArrayType *textArray = PG_GETARG_ARRAYTYPE_P(6);
173 deconstruct_array(textArray,
174 TEXTOID, -1, false, 'i',
175 &textDatums, NULL, &ndatums);
176 for (i = 0; i < ndatums; i++)
178 text *txtname = DatumGetTextPP(textDatums[i]);
179 char *extName = text_to_cstring(txtname);
180 Oid extOid = get_extension_oid(extName, false);
182 requiredExtensions = lappend_oid(requiredExtensions, extOid);
186 InsertExtensionTuple(text_to_cstring(extName),
188 get_namespace_oid(text_to_cstring(schemaName), false),
190 text_to_cstring(extVersion),
199 binary_upgrade_set_record_init_privs(PG_FUNCTION_ARGS)
201 bool record_init_privs = PG_GETARG_BOOL(0);
203 CHECK_IS_BINARY_UPGRADE;
204 binary_upgrade_record_init_privs = record_init_privs;