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-2023, PostgreSQL Global Development Group 9 : * src/backend/utils/adt/pg_upgrade_support.c 10 : */ 11 : 12 : #include "postgres.h" 13 : 14 : #include "catalog/binary_upgrade.h" 15 : #include "catalog/heap.h" 16 : #include "catalog/namespace.h" 17 : #include "catalog/pg_type.h" 18 : #include "commands/extension.h" 19 : #include "miscadmin.h" 20 : #include "utils/array.h" 21 : #include "utils/builtins.h" 22 : 23 : 24 : #define CHECK_IS_BINARY_UPGRADE \ 25 : do { \ 26 : if (!IsBinaryUpgrade) \ 27 : ereport(ERROR, \ 28 : (errcode(ERRCODE_CANT_CHANGE_RUNTIME_PARAM), \ 29 : errmsg("function can only be called when server is in binary upgrade mode"))); \ 30 : } while (0) 31 : 32 : Datum 33 0 : binary_upgrade_set_next_pg_tablespace_oid(PG_FUNCTION_ARGS) 34 : { 35 0 : Oid tbspoid = PG_GETARG_OID(0); 36 : 37 0 : CHECK_IS_BINARY_UPGRADE; 38 0 : binary_upgrade_next_pg_tablespace_oid = tbspoid; 39 : 40 0 : PG_RETURN_VOID(); 41 : } 42 : 43 : Datum 44 1434 : binary_upgrade_set_next_pg_type_oid(PG_FUNCTION_ARGS) 45 : { 46 1434 : Oid typoid = PG_GETARG_OID(0); 47 : 48 1434 : CHECK_IS_BINARY_UPGRADE; 49 1434 : binary_upgrade_next_pg_type_oid = typoid; 50 : 51 1434 : PG_RETURN_VOID(); 52 : } 53 : 54 : Datum 55 1432 : binary_upgrade_set_next_array_pg_type_oid(PG_FUNCTION_ARGS) 56 : { 57 1432 : Oid typoid = PG_GETARG_OID(0); 58 : 59 1432 : CHECK_IS_BINARY_UPGRADE; 60 1432 : binary_upgrade_next_array_pg_type_oid = typoid; 61 : 62 1432 : PG_RETURN_VOID(); 63 : } 64 : 65 : Datum 66 8 : binary_upgrade_set_next_multirange_pg_type_oid(PG_FUNCTION_ARGS) 67 : { 68 8 : Oid typoid = PG_GETARG_OID(0); 69 : 70 8 : CHECK_IS_BINARY_UPGRADE; 71 8 : binary_upgrade_next_mrng_pg_type_oid = typoid; 72 : 73 8 : PG_RETURN_VOID(); 74 : } 75 : 76 : Datum 77 8 : binary_upgrade_set_next_multirange_array_pg_type_oid(PG_FUNCTION_ARGS) 78 : { 79 8 : Oid typoid = PG_GETARG_OID(0); 80 : 81 8 : CHECK_IS_BINARY_UPGRADE; 82 8 : binary_upgrade_next_mrng_array_pg_type_oid = typoid; 83 : 84 8 : PG_RETURN_VOID(); 85 : } 86 : 87 : Datum 88 1436 : binary_upgrade_set_next_heap_pg_class_oid(PG_FUNCTION_ARGS) 89 : { 90 1436 : Oid reloid = PG_GETARG_OID(0); 91 : 92 1436 : CHECK_IS_BINARY_UPGRADE; 93 1436 : binary_upgrade_next_heap_pg_class_oid = reloid; 94 : 95 1436 : PG_RETURN_VOID(); 96 : } 97 : 98 : Datum 99 1170 : binary_upgrade_set_next_heap_relfilenode(PG_FUNCTION_ARGS) 100 : { 101 1170 : RelFileNumber relfilenumber = PG_GETARG_OID(0); 102 : 103 1170 : CHECK_IS_BINARY_UPGRADE; 104 1170 : binary_upgrade_next_heap_pg_class_relfilenumber = relfilenumber; 105 : 106 1170 : PG_RETURN_VOID(); 107 : } 108 : 109 : Datum 110 960 : binary_upgrade_set_next_index_pg_class_oid(PG_FUNCTION_ARGS) 111 : { 112 960 : Oid reloid = PG_GETARG_OID(0); 113 : 114 960 : CHECK_IS_BINARY_UPGRADE; 115 960 : binary_upgrade_next_index_pg_class_oid = reloid; 116 : 117 960 : PG_RETURN_VOID(); 118 : } 119 : 120 : Datum 121 972 : binary_upgrade_set_next_index_relfilenode(PG_FUNCTION_ARGS) 122 : { 123 972 : RelFileNumber relfilenumber = PG_GETARG_OID(0); 124 : 125 972 : CHECK_IS_BINARY_UPGRADE; 126 972 : binary_upgrade_next_index_pg_class_relfilenumber = relfilenumber; 127 : 128 972 : PG_RETURN_VOID(); 129 : } 130 : 131 : Datum 132 488 : binary_upgrade_set_next_toast_pg_class_oid(PG_FUNCTION_ARGS) 133 : { 134 488 : Oid reloid = PG_GETARG_OID(0); 135 : 136 488 : CHECK_IS_BINARY_UPGRADE; 137 488 : binary_upgrade_next_toast_pg_class_oid = reloid; 138 : 139 488 : PG_RETURN_VOID(); 140 : } 141 : 142 : Datum 143 488 : binary_upgrade_set_next_toast_relfilenode(PG_FUNCTION_ARGS) 144 : { 145 488 : RelFileNumber relfilenumber = PG_GETARG_OID(0); 146 : 147 488 : CHECK_IS_BINARY_UPGRADE; 148 488 : binary_upgrade_next_toast_pg_class_relfilenumber = relfilenumber; 149 : 150 488 : PG_RETURN_VOID(); 151 : } 152 : 153 : Datum 154 88 : binary_upgrade_set_next_pg_enum_oid(PG_FUNCTION_ARGS) 155 : { 156 88 : Oid enumoid = PG_GETARG_OID(0); 157 : 158 88 : CHECK_IS_BINARY_UPGRADE; 159 88 : binary_upgrade_next_pg_enum_oid = enumoid; 160 : 161 88 : PG_RETURN_VOID(); 162 : } 163 : 164 : Datum 165 2 : binary_upgrade_set_next_pg_authid_oid(PG_FUNCTION_ARGS) 166 : { 167 2 : Oid authoid = PG_GETARG_OID(0); 168 : 169 2 : CHECK_IS_BINARY_UPGRADE; 170 2 : binary_upgrade_next_pg_authid_oid = authoid; 171 2 : PG_RETURN_VOID(); 172 : } 173 : 174 : Datum 175 0 : binary_upgrade_create_empty_extension(PG_FUNCTION_ARGS) 176 : { 177 : text *extName; 178 : text *schemaName; 179 : bool relocatable; 180 : text *extVersion; 181 : Datum extConfig; 182 : Datum extCondition; 183 : List *requiredExtensions; 184 : 185 0 : CHECK_IS_BINARY_UPGRADE; 186 : 187 : /* We must check these things before dereferencing the arguments */ 188 0 : if (PG_ARGISNULL(0) || 189 0 : PG_ARGISNULL(1) || 190 0 : PG_ARGISNULL(2) || 191 0 : PG_ARGISNULL(3)) 192 0 : elog(ERROR, "null argument to binary_upgrade_create_empty_extension is not allowed"); 193 : 194 0 : extName = PG_GETARG_TEXT_PP(0); 195 0 : schemaName = PG_GETARG_TEXT_PP(1); 196 0 : relocatable = PG_GETARG_BOOL(2); 197 0 : extVersion = PG_GETARG_TEXT_PP(3); 198 : 199 0 : if (PG_ARGISNULL(4)) 200 0 : extConfig = PointerGetDatum(NULL); 201 : else 202 0 : extConfig = PG_GETARG_DATUM(4); 203 : 204 0 : if (PG_ARGISNULL(5)) 205 0 : extCondition = PointerGetDatum(NULL); 206 : else 207 0 : extCondition = PG_GETARG_DATUM(5); 208 : 209 0 : requiredExtensions = NIL; 210 0 : if (!PG_ARGISNULL(6)) 211 : { 212 0 : ArrayType *textArray = PG_GETARG_ARRAYTYPE_P(6); 213 : Datum *textDatums; 214 : int ndatums; 215 : int i; 216 : 217 0 : deconstruct_array_builtin(textArray, TEXTOID, &textDatums, NULL, &ndatums); 218 0 : for (i = 0; i < ndatums; i++) 219 : { 220 0 : char *extName = TextDatumGetCString(textDatums[i]); 221 0 : Oid extOid = get_extension_oid(extName, false); 222 : 223 0 : requiredExtensions = lappend_oid(requiredExtensions, extOid); 224 : } 225 : } 226 : 227 0 : InsertExtensionTuple(text_to_cstring(extName), 228 : GetUserId(), 229 0 : get_namespace_oid(text_to_cstring(schemaName), false), 230 : relocatable, 231 0 : text_to_cstring(extVersion), 232 : extConfig, 233 : extCondition, 234 : requiredExtensions); 235 : 236 0 : PG_RETURN_VOID(); 237 : } 238 : 239 : Datum 240 0 : binary_upgrade_set_record_init_privs(PG_FUNCTION_ARGS) 241 : { 242 0 : bool record_init_privs = PG_GETARG_BOOL(0); 243 : 244 0 : CHECK_IS_BINARY_UPGRADE; 245 0 : binary_upgrade_record_init_privs = record_init_privs; 246 : 247 0 : PG_RETURN_VOID(); 248 : } 249 : 250 : Datum 251 4 : binary_upgrade_set_missing_value(PG_FUNCTION_ARGS) 252 : { 253 4 : Oid table_id = PG_GETARG_OID(0); 254 4 : text *attname = PG_GETARG_TEXT_P(1); 255 4 : text *value = PG_GETARG_TEXT_P(2); 256 4 : char *cattname = text_to_cstring(attname); 257 4 : char *cvalue = text_to_cstring(value); 258 : 259 4 : CHECK_IS_BINARY_UPGRADE; 260 4 : SetAttrMissing(table_id, cattname, cvalue); 261 : 262 4 : PG_RETURN_VOID(); 263 : }