Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * bootstrap.c
4 : : * routines to support running postgres in 'bootstrap' mode
5 : : * bootstrap mode is used to create the initial template database
6 : : *
7 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
8 : : * Portions Copyright (c) 1994, Regents of the University of California
9 : : *
10 : : * IDENTIFICATION
11 : : * src/backend/bootstrap/bootstrap.c
12 : : *
13 : : *-------------------------------------------------------------------------
14 : : */
15 : : #include "postgres.h"
16 : :
17 : : #include <unistd.h>
18 : : #include <signal.h>
19 : :
20 : : #include "access/genam.h"
21 : : #include "access/heapam.h"
22 : : #include "access/htup_details.h"
23 : : #include "access/tableam.h"
24 : : #include "access/toast_compression.h"
25 : : #include "access/xact.h"
26 : : #include "bootstrap/bootstrap.h"
27 : : #include "catalog/index.h"
28 : : #include "catalog/pg_authid.h"
29 : : #include "catalog/pg_collation.h"
30 : : #include "catalog/pg_proc.h"
31 : : #include "catalog/pg_type.h"
32 : : #include "common/link-canary.h"
33 : : #include "miscadmin.h"
34 : : #include "nodes/makefuncs.h"
35 : : #include "port/pg_getopt_ctx.h"
36 : : #include "postmaster/postmaster.h"
37 : : #include "storage/bufpage.h"
38 : : #include "storage/checksum.h"
39 : : #include "storage/fd.h"
40 : : #include "storage/ipc.h"
41 : : #include "storage/proc.h"
42 : : #include "storage/shmem_internal.h"
43 : : #include "utils/builtins.h"
44 : : #include "utils/fmgroids.h"
45 : : #include "utils/guc.h"
46 : : #include "utils/memutils.h"
47 : : #include "utils/rel.h"
48 : : #include "utils/relmapper.h"
49 : :
50 : :
51 : : static void CheckerModeMain(void);
52 : : static void bootstrap_signals(void);
53 : : static Form_pg_attribute AllocateAttribute(void);
54 : : static void InsertOneProargdefaultsValue(const char *value);
55 : : static void populate_typ_list(void);
56 : : static Oid gettype(char *type);
57 : : static void cleanup(void);
58 : :
59 : : /* ----------------
60 : : * global variables
61 : : * ----------------
62 : : */
63 : :
64 : : Relation boot_reldesc; /* current relation descriptor */
65 : :
66 : : Form_pg_attribute attrtypes[MAXATTR]; /* points to attribute info */
67 : : int numattr; /* number of attributes for cur. rel */
68 : :
69 : :
70 : : /*
71 : : * Basic information associated with each type. This is used before
72 : : * pg_type is filled, so it has to cover the datatypes used as column types
73 : : * in the core "bootstrapped" catalogs.
74 : : *
75 : : * XXX several of these input/output functions do catalog scans
76 : : * (e.g., F_REGPROCIN scans pg_proc). this obviously creates some
77 : : * order dependencies in the catalog creation process.
78 : : */
79 : : struct typinfo
80 : : {
81 : : char name[NAMEDATALEN];
82 : : Oid oid;
83 : : Oid elem;
84 : : int16 len;
85 : : bool byval;
86 : : char align;
87 : : char storage;
88 : : Oid collation;
89 : : Oid inproc;
90 : : Oid outproc;
91 : : };
92 : :
93 : : static const struct typinfo TypInfo[] = {
94 : : {"bool", BOOLOID, 0, 1, true, TYPALIGN_CHAR, TYPSTORAGE_PLAIN, InvalidOid,
95 : : F_BOOLIN, F_BOOLOUT},
96 : : {"bytea", BYTEAOID, 0, -1, false, TYPALIGN_INT, TYPSTORAGE_EXTENDED, InvalidOid,
97 : : F_BYTEAIN, F_BYTEAOUT},
98 : : {"char", CHAROID, 0, 1, true, TYPALIGN_CHAR, TYPSTORAGE_PLAIN, InvalidOid,
99 : : F_CHARIN, F_CHAROUT},
100 : : {"cstring", CSTRINGOID, 0, -2, false, TYPALIGN_CHAR, TYPSTORAGE_PLAIN, InvalidOid,
101 : : F_CSTRING_IN, F_CSTRING_OUT},
102 : : {"int2", INT2OID, 0, 2, true, TYPALIGN_SHORT, TYPSTORAGE_PLAIN, InvalidOid,
103 : : F_INT2IN, F_INT2OUT},
104 : : {"int4", INT4OID, 0, 4, true, TYPALIGN_INT, TYPSTORAGE_PLAIN, InvalidOid,
105 : : F_INT4IN, F_INT4OUT},
106 : : {"int8", INT8OID, 0, 8, true, TYPALIGN_DOUBLE, TYPSTORAGE_PLAIN, InvalidOid,
107 : : F_INT8IN, F_INT8OUT},
108 : : {"float4", FLOAT4OID, 0, 4, true, TYPALIGN_INT, TYPSTORAGE_PLAIN, InvalidOid,
109 : : F_FLOAT4IN, F_FLOAT4OUT},
110 : : {"float8", FLOAT8OID, 0, 8, true, TYPALIGN_DOUBLE, TYPSTORAGE_PLAIN, InvalidOid,
111 : : F_FLOAT8IN, F_FLOAT8OUT},
112 : : {"name", NAMEOID, CHAROID, NAMEDATALEN, false, TYPALIGN_CHAR, TYPSTORAGE_PLAIN, C_COLLATION_OID,
113 : : F_NAMEIN, F_NAMEOUT},
114 : : {"regproc", REGPROCOID, 0, 4, true, TYPALIGN_INT, TYPSTORAGE_PLAIN, InvalidOid,
115 : : F_REGPROCIN, F_REGPROCOUT},
116 : : {"text", TEXTOID, 0, -1, false, TYPALIGN_INT, TYPSTORAGE_EXTENDED, DEFAULT_COLLATION_OID,
117 : : F_TEXTIN, F_TEXTOUT},
118 : : {"jsonb", JSONBOID, 0, -1, false, TYPALIGN_INT, TYPSTORAGE_EXTENDED, InvalidOid,
119 : : F_JSONB_IN, F_JSONB_OUT},
120 : : {"oid", OIDOID, 0, 4, true, TYPALIGN_INT, TYPSTORAGE_PLAIN, InvalidOid,
121 : : F_OIDIN, F_OIDOUT},
122 : : {"pg_lsn", PG_LSNOID, 0, 8, true, TYPALIGN_DOUBLE, TYPSTORAGE_PLAIN, InvalidOid,
123 : : F_PG_LSN_IN, F_PG_LSN_OUT},
124 : : {"aclitem", ACLITEMOID, 0, 16, false, TYPALIGN_DOUBLE, TYPSTORAGE_PLAIN, InvalidOid,
125 : : F_ACLITEMIN, F_ACLITEMOUT},
126 : : {"pg_node_tree", PG_NODE_TREEOID, 0, -1, false, TYPALIGN_INT, TYPSTORAGE_EXTENDED, DEFAULT_COLLATION_OID,
127 : : F_PG_NODE_TREE_IN, F_PG_NODE_TREE_OUT},
128 : : {"int2vector", INT2VECTOROID, INT2OID, -1, false, TYPALIGN_INT, TYPSTORAGE_PLAIN, InvalidOid,
129 : : F_INT2VECTORIN, F_INT2VECTOROUT},
130 : : {"oidvector", OIDVECTOROID, OIDOID, -1, false, TYPALIGN_INT, TYPSTORAGE_PLAIN, InvalidOid,
131 : : F_OIDVECTORIN, F_OIDVECTOROUT},
132 : : {"_int4", INT4ARRAYOID, INT4OID, -1, false, TYPALIGN_INT, TYPSTORAGE_EXTENDED, InvalidOid,
133 : : F_ARRAY_IN, F_ARRAY_OUT},
134 : : {"_text", TEXTARRAYOID, TEXTOID, -1, false, TYPALIGN_INT, TYPSTORAGE_EXTENDED, DEFAULT_COLLATION_OID,
135 : : F_ARRAY_IN, F_ARRAY_OUT},
136 : : {"_oid", OIDARRAYOID, OIDOID, -1, false, TYPALIGN_INT, TYPSTORAGE_EXTENDED, InvalidOid,
137 : : F_ARRAY_IN, F_ARRAY_OUT},
138 : : {"_char", CHARARRAYOID, CHAROID, -1, false, TYPALIGN_INT, TYPSTORAGE_EXTENDED, InvalidOid,
139 : : F_ARRAY_IN, F_ARRAY_OUT},
140 : : {"_aclitem", ACLITEMARRAYOID, ACLITEMOID, -1, false, TYPALIGN_DOUBLE, TYPSTORAGE_EXTENDED, InvalidOid,
141 : : F_ARRAY_IN, F_ARRAY_OUT}
142 : : };
143 : :
144 : : static const int n_types = sizeof(TypInfo) / sizeof(struct typinfo);
145 : :
146 : : struct typmap
147 : : { /* a hack */
148 : : Oid am_oid;
149 : : FormData_pg_type am_typ;
150 : : };
151 : :
152 : : static List *Typ = NIL; /* List of struct typmap* */
153 : : static struct typmap *Ap = NULL;
154 : :
155 : : /*
156 : : * Basic information about built-in roles.
157 : : *
158 : : * Presently, this need only list roles that are mentioned in aclitem arrays
159 : : * in the catalog .dat files. We might as well list everything that is in
160 : : * pg_authid.dat, since there aren't that many. Like pg_authid.dat, we
161 : : * represent the bootstrap superuser's name as "POSTGRES", even though it
162 : : * (probably) won't be that in the finished installation; this means aclitem
163 : : * entries in .dat files must spell it like that.
164 : : */
165 : : struct rolinfo
166 : : {
167 : : const char *rolname;
168 : : Oid oid;
169 : : };
170 : :
171 : : static const struct rolinfo RolInfo[] = {
172 : : {"POSTGRES", BOOTSTRAP_SUPERUSERID},
173 : : {"pg_database_owner", ROLE_PG_DATABASE_OWNER},
174 : : {"pg_read_all_data", ROLE_PG_READ_ALL_DATA},
175 : : {"pg_write_all_data", ROLE_PG_WRITE_ALL_DATA},
176 : : {"pg_monitor", ROLE_PG_MONITOR},
177 : : {"pg_read_all_settings", ROLE_PG_READ_ALL_SETTINGS},
178 : : {"pg_read_all_stats", ROLE_PG_READ_ALL_STATS},
179 : : {"pg_stat_scan_tables", ROLE_PG_STAT_SCAN_TABLES},
180 : : {"pg_read_server_files", ROLE_PG_READ_SERVER_FILES},
181 : : {"pg_write_server_files", ROLE_PG_WRITE_SERVER_FILES},
182 : : {"pg_execute_server_program", ROLE_PG_EXECUTE_SERVER_PROGRAM},
183 : : {"pg_signal_backend", ROLE_PG_SIGNAL_BACKEND},
184 : : {"pg_checkpoint", ROLE_PG_CHECKPOINT},
185 : : {"pg_maintain", ROLE_PG_MAINTAIN},
186 : : {"pg_use_reserved_connections", ROLE_PG_USE_RESERVED_CONNECTIONS},
187 : : {"pg_create_subscription", ROLE_PG_CREATE_SUBSCRIPTION},
188 : : {"pg_signal_autovacuum_worker", ROLE_PG_SIGNAL_AUTOVACUUM_WORKER}
189 : : };
190 : :
191 : :
192 : : static Datum values[MAXATTR]; /* current row's attribute values */
193 : : static bool Nulls[MAXATTR];
194 : :
195 : : static MemoryContext nogc = NULL; /* special no-gc mem context */
196 : :
197 : : /*
198 : : * At bootstrap time, we first declare all the indices to be built, and
199 : : * then build them. The IndexList structure stores enough information
200 : : * to allow us to build the indices after they've been declared.
201 : : */
202 : :
203 : : typedef struct _IndexList
204 : : {
205 : : Oid il_heap;
206 : : Oid il_ind;
207 : : IndexInfo *il_info;
208 : : struct _IndexList *il_next;
209 : : } IndexList;
210 : :
211 : : static IndexList *ILHead = NULL;
212 : :
213 : :
214 : : /*
215 : : * In shared memory checker mode, all we really want to do is create shared
216 : : * memory and semaphores (just to prove we can do it with the current GUC
217 : : * settings). Since, in fact, that was already done by
218 : : * CreateSharedMemoryAndSemaphores(), we have nothing more to do here.
219 : : */
220 : : static void
7206 alvherre@alvh.no-ip. 221 :CBC 120 : CheckerModeMain(void)
222 : : {
223 : 120 : proc_exit(0);
224 : : }
225 : :
226 : : /*
227 : : * The main entry point for running the backend in bootstrap mode
228 : : *
229 : : * The bootstrap mode is used to initialize the template database.
230 : : * The bootstrap backend doesn't speak SQL, but instead expects
231 : : * commands in a special bootstrap language.
232 : : *
233 : : * When check_only is true, startup is done only far enough to verify that
234 : : * the current configuration, particularly the passed in options pertaining
235 : : * to shared memory sizing, options work (or at least do not cause an error
236 : : * up to shared memory creation).
237 : : */
238 : : void
1941 andres@anarazel.de 239 : 205 : BootstrapModeMain(int argc, char *argv[], bool check_only)
240 : : {
241 : : int i;
1942 242 : 205 : char *progname = argv[0];
243 : : pg_getopt_ctx optctx;
244 : : int flag;
245 : 205 : char *userDoption = NULL;
236 dgustafsson@postgres 246 : 205 : uint32 bootstrap_data_checksum_version = PG_DATA_CHECKSUM_OFF;
247 : : yyscan_t scanner;
248 : :
7206 alvherre@alvh.no-ip. 249 [ - + ]: 205 : Assert(!IsUnderPostmaster);
250 : :
1942 andres@anarazel.de 251 : 205 : InitStandaloneProcess(argv[0]);
252 : :
253 : : /* Set defaults, to be overridden by explicit options below */
254 : 205 : InitializeGUCOptions();
255 : :
256 : : /* an initial --boot or --check should be present */
1937 257 [ + - + + : 205 : Assert(argc > 1
- + ]
258 : : && (strcmp(argv[1], "--boot") == 0
259 : : || strcmp(argv[1], "--check") == 0));
1942 260 : 205 : argv++;
261 : 205 : argc--;
262 : :
243 heikki.linnakangas@i 263 : 205 : pg_getopt_start(&optctx, argc, argv, "B:c:d:D:Fkr:X:-:");
264 [ + + ]: 1313 : while ((flag = pg_getopt_next(&optctx)) != -1)
265 : : {
1942 andres@anarazel.de 266 [ - - + - : 1132 : switch (flag)
- + + - +
- ]
267 : : {
1942 andres@anarazel.de 268 :UBC 0 : case 'B':
243 heikki.linnakangas@i 269 : 0 : SetConfigOption("shared_buffers", optctx.optarg, PGC_POSTMASTER, PGC_S_ARGV);
1942 andres@anarazel.de 270 : 0 : break;
1447 peter@eisentraut.org 271 : 0 : case '-':
272 : :
273 : : /*
274 : : * Error if the user misplaced a special must-be-first option
275 : : * for dispatching to a subprogram. parse_dispatch_option()
276 : : * returns DISPATCH_POSTMASTER if it doesn't find a match, so
277 : : * error for anything else.
278 : : */
243 heikki.linnakangas@i 279 [ # # ]: 0 : if (parse_dispatch_option(optctx.optarg) != DISPATCH_POSTMASTER)
724 nathan@postgresql.or 280 [ # # ]: 0 : ereport(ERROR,
281 : : (errcode(ERRCODE_SYNTAX_ERROR),
282 : : errmsg("--%s must be first argument", optctx.optarg)));
283 : :
284 : : pg_fallthrough;
285 : : case 'c':
286 : : {
287 : : char *name,
288 : : *value;
289 : :
243 heikki.linnakangas@i 290 :CBC 819 : ParseLongOption(optctx.optarg, &name, &value);
1447 peter@eisentraut.org 291 [ - + ]: 819 : if (!value)
292 : : {
1447 peter@eisentraut.org 293 [ # # ]:UBC 0 : if (flag == '-')
294 [ # # ]: 0 : ereport(ERROR,
295 : : (errcode(ERRCODE_SYNTAX_ERROR),
296 : : errmsg("--%s requires a value",
297 : : optctx.optarg)));
298 : : else
299 [ # # ]: 0 : ereport(ERROR,
300 : : (errcode(ERRCODE_SYNTAX_ERROR),
301 : : errmsg("-c %s requires a value",
302 : : optctx.optarg)));
303 : : }
304 : :
1447 peter@eisentraut.org 305 :CBC 819 : SetConfigOption(name, value, PGC_POSTMASTER, PGC_S_ARGV);
306 : 795 : pfree(name);
307 : 795 : pfree(value);
308 : 795 : break;
309 : : }
1942 andres@anarazel.de 310 :UBC 0 : case 'D':
243 heikki.linnakangas@i 311 : 0 : userDoption = pstrdup(optctx.optarg);
1942 andres@anarazel.de 312 : 0 : break;
313 : 0 : case 'd':
314 : : {
315 : : /* Turn on debugging for the bootstrap process. */
316 : : char *debugstr;
317 : :
243 heikki.linnakangas@i 318 : 0 : debugstr = psprintf("debug%s", optctx.optarg);
1942 andres@anarazel.de 319 : 0 : SetConfigOption("log_min_messages", debugstr,
320 : : PGC_POSTMASTER, PGC_S_ARGV);
321 : 0 : SetConfigOption("client_min_messages", debugstr,
322 : : PGC_POSTMASTER, PGC_S_ARGV);
323 : 0 : pfree(debugstr);
324 : : }
325 : 0 : break;
1942 andres@anarazel.de 326 :CBC 205 : case 'F':
327 : 205 : SetConfigOption("fsync", "false", PGC_POSTMASTER, PGC_S_ARGV);
328 : 205 : break;
329 : 47 : case 'k':
330 : 47 : bootstrap_data_checksum_version = PG_DATA_CHECKSUM_VERSION;
331 : 47 : break;
1942 andres@anarazel.de 332 :UBC 0 : case 'r':
243 heikki.linnakangas@i 333 : 0 : strlcpy(OutputFileName, optctx.optarg, MAXPGPATH);
1942 andres@anarazel.de 334 : 0 : break;
1942 andres@anarazel.de 335 :CBC 61 : case 'X':
243 heikki.linnakangas@i 336 : 61 : SetConfigOption("wal_segment_size", optctx.optarg, PGC_INTERNAL, PGC_S_DYNAMIC_DEFAULT);
1942 andres@anarazel.de 337 : 61 : break;
1942 andres@anarazel.de 338 :UBC 0 : default:
339 : 0 : write_stderr("Try \"%s --help\" for more information.\n",
340 : : progname);
341 : 0 : proc_exit(1);
342 : : break;
343 : : }
344 : : }
345 : :
243 heikki.linnakangas@i 346 [ - + ]:CBC 181 : if (argc != optctx.optind)
347 : : {
1942 andres@anarazel.de 348 :UBC 0 : write_stderr("%s: invalid command-line arguments\n", progname);
349 : 0 : proc_exit(1);
350 : : }
351 : :
352 : : /* Acquire configuration parameters */
1942 andres@anarazel.de 353 [ - + ]:CBC 181 : if (!SelectConfigFiles(userDoption, progname))
1942 andres@anarazel.de 354 :UBC 0 : proc_exit(1);
355 : :
356 : : /*
357 : : * Validate we have been given a reasonable-looking DataDir and change
358 : : * into it
359 : : */
1942 andres@anarazel.de 360 :CBC 180 : checkDataDir();
361 : 180 : ChangeToDataDir();
362 : :
363 : 180 : CreateDataDirLockFile(false);
364 : :
365 : 180 : SetProcessingMode(BootstrapProcessing);
366 : 180 : IgnoreSystemIndexes = true;
367 : :
236 heikki.linnakangas@i 368 : 180 : RegisterBuiltinShmemCallbacks();
369 : :
1942 andres@anarazel.de 370 : 180 : InitializeMaxBackends();
371 : :
372 : : /*
373 : : * Even though bootstrapping runs in single-process mode, initialize
374 : : * postmaster child slots array so that --check can detect running out of
375 : : * shared memory or other resources if max_connections is set too high.
376 : : */
744 heikki.linnakangas@i 377 : 180 : InitPostmasterChildSlots();
378 : :
798 tomas.vondra@postgre 379 : 180 : InitializeFastPathLocks();
380 : :
236 heikki.linnakangas@i 381 : 180 : ShmemCallRequestCallbacks();
1941 andres@anarazel.de 382 : 180 : CreateSharedMemoryAndSemaphores();
383 : :
384 : : /*
385 : : * Estimate number of openable files. This is essential too in --check
386 : : * mode, because on some platforms semaphores count as open files.
387 : : */
711 tgl@sss.pgh.pa.us 388 : 180 : set_max_safe_fds();
389 : :
390 : : /*
391 : : * XXX: It might make sense to move this into its own function at some
392 : : * point. Right now it seems like it'd cause more code duplication than
393 : : * it's worth.
394 : : */
1941 andres@anarazel.de 395 [ + + ]: 180 : if (check_only)
396 : : {
1942 397 : 120 : SetProcessingMode(NormalProcessing);
398 : 120 : CheckerModeMain();
1942 andres@anarazel.de 399 :UBC 0 : abort();
400 : : }
401 : :
402 : : /*
403 : : * Do backend-like initialization for bootstrap mode
404 : : */
1941 andres@anarazel.de 405 :CBC 60 : InitProcess();
406 : :
407 : 60 : BaseInit();
408 : :
1942 409 : 60 : bootstrap_signals();
858 peter@eisentraut.org 410 : 60 : BootStrapXLOG(bootstrap_data_checksum_version);
411 : :
412 : : /*
413 : : * To ensure that src/common/link-canary.c is linked into the backend, we
414 : : * must call it from somewhere. Here is as good as anywhere.
415 : : */
3002 tgl@sss.pgh.pa.us 416 [ - + ]: 60 : if (pg_link_canary_is_frontend())
3002 tgl@sss.pgh.pa.us 417 [ # # ]:UBC 0 : elog(ERROR, "backend is incorrectly linked to frontend functions");
418 : :
1144 michael@paquier.xyz 419 :CBC 60 : InitPostgres(NULL, InvalidOid, NULL, InvalidOid, 0, NULL);
420 : :
421 : : /* Initialize stuff for bootstrap-file processing */
10674 bruce@momjian.us 422 [ + + ]: 2460 : for (i = 0; i < MAXATTR; i++)
423 : : {
8361 neilc@samurai.com 424 : 2400 : attrtypes[i] = NULL;
6600 tgl@sss.pgh.pa.us 425 : 2400 : Nulls[i] = false;
426 : : }
427 : :
709 peter@eisentraut.org 428 [ - + ]: 60 : if (boot_yylex_init(&scanner) != 0)
709 peter@eisentraut.org 429 [ # # ]:UBC 0 : elog(ERROR, "yylex_init() failed: %m");
430 : :
431 : : /*
432 : : * Process bootstrap input.
433 : : */
3515 tgl@sss.pgh.pa.us 434 :CBC 60 : StartTransactionCommand();
709 peter@eisentraut.org 435 : 60 : boot_yyparse(scanner);
3515 tgl@sss.pgh.pa.us 436 : 60 : CommitTransactionCommand();
437 : :
438 : : /*
439 : : * We should now know about all mapped relations, so it's okay to write
440 : : * out the initial relation mapping files.
441 : : */
6138 442 : 60 : RelationMapFinishBootstrap();
443 : :
444 : : /* Clean up and exit */
10674 bruce@momjian.us 445 : 60 : cleanup();
7206 alvherre@alvh.no-ip. 446 : 60 : proc_exit(0);
447 : : }
448 : :
449 : :
450 : : /* ----------------------------------------------------------------
451 : : * misc functions
452 : : * ----------------------------------------------------------------
453 : : */
454 : :
455 : : /*
456 : : * Set up signal handling for a bootstrap process
457 : : */
458 : : static void
8218 tgl@sss.pgh.pa.us 459 : 60 : bootstrap_signals(void)
460 : : {
4337 andres@anarazel.de 461 [ - + ]: 60 : Assert(!IsUnderPostmaster);
462 : :
463 : : /*
464 : : * We don't actually need any non-default signal handling in bootstrap
465 : : * mode; "curl up and die" is a sufficient response for all these cases.
466 : : * Let's set that handling explicitly, as documentation if nothing else.
467 : : */
228 andrew@dunslane.net 468 : 60 : pqsignal(SIGHUP, PG_SIG_DFL);
469 : 60 : pqsignal(SIGINT, PG_SIG_DFL);
470 : 60 : pqsignal(SIGTERM, PG_SIG_DFL);
471 : 60 : pqsignal(SIGQUIT, PG_SIG_DFL);
8218 tgl@sss.pgh.pa.us 472 : 60 : }
473 : :
474 : : /* ----------------------------------------------------------------
475 : : * MANUAL BACKEND INTERACTIVE INTERFACE COMMANDS
476 : : * ----------------------------------------------------------------
477 : : */
478 : :
479 : : /* ----------------
480 : : * boot_openrel
481 : : *
482 : : * Execute BKI OPEN command.
483 : : * ----------------
484 : : */
485 : : void
11099 scrappy@hub.org 486 : 3600 : boot_openrel(char *relname)
487 : : {
488 : : int i;
489 : :
7898 tgl@sss.pgh.pa.us 490 [ - + ]: 3600 : if (strlen(relname) >= NAMEDATALEN)
10502 bruce@momjian.us 491 :UBC 0 : relname[NAMEDATALEN - 1] = '\0';
492 : :
493 : : /*
494 : : * pg_type must be filled before any OPEN command is executed, hence we
495 : : * can now populate Typ if we haven't yet.
496 : : */
2075 tomas.vondra@postgre 497 [ - + ]:CBC 3600 : if (Typ == NIL)
2075 tomas.vondra@postgre 498 :UBC 0 : populate_typ_list();
499 : :
8981 tgl@sss.pgh.pa.us 500 [ - + ]:CBC 3600 : if (boot_reldesc != NULL)
10674 bruce@momjian.us 501 :UBC 0 : closerel(NULL);
502 : :
8530 tgl@sss.pgh.pa.us 503 [ - + ]:CBC 3600 : elog(DEBUG4, "open relation %s, attrsize %d",
504 : : relname, (int) ATTRIBUTE_FIXED_PART_SIZE);
505 : :
2868 andres@anarazel.de 506 : 3600 : boot_reldesc = table_openrv(makeRangeVar(NULL, relname, -1), NoLock);
3157 teodor@sigaev.ru 507 : 3600 : numattr = RelationGetNumberOfAttributes(boot_reldesc);
10674 bruce@momjian.us 508 [ + + ]: 33300 : for (i = 0; i < numattr; i++)
509 : : {
510 [ - + ]: 29700 : if (attrtypes[i] == NULL)
10674 bruce@momjian.us 511 :UBC 0 : attrtypes[i] = AllocateAttribute();
654 peter@eisentraut.org 512 :CBC 29700 : memmove(attrtypes[i],
513 : 29700 : TupleDescAttr(boot_reldesc->rd_att, i),
514 : : ATTRIBUTE_FIXED_PART_SIZE);
515 : :
516 : : {
10315 bruce@momjian.us 517 : 29700 : Form_pg_attribute at = attrtypes[i];
518 : :
8586 519 [ - + ]: 29700 : elog(DEBUG4, "create attribute %d name %s len %d num %d type %u",
520 : : i, NameStr(at->attname), at->attlen, at->attnum,
521 : : at->atttypid);
522 : : }
523 : : }
11099 scrappy@hub.org 524 : 3600 : }
525 : :
526 : : /* ----------------
527 : : * closerel
528 : : * ----------------
529 : : */
530 : : void
1530 pg@bowt.ie 531 : 3840 : closerel(char *relname)
532 : : {
533 [ + - ]: 3840 : if (relname)
534 : : {
8981 tgl@sss.pgh.pa.us 535 [ + - ]: 3840 : if (boot_reldesc)
536 : : {
1530 pg@bowt.ie 537 [ - + ]: 3840 : if (strcmp(RelationGetRelationName(boot_reldesc), relname) != 0)
8530 tgl@sss.pgh.pa.us 538 [ # # ]:UBC 0 : elog(ERROR, "close of %s when %s was expected",
539 : : relname, RelationGetRelationName(boot_reldesc));
540 : : }
541 : : else
542 [ # # ]: 0 : elog(ERROR, "close of %s before any relation was opened",
543 : : relname);
544 : : }
545 : :
8981 tgl@sss.pgh.pa.us 546 [ - + ]:CBC 3840 : if (boot_reldesc == NULL)
9331 peter_e@gmx.net 547 [ # # ]:UBC 0 : elog(ERROR, "no open relation to close");
548 : : else
549 : : {
7898 tgl@sss.pgh.pa.us 550 [ - + ]:CBC 3840 : elog(DEBUG4, "close relation %s",
551 : : RelationGetRelationName(boot_reldesc));
2868 andres@anarazel.de 552 : 3840 : table_close(boot_reldesc, NoLock);
8361 neilc@samurai.com 553 : 3840 : boot_reldesc = NULL;
554 : : }
11099 scrappy@hub.org 555 : 3840 : }
556 : :
557 : :
558 : :
559 : : /* ----------------
560 : : * DEFINEATTR()
561 : : *
562 : : * define a <field,type> pair
563 : : * if there are n fields in a relation to be created, this routine
564 : : * will be called n times
565 : : * ----------------
566 : : */
567 : : void
4298 andres@anarazel.de 568 : 37020 : DefineAttr(char *name, char *type, int attnum, int nullness)
569 : : {
570 : : Oid typeoid;
571 : :
8981 tgl@sss.pgh.pa.us 572 [ - + ]: 37020 : if (boot_reldesc != NULL)
573 : : {
8530 tgl@sss.pgh.pa.us 574 [ # # ]:UBC 0 : elog(WARNING, "no open relations allowed with CREATE command");
7898 575 : 0 : closerel(NULL);
576 : : }
577 : :
8361 neilc@samurai.com 578 [ + + ]:CBC 37020 : if (attrtypes[attnum] == NULL)
10674 bruce@momjian.us 579 : 2040 : attrtypes[attnum] = AllocateAttribute();
6519 tgl@sss.pgh.pa.us 580 [ + - - + : 37020 : MemSet(attrtypes[attnum], 0, ATTRIBUTE_FIXED_PART_SIZE);
- - - - -
- ]
581 : :
8884 582 : 37020 : namestrcpy(&attrtypes[attnum]->attname, name);
8586 bruce@momjian.us 583 [ - + ]: 37020 : elog(DEBUG4, "column %s %s", NameStr(attrtypes[attnum]->attname), type);
2692 michael@paquier.xyz 584 : 37020 : attrtypes[attnum]->attnum = attnum + 1;
585 : :
8884 tgl@sss.pgh.pa.us 586 : 37020 : typeoid = gettype(type);
587 : :
2075 tomas.vondra@postgre 588 [ + + ]: 37020 : if (Typ != NIL)
589 : : {
10674 bruce@momjian.us 590 : 31800 : attrtypes[attnum]->atttypid = Ap->am_oid;
7914 tgl@sss.pgh.pa.us 591 : 31800 : attrtypes[attnum]->attlen = Ap->am_typ.typlen;
10674 bruce@momjian.us 592 : 31800 : attrtypes[attnum]->attbyval = Ap->am_typ.typbyval;
10323 593 : 31800 : attrtypes[attnum]->attalign = Ap->am_typ.typalign;
2015 tgl@sss.pgh.pa.us 594 : 31800 : attrtypes[attnum]->attstorage = Ap->am_typ.typstorage;
2011 595 : 31800 : attrtypes[attnum]->attcompression = InvalidCompressionMethod;
5772 peter_e@gmx.net 596 : 31800 : attrtypes[attnum]->attcollation = Ap->am_typ.typcollation;
597 : : /* if an array type, assume 1-dimensional attribute */
8362 tgl@sss.pgh.pa.us 598 [ + + + + ]: 31800 : if (Ap->am_typ.typelem != InvalidOid && Ap->am_typ.typlen < 0)
599 : 2820 : attrtypes[attnum]->attndims = 1;
600 : : else
601 : 28980 : attrtypes[attnum]->attndims = 0;
602 : : }
603 : : else
604 : : {
8276 605 : 5220 : attrtypes[attnum]->atttypid = TypInfo[typeoid].oid;
7914 606 : 5220 : attrtypes[attnum]->attlen = TypInfo[typeoid].len;
8276 607 : 5220 : attrtypes[attnum]->attbyval = TypInfo[typeoid].byval;
608 : 5220 : attrtypes[attnum]->attalign = TypInfo[typeoid].align;
2015 609 : 5220 : attrtypes[attnum]->attstorage = TypInfo[typeoid].storage;
2011 610 : 5220 : attrtypes[attnum]->attcompression = InvalidCompressionMethod;
5772 peter_e@gmx.net 611 : 5220 : attrtypes[attnum]->attcollation = TypInfo[typeoid].collation;
612 : : /* if an array type, assume 1-dimensional attribute */
7914 tgl@sss.pgh.pa.us 613 [ + + ]: 5220 : if (TypInfo[typeoid].elem != InvalidOid &&
614 [ + + ]: 840 : attrtypes[attnum]->attlen < 0)
8362 615 : 660 : attrtypes[attnum]->attndims = 1;
616 : : else
617 : 4560 : attrtypes[attnum]->attndims = 0;
618 : : }
619 : :
620 : : /*
621 : : * If a system catalog column is collation-aware, force it to use C
622 : : * collation, so that its behavior is independent of the database's
623 : : * collation. This is essential to allow template0 to be cloned with a
624 : : * different database collation.
625 : : */
2902 626 [ + + ]: 37020 : if (OidIsValid(attrtypes[attnum]->attcollation))
627 : 6060 : attrtypes[attnum]->attcollation = C_COLLATION_OID;
628 : :
10521 bruce@momjian.us 629 : 37020 : attrtypes[attnum]->atttypmod = -1;
8833 tgl@sss.pgh.pa.us 630 : 37020 : attrtypes[attnum]->attislocal = true;
631 : :
4298 andres@anarazel.de 632 [ + + ]: 37020 : if (nullness == BOOTCOL_NULL_FORCE_NOT_NULL)
633 : : {
634 : 2100 : attrtypes[attnum]->attnotnull = true;
635 : : }
636 [ + + ]: 34920 : else if (nullness == BOOTCOL_NULL_FORCE_NULL)
637 : : {
638 : 240 : attrtypes[attnum]->attnotnull = false;
639 : : }
640 : : else
641 : : {
642 [ - + ]: 34680 : Assert(nullness == BOOTCOL_NULL_AUTO);
643 : :
644 : : /*
645 : : * Mark as "not null" if type is fixed-width and prior columns are
646 : : * likewise fixed-width and not-null. This corresponds to case where
647 : : * column can be accessed directly via C struct declaration.
648 : : */
2321 tgl@sss.pgh.pa.us 649 [ + + ]: 34680 : if (attrtypes[attnum]->attlen > 0)
650 : : {
651 : : int i;
652 : :
653 : : /* check earlier attributes */
4298 andres@anarazel.de 654 [ + + ]: 214140 : for (i = 0; i < attnum; i++)
655 : : {
2321 tgl@sss.pgh.pa.us 656 [ + + ]: 184620 : if (attrtypes[i]->attlen <= 0 ||
657 [ + - ]: 184440 : !attrtypes[i]->attnotnull)
658 : : break;
659 : : }
4298 andres@anarazel.de 660 [ + + ]: 29700 : if (i == attnum)
661 : 29520 : attrtypes[attnum]->attnotnull = true;
662 : : }
663 : : }
11099 scrappy@hub.org 664 : 37020 : }
665 : :
666 : :
667 : : /* ----------------
668 : : * InsertOneTuple
669 : : *
670 : : * If objectid is not zero, it is a specific OID to assign to the tuple.
671 : : * Otherwise, an OID will be assigned (if necessary) by heap_insert.
672 : : * ----------------
673 : : */
674 : : void
2930 andres@anarazel.de 675 : 652140 : InsertOneTuple(void)
676 : : {
677 : : HeapTuple tuple;
678 : : TupleDesc tupDesc;
679 : : int i;
680 : :
681 [ - + ]: 652140 : elog(DEBUG4, "inserting row with %d columns", numattr);
682 : :
683 : 652140 : tupDesc = CreateTupleDesc(numattr, attrtypes);
6600 tgl@sss.pgh.pa.us 684 : 652140 : tuple = heap_form_tuple(tupDesc, values, Nulls);
8897 bruce@momjian.us 685 : 652140 : pfree(tupDesc); /* just free's tupDesc, not the attrtypes */
686 : :
8957 tgl@sss.pgh.pa.us 687 : 652140 : simple_heap_insert(boot_reldesc, tuple);
9844 JanWieck@Yahoo.com 688 : 652140 : heap_freetuple(tuple);
8586 bruce@momjian.us 689 [ - + ]: 652140 : elog(DEBUG4, "row inserted");
690 : :
691 : : /*
692 : : * Reset null markers for next tuple
693 : : */
10674 694 [ + + ]: 10373760 : for (i = 0; i < numattr; i++)
6600 tgl@sss.pgh.pa.us 695 : 9721620 : Nulls[i] = false;
11099 scrappy@hub.org 696 : 652140 : }
697 : :
698 : : /* ----------------
699 : : * InsertOneValue
700 : : * ----------------
701 : : */
702 : : void
95 peter@eisentraut.org 703 :GNC 7803671 : InsertOneValue(const char *value, int i)
704 : : {
705 : : Form_pg_attribute attr;
706 : : Oid typoid;
707 : : int16 typlen;
708 : : bool typbyval;
709 : : char typalign;
710 : : char typdelim;
711 : : Oid typioparam;
712 : : Oid typinput;
713 : : Oid typoutput;
714 : : Oid typcollation;
715 : :
1492 peter@eisentraut.org 716 [ + - - + ]:CBC 7803671 : Assert(i >= 0 && i < MAXATTR);
717 : :
8530 tgl@sss.pgh.pa.us 718 [ - + ]: 7803671 : elog(DEBUG4, "inserting column %d value \"%s\"", i, value);
719 : :
283 720 : 7803671 : attr = TupleDescAttr(RelationGetDescr(boot_reldesc), i);
721 : 7803671 : typoid = attr->atttypid;
722 : :
7410 723 : 7803671 : boot_get_type_io_data(typoid,
724 : : &typlen, &typbyval, &typalign,
725 : : &typdelim, &typioparam,
726 : : &typinput, &typoutput,
727 : : &typcollation);
728 : :
729 : : /*
730 : : * pg_node_tree values can't be inserted normally (pg_node_tree_in would
731 : : * just error out), so provide special cases for such columns that we
732 : : * would like to fill during bootstrap.
733 : : */
283 734 [ + + ]: 7803671 : if (typoid == PG_NODE_TREEOID)
735 : : {
736 : : /* pg_proc.proargdefaults */
737 [ + - + - ]: 2520 : if (RelationGetRelid(boot_reldesc) == ProcedureRelationId &&
738 : : i == Anum_pg_proc_proargdefaults - 1)
739 : 2520 : InsertOneProargdefaultsValue(value);
740 : : else /* maybe other cases later */
283 tgl@sss.pgh.pa.us 741 [ # # ]:UBC 0 : elog(ERROR, "can't handle pg_node_tree input for %s.%s",
742 : : RelationGetRelationName(boot_reldesc),
743 : : NameStr(attr->attname));
744 : : }
745 : : else
746 : : {
747 : : /* Normal case */
283 tgl@sss.pgh.pa.us 748 :CBC 7801151 : values[i] = OidInputFunctionCall(typinput, value, typioparam, -1);
749 : : }
750 : :
751 : : /*
752 : : * We use ereport not elog here so that parameters aren't evaluated unless
753 : : * the message is going to be printed, which generally it isn't
754 : : */
4773 755 [ - + ]: 7803671 : ereport(DEBUG4,
756 : : (errmsg_internal("inserted -> %s",
757 : : OidOutputFunctionCall(typoutput, values[i]))));
11099 scrappy@hub.org 758 : 7803671 : }
759 : :
760 : : /* ----------------
761 : : * InsertOneProargdefaultsValue
762 : : *
763 : : * In general, proargdefaults can be a list of any expressions, but
764 : : * for bootstrap we only support a list of Const nodes. The input
765 : : * has the form of a text array, and we feed non-null elements to the
766 : : * typinput functions for the appropriate parameters.
767 : : * ----------------
768 : : */
769 : : static void
95 peter@eisentraut.org 770 :GNC 2520 : InsertOneProargdefaultsValue(const char *value)
771 : : {
772 : : int pronargs;
773 : : oidvector *proargtypes;
774 : : Datum arrayval;
775 : : Datum *array_datums;
776 : : bool *array_nulls;
777 : : int array_count;
778 : : List *proargdefaults;
779 : : char *nodestring;
780 : :
781 : : /* The pg_proc columns we need to use must have been filled already */
782 : : StaticAssertDecl(Anum_pg_proc_pronargs < Anum_pg_proc_proargdefaults,
783 : : "pronargs must come before proargdefaults");
784 : : StaticAssertDecl(Anum_pg_proc_pronargdefaults < Anum_pg_proc_proargdefaults,
785 : : "pronargdefaults must come before proargdefaults");
786 : : StaticAssertDecl(Anum_pg_proc_proargtypes < Anum_pg_proc_proargdefaults,
787 : : "proargtypes must come before proargdefaults");
283 tgl@sss.pgh.pa.us 788 [ - + ]:CBC 2520 : if (Nulls[Anum_pg_proc_pronargs - 1])
283 tgl@sss.pgh.pa.us 789 [ # # ]:UBC 0 : elog(ERROR, "pronargs must not be null");
283 tgl@sss.pgh.pa.us 790 [ - + ]:CBC 2520 : if (Nulls[Anum_pg_proc_proargtypes - 1])
283 tgl@sss.pgh.pa.us 791 [ # # ]:UBC 0 : elog(ERROR, "proargtypes must not be null");
283 tgl@sss.pgh.pa.us 792 :CBC 2520 : pronargs = DatumGetInt16(values[Anum_pg_proc_pronargs - 1]);
793 : 2520 : proargtypes = DatumGetPointer(values[Anum_pg_proc_proargtypes - 1]);
794 [ - + ]: 2520 : Assert(pronargs == proargtypes->dim1);
795 : :
796 : : /* Parse the input string as an array value, then deconstruct to Datums */
797 : 2520 : arrayval = OidFunctionCall3(F_ARRAY_IN,
798 : : CStringGetDatum(value),
799 : : ObjectIdGetDatum(CSTRINGOID),
800 : : Int32GetDatum(-1));
801 : 2520 : deconstruct_array_builtin(DatumGetArrayTypeP(arrayval), CSTRINGOID,
802 : : &array_datums, &array_nulls, &array_count);
803 : :
804 : : /* The values should correspond to the last N argtypes */
805 [ - + ]: 2520 : if (array_count > pronargs)
283 tgl@sss.pgh.pa.us 806 [ # # ]:UBC 0 : elog(ERROR, "too many proargdefaults entries");
807 : :
808 : : /* Build the List of Const nodes */
283 tgl@sss.pgh.pa.us 809 :CBC 2520 : proargdefaults = NIL;
810 [ + + ]: 6720 : for (int i = 0; i < array_count; i++)
811 : : {
812 : 4200 : Oid argtype = proargtypes->values[pronargs - array_count + i];
813 : : int16 typlen;
814 : : bool typbyval;
815 : : char typalign;
816 : : char typdelim;
817 : : Oid typioparam;
818 : : Oid typinput;
819 : : Oid typoutput;
820 : : Oid typcollation;
821 : : Datum defval;
822 : : bool defnull;
823 : : Const *defConst;
824 : :
825 : 4200 : boot_get_type_io_data(argtype,
826 : : &typlen, &typbyval, &typalign,
827 : : &typdelim, &typioparam,
828 : : &typinput, &typoutput,
829 : : &typcollation);
830 : :
831 : 4200 : defnull = array_nulls[i];
832 [ + + ]: 4200 : if (defnull)
833 : 120 : defval = (Datum) 0;
834 : : else
835 : 4080 : defval = OidInputFunctionCall(typinput,
836 : 4080 : DatumGetCString(array_datums[i]),
837 : : typioparam, -1);
838 : :
839 : 4200 : defConst = makeConst(argtype,
840 : : -1, /* never any typmod */
841 : : typcollation,
842 : : typlen,
843 : : defval,
844 : : defnull,
845 : : typbyval);
846 : 4200 : proargdefaults = lappend(proargdefaults, defConst);
847 : : }
848 : :
849 : : /*
850 : : * Flatten the List to a node-tree string, then convert to a text datum,
851 : : * which is the storage representation of pg_node_tree.
852 : : */
853 : 2520 : nodestring = nodeToString(proargdefaults);
854 : 2520 : values[Anum_pg_proc_proargdefaults - 1] = CStringGetTextDatum(nodestring);
855 : 2520 : Nulls[Anum_pg_proc_proargdefaults - 1] = false;
856 : :
857 : : /*
858 : : * Hack: fill in pronargdefaults with the right value. This is surely
859 : : * ugly, but it beats making the programmer do it.
860 : : */
861 : 2520 : values[Anum_pg_proc_pronargdefaults - 1] = Int16GetDatum(array_count);
862 : 2520 : Nulls[Anum_pg_proc_pronargdefaults - 1] = false;
863 : 2520 : }
864 : :
865 : : /* ----------------
866 : : * InsertOneNull
867 : : * ----------------
868 : : */
869 : : void
11099 scrappy@hub.org 870 : 1917949 : InsertOneNull(int i)
871 : : {
8586 bruce@momjian.us 872 [ - + ]: 1917949 : elog(DEBUG4, "inserting column %d NULL", i);
5562 peter_e@gmx.net 873 [ + - - + ]: 1917949 : Assert(i >= 0 && i < MAXATTR);
3387 andres@anarazel.de 874 [ - + ]: 1917949 : if (TupleDescAttr(boot_reldesc->rd_att, i)->attnotnull)
3455 tgl@sss.pgh.pa.us 875 [ # # ]:UBC 0 : elog(ERROR,
876 : : "NULL value specified for not-null column \"%s\" of relation \"%s\"",
877 : : NameStr(TupleDescAttr(boot_reldesc->rd_att, i)->attname),
878 : : RelationGetRelationName(boot_reldesc));
9678 tgl@sss.pgh.pa.us 879 :CBC 1917949 : values[i] = PointerGetDatum(NULL);
6600 880 : 1917949 : Nulls[i] = true;
11099 scrappy@hub.org 881 : 1917949 : }
882 : :
883 : : /* ----------------
884 : : * cleanup
885 : : * ----------------
886 : : */
887 : : static void
8585 tgl@sss.pgh.pa.us 888 : 60 : cleanup(void)
889 : : {
8884 890 [ - + ]: 60 : if (boot_reldesc != NULL)
8884 tgl@sss.pgh.pa.us 891 :UBC 0 : closerel(NULL);
11099 scrappy@hub.org 892 :CBC 60 : }
893 : :
894 : : /* ----------------
895 : : * populate_typ_list
896 : : *
897 : : * Load the Typ list by reading pg_type.
898 : : * ----------------
899 : : */
900 : : static void
2075 tomas.vondra@postgre 901 : 120 : populate_typ_list(void)
902 : : {
903 : : Relation rel;
904 : : TableScanDesc scan;
905 : : HeapTuple tup;
906 : : MemoryContext old;
907 : :
908 [ - + ]: 120 : Assert(Typ == NIL);
909 : :
2275 tgl@sss.pgh.pa.us 910 : 120 : rel = table_open(TypeRelationId, NoLock);
911 : 120 : scan = table_beginscan_catalog(rel, 0, NULL);
2075 tomas.vondra@postgre 912 : 120 : old = MemoryContextSwitchTo(TopMemoryContext);
2275 tgl@sss.pgh.pa.us 913 [ + + ]: 25680 : while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
914 : : {
915 : 25560 : Form_pg_type typForm = (Form_pg_type) GETSTRUCT(tup);
916 : : struct typmap *newtyp;
917 : :
353 michael@paquier.xyz 918 : 25560 : newtyp = palloc_object(struct typmap);
2075 tomas.vondra@postgre 919 : 25560 : Typ = lappend(Typ, newtyp);
920 : :
921 : 25560 : newtyp->am_oid = typForm->oid;
922 : 25560 : memcpy(&newtyp->am_typ, typForm, sizeof(newtyp->am_typ));
923 : : }
924 : 120 : MemoryContextSwitchTo(old);
2275 tgl@sss.pgh.pa.us 925 : 120 : table_endscan(scan);
926 : 120 : table_close(rel, NoLock);
927 : 120 : }
928 : :
929 : : /* ----------------
930 : : * gettype
931 : : *
932 : : * NB: this is really ugly; it will return an integer index into TypInfo[],
933 : : * and not an OID at all, until the first reference to a type not known in
934 : : * TypInfo[]. At that point it will read and cache pg_type in Typ,
935 : : * and subsequently return a real OID (and set the global pointer Ap to
936 : : * point at the found row in Typ). So caller must check whether Typ is
937 : : * still NIL to determine what the return value is!
938 : : * ----------------
939 : : */
940 : : static Oid
11099 scrappy@hub.org 941 : 37080 : gettype(char *type)
942 : : {
2075 tomas.vondra@postgre 943 [ + + ]: 37080 : if (Typ != NIL)
944 : : {
945 : : ListCell *lc;
946 : :
2026 tgl@sss.pgh.pa.us 947 [ + - + + : 590760 : foreach(lc, Typ)
+ + ]
948 : : {
2075 tomas.vondra@postgre 949 : 590700 : struct typmap *app = lfirst(lc);
950 : :
951 [ + + ]: 590700 : if (strncmp(NameStr(app->am_typ.typname), type, NAMEDATALEN) == 0)
952 : : {
953 : 31740 : Ap = app;
954 : 31740 : return app->am_oid;
955 : : }
956 : : }
957 : :
958 : : /*
959 : : * The type wasn't known; reload the pg_type contents and check again
960 : : * to handle composite types, added since last populating the list.
961 : : */
962 : :
963 : 60 : list_free_deep(Typ);
964 : 60 : Typ = NIL;
965 : 60 : populate_typ_list();
966 : :
967 : : /*
968 : : * Calling gettype would result in infinite recursion for types
969 : : * missing in pg_type, so just repeat the lookup.
970 : : */
2026 tgl@sss.pgh.pa.us 971 [ + - + - : 13620 : foreach(lc, Typ)
+ - ]
972 : : {
2075 tomas.vondra@postgre 973 : 13620 : struct typmap *app = lfirst(lc);
974 : :
975 [ + + ]: 13620 : if (strncmp(NameStr(app->am_typ.typname), type, NAMEDATALEN) == 0)
976 : : {
977 : 60 : Ap = app;
978 : 60 : return app->am_oid;
979 : : }
980 : : }
981 : : }
982 : : else
983 : : {
984 : : int i;
985 : :
8983 tgl@sss.pgh.pa.us 986 [ + + ]: 51780 : for (i = 0; i < n_types; i++)
987 : : {
8276 988 [ + + ]: 51720 : if (strncmp(type, TypInfo[i].name, NAMEDATALEN) == 0)
10315 bruce@momjian.us 989 : 5220 : return i;
990 : : }
991 : : /* Not in TypInfo, so we'd better be able to read pg_type now */
8586 992 [ - + ]: 60 : elog(DEBUG4, "external type: %s", type);
2075 tomas.vondra@postgre 993 : 60 : populate_typ_list();
10315 bruce@momjian.us 994 : 60 : return gettype(type);
995 : : }
8530 tgl@sss.pgh.pa.us 996 [ # # ]:UBC 0 : elog(ERROR, "unrecognized type \"%s\"", type);
997 : : /* not reached, here to make compiler happy */
998 : : return 0;
999 : : }
1000 : :
1001 : : /* ----------------
1002 : : * boot_get_type_io_data
1003 : : *
1004 : : * Obtain type I/O information at bootstrap time. This intentionally has
1005 : : * an API very close to that of lsyscache.c's get_type_io_data, except that
1006 : : * we only support obtaining the typinput and typoutput routines, not
1007 : : * the binary I/O routines, and we also return the type's collation.
1008 : : * This is exported so that array_in and array_out can be made to work
1009 : : * during early bootstrap.
1010 : : * ----------------
1011 : : */
1012 : : void
7410 tgl@sss.pgh.pa.us 1013 :CBC 7847111 : boot_get_type_io_data(Oid typid,
1014 : : int16 *typlen,
1015 : : bool *typbyval,
1016 : : char *typalign,
1017 : : char *typdelim,
1018 : : Oid *typioparam,
1019 : : Oid *typinput,
1020 : : Oid *typoutput,
1021 : : Oid *typcollation)
1022 : : {
2075 tomas.vondra@postgre 1023 [ + + ]: 7847111 : if (Typ != NIL)
1024 : : {
1025 : : /* We have the boot-time contents of pg_type, so use it */
1026 : 3082991 : struct typmap *ap = NULL;
1027 : : ListCell *lc;
1028 : :
2026 tgl@sss.pgh.pa.us 1029 [ + - + - : 27830510 : foreach(lc, Typ)
+ - ]
1030 : : {
2075 tomas.vondra@postgre 1031 : 27830510 : ap = lfirst(lc);
1032 [ + + ]: 27830510 : if (ap->am_oid == typid)
1033 : 3082991 : break;
1034 : : }
1035 : :
1036 [ + - - + ]: 3082991 : if (!ap || ap->am_oid != typid)
7410 tgl@sss.pgh.pa.us 1037 [ # # ]:UBC 0 : elog(ERROR, "type OID %u not found in Typ list", typid);
1038 : :
7410 tgl@sss.pgh.pa.us 1039 :CBC 3082991 : *typlen = ap->am_typ.typlen;
1040 : 3082991 : *typbyval = ap->am_typ.typbyval;
1041 : 3082991 : *typalign = ap->am_typ.typalign;
1042 : 3082991 : *typdelim = ap->am_typ.typdelim;
1043 : :
1044 : : /* XXX this logic must match getTypeIOParam() */
1045 [ + + ]: 3082991 : if (OidIsValid(ap->am_typ.typelem))
1046 : 85680 : *typioparam = ap->am_typ.typelem;
1047 : : else
1048 : 2997311 : *typioparam = typid;
1049 : :
1050 : 3082991 : *typinput = ap->am_typ.typinput;
1051 : 3082991 : *typoutput = ap->am_typ.typoutput;
1052 : :
283 1053 : 3082991 : *typcollation = ap->am_typ.typcollation;
1054 : : }
1055 : : else
1056 : : {
1057 : : /* We don't have pg_type yet, so use the hard-wired TypInfo array */
1058 : : int typeindex;
1059 : :
7410 1060 [ + - ]: 40353240 : for (typeindex = 0; typeindex < n_types; typeindex++)
1061 : : {
1062 [ + + ]: 40353240 : if (TypInfo[typeindex].oid == typid)
1063 : 4764120 : break;
1064 : : }
1065 [ - + ]: 4764120 : if (typeindex >= n_types)
7410 tgl@sss.pgh.pa.us 1066 [ # # ]:UBC 0 : elog(ERROR, "type OID %u not found in TypInfo", typid);
1067 : :
7410 tgl@sss.pgh.pa.us 1068 :CBC 4764120 : *typlen = TypInfo[typeindex].len;
1069 : 4764120 : *typbyval = TypInfo[typeindex].byval;
1070 : 4764120 : *typalign = TypInfo[typeindex].align;
1071 : : /* We assume typdelim is ',' for all boot-time types */
1072 : 4764120 : *typdelim = ',';
1073 : :
1074 : : /* XXX this logic must match getTypeIOParam() */
1075 [ + + ]: 4764120 : if (OidIsValid(TypInfo[typeindex].elem))
1076 : 461460 : *typioparam = TypInfo[typeindex].elem;
1077 : : else
1078 : 4302660 : *typioparam = typid;
1079 : :
1080 : 4764120 : *typinput = TypInfo[typeindex].inproc;
1081 : 4764120 : *typoutput = TypInfo[typeindex].outproc;
1082 : :
283 1083 : 4764120 : *typcollation = TypInfo[typeindex].collation;
1084 : : }
7410 1085 : 7847111 : }
1086 : :
1087 : : /* ----------------
1088 : : * boot_get_role_oid
1089 : : *
1090 : : * Look up a role name at bootstrap time. This is equivalent to
1091 : : * get_role_oid(rolname, true): return the role OID or InvalidOid if
1092 : : * not found. We only need to cope with built-in role names.
1093 : : * ----------------
1094 : : */
1095 : : Oid
268 1096 : 5160 : boot_get_role_oid(const char *rolname)
1097 : : {
140 peter@eisentraut.org 1098 [ + - ]:GNC 9600 : for (size_t i = 0; i < lengthof(RolInfo); i++)
1099 : : {
268 tgl@sss.pgh.pa.us 1100 [ + + ]:CBC 9600 : if (strcmp(RolInfo[i].rolname, rolname) == 0)
1101 : 5160 : return RolInfo[i].oid;
1102 : : }
268 tgl@sss.pgh.pa.us 1103 :UBC 0 : return InvalidOid;
1104 : : }
1105 : :
1106 : : /* ----------------
1107 : : * AllocateAttribute
1108 : : *
1109 : : * Note: bootstrap never sets any per-column ACLs, so we only need
1110 : : * ATTRIBUTE_FIXED_PART_SIZE space per attribute.
1111 : : * ----------------
1112 : : */
1113 : : static Form_pg_attribute
8884 tgl@sss.pgh.pa.us 1114 :CBC 2040 : AllocateAttribute(void)
1115 : : {
3742 1116 : 2040 : return (Form_pg_attribute)
1117 : 2040 : MemoryContextAllocZero(TopMemoryContext, ATTRIBUTE_FIXED_PART_SIZE);
1118 : : }
1119 : :
1120 : : /*
1121 : : * index_register() -- record an index that has been set up for building
1122 : : * later.
1123 : : *
1124 : : * At bootstrap time, we define a bunch of indexes on system catalogs.
1125 : : * We postpone actually building the indexes until just before we're
1126 : : * finished with initialization, however. This is because the indexes
1127 : : * themselves have catalog entries, and those have to be included in the
1128 : : * indexes on those catalogs. Doing it in two phases is the simplest
1129 : : * way of making sure the indexes have the right contents at the end.
1130 : : */
1131 : : void
9013 1132 : 9600 : index_register(Oid heap,
1133 : : Oid ind,
1134 : : const IndexInfo *indexInfo)
1135 : : {
1136 : : IndexList *newind;
1137 : : MemoryContext oldcxt;
1138 : :
1139 : : /*
1140 : : * XXX mao 10/31/92 -- don't gc index reldescs, associated info at
1141 : : * bootstrap time. we'll declare the indexes now, but want to create them
1142 : : * later.
1143 : : */
1144 : :
9649 1145 [ + + ]: 9600 : if (nogc == NULL)
8361 neilc@samurai.com 1146 : 60 : nogc = AllocSetContextCreate(NULL,
1147 : : "BootstrapNoGC",
1148 : : ALLOCSET_DEFAULT_SIZES);
1149 : :
9649 tgl@sss.pgh.pa.us 1150 : 9600 : oldcxt = MemoryContextSwitchTo(nogc);
1151 : :
353 michael@paquier.xyz 1152 : 9600 : newind = palloc_object(IndexList);
9013 tgl@sss.pgh.pa.us 1153 : 9600 : newind->il_heap = heap;
1154 : 9600 : newind->il_ind = ind;
353 michael@paquier.xyz 1155 : 9600 : newind->il_info = palloc_object(IndexInfo);
1156 : :
9633 tgl@sss.pgh.pa.us 1157 : 9600 : memcpy(newind->il_info, indexInfo, sizeof(IndexInfo));
1158 : : /* expressions will likely be null, but may as well copy it */
3551 peter_e@gmx.net 1159 : 19200 : newind->il_info->ii_Expressions =
8585 tgl@sss.pgh.pa.us 1160 : 9600 : copyObject(indexInfo->ii_Expressions);
1161 : 9600 : newind->il_info->ii_ExpressionsState = NIL;
1162 : : /* predicate will likely be null, but may as well copy it */
3551 peter_e@gmx.net 1163 : 19200 : newind->il_info->ii_Predicate =
9266 tgl@sss.pgh.pa.us 1164 : 9600 : copyObject(indexInfo->ii_Predicate);
3546 andres@anarazel.de 1165 : 9600 : newind->il_info->ii_PredicateState = NULL;
1166 : : /* no exclusion constraints at bootstrap time, so no need to copy */
6200 tgl@sss.pgh.pa.us 1167 [ - + ]: 9600 : Assert(indexInfo->ii_ExclusionOps == NULL);
1168 [ - + ]: 9600 : Assert(indexInfo->ii_ExclusionProcs == NULL);
1169 [ - + ]: 9600 : Assert(indexInfo->ii_ExclusionStrats == NULL);
1170 : :
10674 bruce@momjian.us 1171 : 9600 : newind->il_next = ILHead;
1172 : 9600 : ILHead = newind;
1173 : :
1174 : 9600 : MemoryContextSwitchTo(oldcxt);
11099 scrappy@hub.org 1175 : 9600 : }
1176 : :
1177 : :
1178 : : /*
1179 : : * build_indices -- fill in all the indexes registered earlier
1180 : : */
1181 : : void
8361 neilc@samurai.com 1182 : 60 : build_indices(void)
1183 : : {
1184 [ + + ]: 9660 : for (; ILHead != NULL; ILHead = ILHead->il_next)
1185 : : {
1186 : : Relation heap;
1187 : : Relation ind;
1188 : :
1189 : : /* need not bother with locks during bootstrap */
2868 andres@anarazel.de 1190 : 9600 : heap = table_open(ILHead->il_heap, NoLock);
7425 tgl@sss.pgh.pa.us 1191 : 9600 : ind = index_open(ILHead->il_ind, NoLock);
1192 : :
237 alvherre@kurilemu.de 1193 : 9600 : index_build(heap, ind, ILHead->il_info, false, false, false);
1194 : :
7425 tgl@sss.pgh.pa.us 1195 : 9600 : index_close(ind, NoLock);
2868 andres@anarazel.de 1196 : 9600 : table_close(heap, NoLock);
1197 : : }
11099 scrappy@hub.org 1198 : 60 : }
|