PostgreSQL Source Code: src/backend/bootstrap/bootstrap.c Source File

PostgreSQL Source Code git master
bootstrap.c
Go to the documentation of this file.
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-2025, 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_collation.h"
29#include "catalog/pg_type.h"
30#include "common/link-canary.h"
31#include "miscadmin.h"
32#include "nodes/makefuncs.h"
33#include "pg_getopt.h"
34#include "postmaster/postmaster.h"
35#include "storage/bufpage.h"
36#include "storage/ipc.h"
37#include "storage/proc.h"
38#include "utils/builtins.h"
39#include "utils/fmgroids.h"
40#include "utils/guc.h"
41#include "utils/memutils.h"
42#include "utils/rel.h"
43#include "utils/relmapper.h"
44
45
46static void CheckerModeMain(void);
47static void bootstrap_signals(void);
48static Form_pg_attribute AllocateAttribute(void);
49static void populate_typ_list(void);
50static Oid gettype(char *type);
51static void cleanup(void);
52
53/* ----------------
54 * global variables
55 * ----------------
56 */
57
58 Relation boot_reldesc; /* current relation descriptor */
59
60 Form_pg_attribute attrtypes[MAXATTR]; /* points to attribute info */
61 int numattr; /* number of attributes for cur. rel */
62
63
64/*
65 * Basic information associated with each type. This is used before
66 * pg_type is filled, so it has to cover the datatypes used as column types
67 * in the core "bootstrapped" catalogs.
68 *
69 * XXX several of these input/output functions do catalog scans
70 * (e.g., F_REGPROCIN scans pg_proc). this obviously creates some
71 * order dependencies in the catalog creation process.
72 */
73 struct typinfo
74{
75 char name[NAMEDATALEN];
76 Oid oid;
77 Oid elem;
78 int16 len;
79 bool byval;
80 char align;
81 char storage;
82 Oid collation;
83 Oid inproc;
84 Oid outproc;
85};
86
87 static const struct typinfo TypInfo[] = {
88 {"bool", BOOLOID, 0, 1, true, TYPALIGN_CHAR, TYPSTORAGE_PLAIN, InvalidOid,
89 F_BOOLIN, F_BOOLOUT},
90 {"bytea", BYTEAOID, 0, -1, false, TYPALIGN_INT, TYPSTORAGE_EXTENDED, InvalidOid,
91 F_BYTEAIN, F_BYTEAOUT},
92 {"char", CHAROID, 0, 1, true, TYPALIGN_CHAR, TYPSTORAGE_PLAIN, InvalidOid,
93 F_CHARIN, F_CHAROUT},
94 {"int2", INT2OID, 0, 2, true, TYPALIGN_SHORT, TYPSTORAGE_PLAIN, InvalidOid,
95 F_INT2IN, F_INT2OUT},
96 {"int4", INT4OID, 0, 4, true, TYPALIGN_INT, TYPSTORAGE_PLAIN, InvalidOid,
97 F_INT4IN, F_INT4OUT},
98 {"float4", FLOAT4OID, 0, 4, true, TYPALIGN_INT, TYPSTORAGE_PLAIN, InvalidOid,
99 F_FLOAT4IN, F_FLOAT4OUT},
100 {"name", NAMEOID, CHAROID, NAMEDATALEN, false, TYPALIGN_CHAR, TYPSTORAGE_PLAIN, C_COLLATION_OID,
101 F_NAMEIN, F_NAMEOUT},
102 {"regclass", REGCLASSOID, 0, 4, true, TYPALIGN_INT, TYPSTORAGE_PLAIN, InvalidOid,
103 F_REGCLASSIN, F_REGCLASSOUT},
104 {"regproc", REGPROCOID, 0, 4, true, TYPALIGN_INT, TYPSTORAGE_PLAIN, InvalidOid,
105 F_REGPROCIN, F_REGPROCOUT},
106 {"regtype", REGTYPEOID, 0, 4, true, TYPALIGN_INT, TYPSTORAGE_PLAIN, InvalidOid,
107 F_REGTYPEIN, F_REGTYPEOUT},
108 {"regrole", REGROLEOID, 0, 4, true, TYPALIGN_INT, TYPSTORAGE_PLAIN, InvalidOid,
109 F_REGROLEIN, F_REGROLEOUT},
110 {"regnamespace", REGNAMESPACEOID, 0, 4, true, TYPALIGN_INT, TYPSTORAGE_PLAIN, InvalidOid,
111 F_REGNAMESPACEIN, F_REGNAMESPACEOUT},
112 {"regdatabase", REGDATABASEOID, 0, 4, true, TYPALIGN_INT, TYPSTORAGE_PLAIN, InvalidOid,
113 F_REGDATABASEIN, F_REGDATABASEOUT},
114 {"text", TEXTOID, 0, -1, false, TYPALIGN_INT, TYPSTORAGE_EXTENDED, DEFAULT_COLLATION_OID,
115 F_TEXTIN, F_TEXTOUT},
116 {"oid", OIDOID, 0, 4, true, TYPALIGN_INT, TYPSTORAGE_PLAIN, InvalidOid,
117 F_OIDIN, F_OIDOUT},
118 {"tid", TIDOID, 0, 6, false, TYPALIGN_SHORT, TYPSTORAGE_PLAIN, InvalidOid,
119 F_TIDIN, F_TIDOUT},
120 {"xid", XIDOID, 0, 4, true, TYPALIGN_INT, TYPSTORAGE_PLAIN, InvalidOid,
121 F_XIDIN, F_XIDOUT},
122 {"cid", CIDOID, 0, 4, true, TYPALIGN_INT, TYPSTORAGE_PLAIN, InvalidOid,
123 F_CIDIN, F_CIDOUT},
124 {"pg_node_tree", PG_NODE_TREEOID, 0, -1, false, TYPALIGN_INT, TYPSTORAGE_EXTENDED, DEFAULT_COLLATION_OID,
125 F_PG_NODE_TREE_IN, F_PG_NODE_TREE_OUT},
126 {"int2vector", INT2VECTOROID, INT2OID, -1, false, TYPALIGN_INT, TYPSTORAGE_PLAIN, InvalidOid,
127 F_INT2VECTORIN, F_INT2VECTOROUT},
128 {"oidvector", OIDVECTOROID, OIDOID, -1, false, TYPALIGN_INT, TYPSTORAGE_PLAIN, InvalidOid,
129 F_OIDVECTORIN, F_OIDVECTOROUT},
130 {"_int4", INT4ARRAYOID, INT4OID, -1, false, TYPALIGN_INT, TYPSTORAGE_EXTENDED, InvalidOid,
131 F_ARRAY_IN, F_ARRAY_OUT},
132 {"_text", 1009, TEXTOID, -1, false, TYPALIGN_INT, TYPSTORAGE_EXTENDED, DEFAULT_COLLATION_OID,
133 F_ARRAY_IN, F_ARRAY_OUT},
134 {"_oid", 1028, OIDOID, -1, false, TYPALIGN_INT, TYPSTORAGE_EXTENDED, InvalidOid,
135 F_ARRAY_IN, F_ARRAY_OUT},
136 {"_char", 1002, CHAROID, -1, false, TYPALIGN_INT, TYPSTORAGE_EXTENDED, InvalidOid,
137 F_ARRAY_IN, F_ARRAY_OUT},
138 {"_aclitem", 1034, ACLITEMOID, -1, false, TYPALIGN_INT, TYPSTORAGE_EXTENDED, InvalidOid,
139 F_ARRAY_IN, F_ARRAY_OUT}
140};
141
142 static const int n_types = sizeof(TypInfo) / sizeof(struct typinfo);
143
144 struct typmap
145{ /* a hack */
146 Oid am_oid;
147 FormData_pg_type am_typ;
148};
149
150 static List *Typ = NIL; /* List of struct typmap* */
151 static struct typmap *Ap = NULL;
152
153 static Datum values[MAXATTR]; /* current row's attribute values */
154 static bool Nulls[MAXATTR];
155
156 static MemoryContext nogc = NULL; /* special no-gc mem context */
157
158/*
159 * At bootstrap time, we first declare all the indices to be built, and
160 * then build them. The IndexList structure stores enough information
161 * to allow us to build the indices after they've been declared.
162 */
163
164 typedef struct _IndexList
165{
166 Oid il_heap;
167 Oid il_ind;
168 IndexInfo *il_info;
169 struct _IndexList *il_next;
170 } IndexList;
171
172 static IndexList *ILHead = NULL;
173
174
175/*
176 * In shared memory checker mode, all we really want to do is create shared
177 * memory and semaphores (just to prove we can do it with the current GUC
178 * settings). Since, in fact, that was already done by
179 * CreateSharedMemoryAndSemaphores(), we have nothing more to do here.
180 */
181static void
182 CheckerModeMain(void)
183{
184 proc_exit(0);
185}
186
187/*
188 * The main entry point for running the backend in bootstrap mode
189 *
190 * The bootstrap mode is used to initialize the template database.
191 * The bootstrap backend doesn't speak SQL, but instead expects
192 * commands in a special bootstrap language.
193 *
194 * When check_only is true, startup is done only far enough to verify that
195 * the current configuration, particularly the passed in options pertaining
196 * to shared memory sizing, options work (or at least do not cause an error
197 * up to shared memory creation).
198 */
199void
200 BootstrapModeMain(int argc, char *argv[], bool check_only)
201{
202 int i;
203 char *progname = argv[0];
204 int flag;
205 char *userDoption = NULL;
206 uint32 bootstrap_data_checksum_version = 0; /* No checksum */
207 yyscan_t scanner;
208
209 Assert(!IsUnderPostmaster);
210
211 InitStandaloneProcess(argv[0]);
212
213 /* Set defaults, to be overridden by explicit options below */
214 InitializeGUCOptions();
215
216 /* an initial --boot or --check should be present */
217 Assert(argc > 1
218 && (strcmp(argv[1], "--boot") == 0
219 || strcmp(argv[1], "--check") == 0));
220 argv++;
221 argc--;
222
223 while ((flag = getopt(argc, argv, "B:c:d:D:Fkr:X:-:")) != -1)
224 {
225 switch (flag)
226 {
227 case 'B':
228 SetConfigOption("shared_buffers", optarg, PGC_POSTMASTER, PGC_S_ARGV);
229 break;
230 case '-':
231
232 /*
233 * Error if the user misplaced a special must-be-first option
234 * for dispatching to a subprogram. parse_dispatch_option()
235 * returns DISPATCH_POSTMASTER if it doesn't find a match, so
236 * error for anything else.
237 */
238 if (parse_dispatch_option(optarg) != DISPATCH_POSTMASTER)
239 ereport(ERROR,
240 (errcode(ERRCODE_SYNTAX_ERROR),
241 errmsg("--%s must be first argument", optarg)));
242
243 /* FALLTHROUGH */
244 case 'c':
245 {
246 char *name,
247 *value;
248
249 ParseLongOption(optarg, &name, &value);
250 if (!value)
251 {
252 if (flag == '-')
253 ereport(ERROR,
254 (errcode(ERRCODE_SYNTAX_ERROR),
255 errmsg("--%s requires a value",
256 optarg)));
257 else
258 ereport(ERROR,
259 (errcode(ERRCODE_SYNTAX_ERROR),
260 errmsg("-c %s requires a value",
261 optarg)));
262 }
263
264 SetConfigOption(name, value, PGC_POSTMASTER, PGC_S_ARGV);
265 pfree(name);
266 pfree(value);
267 break;
268 }
269 case 'D':
270 userDoption = pstrdup(optarg);
271 break;
272 case 'd':
273 {
274 /* Turn on debugging for the bootstrap process. */
275 char *debugstr;
276
277 debugstr = psprintf("debug%s", optarg);
278 SetConfigOption("log_min_messages", debugstr,
279 PGC_POSTMASTER, PGC_S_ARGV);
280 SetConfigOption("client_min_messages", debugstr,
281 PGC_POSTMASTER, PGC_S_ARGV);
282 pfree(debugstr);
283 }
284 break;
285 case 'F':
286 SetConfigOption("fsync", "false", PGC_POSTMASTER, PGC_S_ARGV);
287 break;
288 case 'k':
289 bootstrap_data_checksum_version = PG_DATA_CHECKSUM_VERSION;
290 break;
291 case 'r':
292 strlcpy(OutputFileName, optarg, MAXPGPATH);
293 break;
294 case 'X':
295 SetConfigOption("wal_segment_size", optarg, PGC_INTERNAL, PGC_S_DYNAMIC_DEFAULT);
296 break;
297 default:
298 write_stderr("Try \"%s --help\" for more information.\n",
299 progname);
300 proc_exit(1);
301 break;
302 }
303 }
304
305 if (argc != optind)
306 {
307 write_stderr("%s: invalid command-line arguments\n", progname);
308 proc_exit(1);
309 }
310
311 /* Acquire configuration parameters */
312 if (!SelectConfigFiles(userDoption, progname))
313 proc_exit(1);
314
315 /*
316 * Validate we have been given a reasonable-looking DataDir and change
317 * into it
318 */
319 checkDataDir();
320 ChangeToDataDir();
321
322 CreateDataDirLockFile(false);
323
324 SetProcessingMode(BootstrapProcessing);
325 IgnoreSystemIndexes = true;
326
327 InitializeMaxBackends();
328
329 /*
330 * Even though bootstrapping runs in single-process mode, initialize
331 * postmaster child slots array so that --check can detect running out of
332 * shared memory or other resources if max_connections is set too high.
333 */
334 InitPostmasterChildSlots();
335
336 InitializeFastPathLocks();
337
338 CreateSharedMemoryAndSemaphores();
339
340 /*
341 * Estimate number of openable files. This is essential too in --check
342 * mode, because on some platforms semaphores count as open files.
343 */
344 set_max_safe_fds();
345
346 /*
347 * XXX: It might make sense to move this into its own function at some
348 * point. Right now it seems like it'd cause more code duplication than
349 * it's worth.
350 */
351 if (check_only)
352 {
353 SetProcessingMode(NormalProcessing);
354 CheckerModeMain();
355 abort();
356 }
357
358 /*
359 * Do backend-like initialization for bootstrap mode
360 */
361 InitProcess();
362
363 BaseInit();
364
365 bootstrap_signals();
366 BootStrapXLOG(bootstrap_data_checksum_version);
367
368 /*
369 * To ensure that src/common/link-canary.c is linked into the backend, we
370 * must call it from somewhere. Here is as good as anywhere.
371 */
372 if (pg_link_canary_is_frontend())
373 elog(ERROR, "backend is incorrectly linked to frontend functions");
374
375 InitPostgres(NULL, InvalidOid, NULL, InvalidOid, 0, NULL);
376
377 /* Initialize stuff for bootstrap-file processing */
378 for (i = 0; i < MAXATTR; i++)
379 {
380 attrtypes[i] = NULL;
381 Nulls[i] = false;
382 }
383
384 if (boot_yylex_init(&scanner) != 0)
385 elog(ERROR, "yylex_init() failed: %m");
386
387 /*
388 * Process bootstrap input.
389 */
390 StartTransactionCommand();
391 boot_yyparse(scanner);
392 CommitTransactionCommand();
393
394 /*
395 * We should now know about all mapped relations, so it's okay to write
396 * out the initial relation mapping files.
397 */
398 RelationMapFinishBootstrap();
399
400 /* Clean up and exit */
401 cleanup();
402 proc_exit(0);
403}
404
405
406/* ----------------------------------------------------------------
407 * misc functions
408 * ----------------------------------------------------------------
409 */
410
411/*
412 * Set up signal handling for a bootstrap process
413 */
414static void
415 bootstrap_signals(void)
416{
417 Assert(!IsUnderPostmaster);
418
419 /*
420 * We don't actually need any non-default signal handling in bootstrap
421 * mode; "curl up and die" is a sufficient response for all these cases.
422 * Let's set that handling explicitly, as documentation if nothing else.
423 */
424 pqsignal(SIGHUP, SIG_DFL);
425 pqsignal(SIGINT, SIG_DFL);
426 pqsignal(SIGTERM, SIG_DFL);
427 pqsignal(SIGQUIT, SIG_DFL);
428}
429
430/* ----------------------------------------------------------------
431 * MANUAL BACKEND INTERACTIVE INTERFACE COMMANDS
432 * ----------------------------------------------------------------
433 */
434
435/* ----------------
436 * boot_openrel
437 *
438 * Execute BKI OPEN command.
439 * ----------------
440 */
441void
442 boot_openrel(char *relname)
443{
444 int i;
445
446 if (strlen(relname) >= NAMEDATALEN)
447 relname[NAMEDATALEN - 1] = '0円';
448
449 /*
450 * pg_type must be filled before any OPEN command is executed, hence we
451 * can now populate Typ if we haven't yet.
452 */
453 if (Typ == NIL)
454 populate_typ_list();
455
456 if (boot_reldesc != NULL)
457 closerel(NULL);
458
459 elog(DEBUG4, "open relation %s, attrsize %d",
460 relname, (int) ATTRIBUTE_FIXED_PART_SIZE);
461
462 boot_reldesc = table_openrv(makeRangeVar(NULL, relname, -1), NoLock);
463 numattr = RelationGetNumberOfAttributes(boot_reldesc);
464 for (i = 0; i < numattr; i++)
465 {
466 if (attrtypes[i] == NULL)
467 attrtypes[i] = AllocateAttribute();
468 memmove(attrtypes[i],
469 TupleDescAttr(boot_reldesc->rd_att, i),
470 ATTRIBUTE_FIXED_PART_SIZE);
471
472 {
473 Form_pg_attribute at = attrtypes[i];
474
475 elog(DEBUG4, "create attribute %d name %s len %d num %d type %u",
476 i, NameStr(at->attname), at->attlen, at->attnum,
477 at->atttypid);
478 }
479 }
480}
481
482/* ----------------
483 * closerel
484 * ----------------
485 */
486void
487 closerel(char *relname)
488{
489 if (relname)
490 {
491 if (boot_reldesc)
492 {
493 if (strcmp(RelationGetRelationName(boot_reldesc), relname) != 0)
494 elog(ERROR, "close of %s when %s was expected",
495 relname, RelationGetRelationName(boot_reldesc));
496 }
497 else
498 elog(ERROR, "close of %s before any relation was opened",
499 relname);
500 }
501
502 if (boot_reldesc == NULL)
503 elog(ERROR, "no open relation to close");
504 else
505 {
506 elog(DEBUG4, "close relation %s",
507 RelationGetRelationName(boot_reldesc));
508 table_close(boot_reldesc, NoLock);
509 boot_reldesc = NULL;
510 }
511}
512
513
514
515/* ----------------
516 * DEFINEATTR()
517 *
518 * define a <field,type> pair
519 * if there are n fields in a relation to be created, this routine
520 * will be called n times
521 * ----------------
522 */
523void
524 DefineAttr(char *name, char *type, int attnum, int nullness)
525{
526 Oid typeoid;
527
528 if (boot_reldesc != NULL)
529 {
530 elog(WARNING, "no open relations allowed with CREATE command");
531 closerel(NULL);
532 }
533
534 if (attrtypes[attnum] == NULL)
535 attrtypes[attnum] = AllocateAttribute();
536 MemSet(attrtypes[attnum], 0, ATTRIBUTE_FIXED_PART_SIZE);
537
538 namestrcpy(&attrtypes[attnum]->attname, name);
539 elog(DEBUG4, "column %s %s", NameStr(attrtypes[attnum]->attname), type);
540 attrtypes[attnum]->attnum = attnum + 1;
541
542 typeoid = gettype(type);
543
544 if (Typ != NIL)
545 {
546 attrtypes[attnum]->atttypid = Ap->am_oid;
547 attrtypes[attnum]->attlen = Ap->am_typ.typlen;
548 attrtypes[attnum]->attbyval = Ap->am_typ.typbyval;
549 attrtypes[attnum]->attalign = Ap->am_typ.typalign;
550 attrtypes[attnum]->attstorage = Ap->am_typ.typstorage;
551 attrtypes[attnum]->attcompression = InvalidCompressionMethod;
552 attrtypes[attnum]->attcollation = Ap->am_typ.typcollation;
553 /* if an array type, assume 1-dimensional attribute */
554 if (Ap->am_typ.typelem != InvalidOid && Ap->am_typ.typlen < 0)
555 attrtypes[attnum]->attndims = 1;
556 else
557 attrtypes[attnum]->attndims = 0;
558 }
559 else
560 {
561 attrtypes[attnum]->atttypid = TypInfo[typeoid].oid;
562 attrtypes[attnum]->attlen = TypInfo[typeoid].len;
563 attrtypes[attnum]->attbyval = TypInfo[typeoid].byval;
564 attrtypes[attnum]->attalign = TypInfo[typeoid].align;
565 attrtypes[attnum]->attstorage = TypInfo[typeoid].storage;
566 attrtypes[attnum]->attcompression = InvalidCompressionMethod;
567 attrtypes[attnum]->attcollation = TypInfo[typeoid].collation;
568 /* if an array type, assume 1-dimensional attribute */
569 if (TypInfo[typeoid].elem != InvalidOid &&
570 attrtypes[attnum]->attlen < 0)
571 attrtypes[attnum]->attndims = 1;
572 else
573 attrtypes[attnum]->attndims = 0;
574 }
575
576 /*
577 * If a system catalog column is collation-aware, force it to use C
578 * collation, so that its behavior is independent of the database's
579 * collation. This is essential to allow template0 to be cloned with a
580 * different database collation.
581 */
582 if (OidIsValid(attrtypes[attnum]->attcollation))
583 attrtypes[attnum]->attcollation = C_COLLATION_OID;
584
585 attrtypes[attnum]->atttypmod = -1;
586 attrtypes[attnum]->attislocal = true;
587
588 if (nullness == BOOTCOL_NULL_FORCE_NOT_NULL)
589 {
590 attrtypes[attnum]->attnotnull = true;
591 }
592 else if (nullness == BOOTCOL_NULL_FORCE_NULL)
593 {
594 attrtypes[attnum]->attnotnull = false;
595 }
596 else
597 {
598 Assert(nullness == BOOTCOL_NULL_AUTO);
599
600 /*
601 * Mark as "not null" if type is fixed-width and prior columns are
602 * likewise fixed-width and not-null. This corresponds to case where
603 * column can be accessed directly via C struct declaration.
604 */
605 if (attrtypes[attnum]->attlen > 0)
606 {
607 int i;
608
609 /* check earlier attributes */
610 for (i = 0; i < attnum; i++)
611 {
612 if (attrtypes[i]->attlen <= 0 ||
613 !attrtypes[i]->attnotnull)
614 break;
615 }
616 if (i == attnum)
617 attrtypes[attnum]->attnotnull = true;
618 }
619 }
620}
621
622
623/* ----------------
624 * InsertOneTuple
625 *
626 * If objectid is not zero, it is a specific OID to assign to the tuple.
627 * Otherwise, an OID will be assigned (if necessary) by heap_insert.
628 * ----------------
629 */
630void
631 InsertOneTuple(void)
632{
633 HeapTuple tuple;
634 TupleDesc tupDesc;
635 int i;
636
637 elog(DEBUG4, "inserting row with %d columns", numattr);
638
639 tupDesc = CreateTupleDesc(numattr, attrtypes);
640 tuple = heap_form_tuple(tupDesc, values, Nulls);
641 pfree(tupDesc); /* just free's tupDesc, not the attrtypes */
642
643 simple_heap_insert(boot_reldesc, tuple);
644 heap_freetuple(tuple);
645 elog(DEBUG4, "row inserted");
646
647 /*
648 * Reset null markers for next tuple
649 */
650 for (i = 0; i < numattr; i++)
651 Nulls[i] = false;
652}
653
654/* ----------------
655 * InsertOneValue
656 * ----------------
657 */
658void
659 InsertOneValue(char *value, int i)
660{
661 Oid typoid;
662 int16 typlen;
663 bool typbyval;
664 char typalign;
665 char typdelim;
666 Oid typioparam;
667 Oid typinput;
668 Oid typoutput;
669
670 Assert(i >= 0 && i < MAXATTR);
671
672 elog(DEBUG4, "inserting column %d value \"%s\"", i, value);
673
674 typoid = TupleDescAttr(boot_reldesc->rd_att, i)->atttypid;
675
676 boot_get_type_io_data(typoid,
677 &typlen, &typbyval, &typalign,
678 &typdelim, &typioparam,
679 &typinput, &typoutput);
680
681 values[i] = OidInputFunctionCall(typinput, value, typioparam, -1);
682
683 /*
684 * We use ereport not elog here so that parameters aren't evaluated unless
685 * the message is going to be printed, which generally it isn't
686 */
687 ereport(DEBUG4,
688 (errmsg_internal("inserted -> %s",
689 OidOutputFunctionCall(typoutput, values[i]))));
690}
691
692/* ----------------
693 * InsertOneNull
694 * ----------------
695 */
696void
697 InsertOneNull(int i)
698{
699 elog(DEBUG4, "inserting column %d NULL", i);
700 Assert(i >= 0 && i < MAXATTR);
701 if (TupleDescAttr(boot_reldesc->rd_att, i)->attnotnull)
702 elog(ERROR,
703 "NULL value specified for not-null column \"%s\" of relation \"%s\"",
704 NameStr(TupleDescAttr(boot_reldesc->rd_att, i)->attname),
705 RelationGetRelationName(boot_reldesc));
706 values[i] = PointerGetDatum(NULL);
707 Nulls[i] = true;
708}
709
710/* ----------------
711 * cleanup
712 * ----------------
713 */
714static void
715 cleanup(void)
716{
717 if (boot_reldesc != NULL)
718 closerel(NULL);
719}
720
721/* ----------------
722 * populate_typ_list
723 *
724 * Load the Typ list by reading pg_type.
725 * ----------------
726 */
727static void
728 populate_typ_list(void)
729{
730 Relation rel;
731 TableScanDesc scan;
732 HeapTuple tup;
733 MemoryContext old;
734
735 Assert(Typ == NIL);
736
737 rel = table_open(TypeRelationId, NoLock);
738 scan = table_beginscan_catalog(rel, 0, NULL);
739 old = MemoryContextSwitchTo(TopMemoryContext);
740 while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
741 {
742 Form_pg_type typForm = (Form_pg_type) GETSTRUCT(tup);
743 struct typmap *newtyp;
744
745 newtyp = (struct typmap *) palloc(sizeof(struct typmap));
746 Typ = lappend(Typ, newtyp);
747
748 newtyp->am_oid = typForm->oid;
749 memcpy(&newtyp->am_typ, typForm, sizeof(newtyp->am_typ));
750 }
751 MemoryContextSwitchTo(old);
752 table_endscan(scan);
753 table_close(rel, NoLock);
754}
755
756/* ----------------
757 * gettype
758 *
759 * NB: this is really ugly; it will return an integer index into TypInfo[],
760 * and not an OID at all, until the first reference to a type not known in
761 * TypInfo[]. At that point it will read and cache pg_type in Typ,
762 * and subsequently return a real OID (and set the global pointer Ap to
763 * point at the found row in Typ). So caller must check whether Typ is
764 * still NIL to determine what the return value is!
765 * ----------------
766 */
767static Oid
768 gettype(char *type)
769{
770 if (Typ != NIL)
771 {
772 ListCell *lc;
773
774 foreach(lc, Typ)
775 {
776 struct typmap *app = lfirst(lc);
777
778 if (strncmp(NameStr(app->am_typ.typname), type, NAMEDATALEN) == 0)
779 {
780 Ap = app;
781 return app->am_oid;
782 }
783 }
784
785 /*
786 * The type wasn't known; reload the pg_type contents and check again
787 * to handle composite types, added since last populating the list.
788 */
789
790 list_free_deep(Typ);
791 Typ = NIL;
792 populate_typ_list();
793
794 /*
795 * Calling gettype would result in infinite recursion for types
796 * missing in pg_type, so just repeat the lookup.
797 */
798 foreach(lc, Typ)
799 {
800 struct typmap *app = lfirst(lc);
801
802 if (strncmp(NameStr(app->am_typ.typname), type, NAMEDATALEN) == 0)
803 {
804 Ap = app;
805 return app->am_oid;
806 }
807 }
808 }
809 else
810 {
811 int i;
812
813 for (i = 0; i < n_types; i++)
814 {
815 if (strncmp(type, TypInfo[i].name, NAMEDATALEN) == 0)
816 return i;
817 }
818 /* Not in TypInfo, so we'd better be able to read pg_type now */
819 elog(DEBUG4, "external type: %s", type);
820 populate_typ_list();
821 return gettype(type);
822 }
823 elog(ERROR, "unrecognized type \"%s\"", type);
824 /* not reached, here to make compiler happy */
825 return 0;
826}
827
828/* ----------------
829 * boot_get_type_io_data
830 *
831 * Obtain type I/O information at bootstrap time. This intentionally has
832 * almost the same API as lsyscache.c's get_type_io_data, except that
833 * we only support obtaining the typinput and typoutput routines, not
834 * the binary I/O routines. It is exported so that array_in and array_out
835 * can be made to work during early bootstrap.
836 * ----------------
837 */
838void
839 boot_get_type_io_data(Oid typid,
840 int16 *typlen,
841 bool *typbyval,
842 char *typalign,
843 char *typdelim,
844 Oid *typioparam,
845 Oid *typinput,
846 Oid *typoutput)
847{
848 if (Typ != NIL)
849 {
850 /* We have the boot-time contents of pg_type, so use it */
851 struct typmap *ap = NULL;
852 ListCell *lc;
853
854 foreach(lc, Typ)
855 {
856 ap = lfirst(lc);
857 if (ap->am_oid == typid)
858 break;
859 }
860
861 if (!ap || ap->am_oid != typid)
862 elog(ERROR, "type OID %u not found in Typ list", typid);
863
864 *typlen = ap->am_typ.typlen;
865 *typbyval = ap->am_typ.typbyval;
866 *typalign = ap->am_typ.typalign;
867 *typdelim = ap->am_typ.typdelim;
868
869 /* XXX this logic must match getTypeIOParam() */
870 if (OidIsValid(ap->am_typ.typelem))
871 *typioparam = ap->am_typ.typelem;
872 else
873 *typioparam = typid;
874
875 *typinput = ap->am_typ.typinput;
876 *typoutput = ap->am_typ.typoutput;
877 }
878 else
879 {
880 /* We don't have pg_type yet, so use the hard-wired TypInfo array */
881 int typeindex;
882
883 for (typeindex = 0; typeindex < n_types; typeindex++)
884 {
885 if (TypInfo[typeindex].oid == typid)
886 break;
887 }
888 if (typeindex >= n_types)
889 elog(ERROR, "type OID %u not found in TypInfo", typid);
890
891 *typlen = TypInfo[typeindex].len;
892 *typbyval = TypInfo[typeindex].byval;
893 *typalign = TypInfo[typeindex].align;
894 /* We assume typdelim is ',' for all boot-time types */
895 *typdelim = ',';
896
897 /* XXX this logic must match getTypeIOParam() */
898 if (OidIsValid(TypInfo[typeindex].elem))
899 *typioparam = TypInfo[typeindex].elem;
900 else
901 *typioparam = typid;
902
903 *typinput = TypInfo[typeindex].inproc;
904 *typoutput = TypInfo[typeindex].outproc;
905 }
906}
907
908/* ----------------
909 * AllocateAttribute
910 *
911 * Note: bootstrap never sets any per-column ACLs, so we only need
912 * ATTRIBUTE_FIXED_PART_SIZE space per attribute.
913 * ----------------
914 */
915static Form_pg_attribute
916 AllocateAttribute(void)
917{
918 return (Form_pg_attribute)
919 MemoryContextAllocZero(TopMemoryContext, ATTRIBUTE_FIXED_PART_SIZE);
920}
921
922/*
923 * index_register() -- record an index that has been set up for building
924 * later.
925 *
926 * At bootstrap time, we define a bunch of indexes on system catalogs.
927 * We postpone actually building the indexes until just before we're
928 * finished with initialization, however. This is because the indexes
929 * themselves have catalog entries, and those have to be included in the
930 * indexes on those catalogs. Doing it in two phases is the simplest
931 * way of making sure the indexes have the right contents at the end.
932 */
933void
934 index_register(Oid heap,
935 Oid ind,
936 const IndexInfo *indexInfo)
937{
938 IndexList *newind;
939 MemoryContext oldcxt;
940
941 /*
942 * XXX mao 10/31/92 -- don't gc index reldescs, associated info at
943 * bootstrap time. we'll declare the indexes now, but want to create them
944 * later.
945 */
946
947 if (nogc == NULL)
948 nogc = AllocSetContextCreate(NULL,
949 "BootstrapNoGC",
950 ALLOCSET_DEFAULT_SIZES);
951
952 oldcxt = MemoryContextSwitchTo(nogc);
953
954 newind = (IndexList *) palloc(sizeof(IndexList));
955 newind->il_heap = heap;
956 newind->il_ind = ind;
957 newind->il_info = (IndexInfo *) palloc(sizeof(IndexInfo));
958
959 memcpy(newind->il_info, indexInfo, sizeof(IndexInfo));
960 /* expressions will likely be null, but may as well copy it */
961 newind->il_info->ii_Expressions =
962 copyObject(indexInfo->ii_Expressions);
963 newind->il_info->ii_ExpressionsState = NIL;
964 /* predicate will likely be null, but may as well copy it */
965 newind->il_info->ii_Predicate =
966 copyObject(indexInfo->ii_Predicate);
967 newind->il_info->ii_PredicateState = NULL;
968 /* no exclusion constraints at bootstrap time, so no need to copy */
969 Assert(indexInfo->ii_ExclusionOps == NULL);
970 Assert(indexInfo->ii_ExclusionProcs == NULL);
971 Assert(indexInfo->ii_ExclusionStrats == NULL);
972
973 newind->il_next = ILHead;
974 ILHead = newind;
975
976 MemoryContextSwitchTo(oldcxt);
977}
978
979
980/*
981 * build_indices -- fill in all the indexes registered earlier
982 */
983void
984 build_indices(void)
985{
986 for (; ILHead != NULL; ILHead = ILHead->il_next)
987 {
988 Relation heap;
989 Relation ind;
990
991 /* need not bother with locks during bootstrap */
992 heap = table_open(ILHead->il_heap, NoLock);
993 ind = index_open(ILHead->il_ind, NoLock);
994
995 index_build(heap, ind, ILHead->il_info, false, false);
996
997 index_close(ind, NoLock);
998 table_close(heap, NoLock);
999 }
1000}
#define write_stderr(str)
Definition: parallel.c:186
static Oid gettype(char *type)
Definition: bootstrap.c:768
static Datum values[MAXATTR]
Definition: bootstrap.c:153
void closerel(char *relname)
Definition: bootstrap.c:487
static void populate_typ_list(void)
Definition: bootstrap.c:728
static void CheckerModeMain(void)
Definition: bootstrap.c:182
void build_indices(void)
Definition: bootstrap.c:984
void InsertOneValue(char *value, int i)
Definition: bootstrap.c:659
static void cleanup(void)
Definition: bootstrap.c:715
static const int n_types
Definition: bootstrap.c:142
void InsertOneNull(int i)
Definition: bootstrap.c:697
static const struct typinfo TypInfo[]
Definition: bootstrap.c:87
Relation boot_reldesc
Definition: bootstrap.c:58
static struct typmap * Ap
Definition: bootstrap.c:151
static List * Typ
Definition: bootstrap.c:150
void InsertOneTuple(void)
Definition: bootstrap.c:631
struct _IndexList IndexList
static void bootstrap_signals(void)
Definition: bootstrap.c:415
Form_pg_attribute attrtypes[MAXATTR]
Definition: bootstrap.c:60
void boot_get_type_io_data(Oid typid, int16 *typlen, bool *typbyval, char *typalign, char *typdelim, Oid *typioparam, Oid *typinput, Oid *typoutput)
Definition: bootstrap.c:839
int numattr
Definition: bootstrap.c:61
static Form_pg_attribute AllocateAttribute(void)
Definition: bootstrap.c:916
static MemoryContext nogc
Definition: bootstrap.c:156
void BootstrapModeMain(int argc, char *argv[], bool check_only)
Definition: bootstrap.c:200
void DefineAttr(char *name, char *type, int attnum, int nullness)
Definition: bootstrap.c:524
static bool Nulls[MAXATTR]
Definition: bootstrap.c:154
void index_register(Oid heap, Oid ind, const IndexInfo *indexInfo)
Definition: bootstrap.c:934
void boot_openrel(char *relname)
Definition: bootstrap.c:442
static IndexList * ILHead
Definition: bootstrap.c:172
#define BOOTCOL_NULL_FORCE_NULL
Definition: bootstrap.h:28
int boot_yylex_init(yyscan_t *yyscannerp)
#define MAXATTR
Definition: bootstrap.h:25
#define BOOTCOL_NULL_FORCE_NOT_NULL
Definition: bootstrap.h:29
#define BOOTCOL_NULL_AUTO
Definition: bootstrap.h:27
int boot_yyparse(yyscan_t yyscanner)
#define PG_DATA_CHECKSUM_VERSION
Definition: bufpage.h:207
#define NameStr(name)
Definition: c.h:751
int16_t int16
Definition: c.h:533
uint32_t uint32
Definition: c.h:538
#define MemSet(start, val, len)
Definition: c.h:1019
#define OidIsValid(objectId)
Definition: c.h:774
void * yyscan_t
Definition: cubedata.h:65
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1161
int errcode(int sqlerrcode)
Definition: elog.c:854
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define WARNING
Definition: elog.h:36
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:226
#define ereport(elevel,...)
Definition: elog.h:150
#define DEBUG4
Definition: elog.h:27
void set_max_safe_fds(void)
Definition: fd.c:1041
Datum OidInputFunctionCall(Oid functionId, char *str, Oid typioparam, int32 typmod)
Definition: fmgr.c:1753
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:1762
bool IsUnderPostmaster
Definition: globals.c:120
char OutputFileName[MAXPGPATH]
Definition: globals.c:79
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition: guc.c:4337
bool SelectConfigFiles(const char *userDoption, const char *progname)
Definition: guc.c:1785
void ParseLongOption(const char *string, char **name, char **value)
Definition: guc.c:6384
void InitializeGUCOptions(void)
Definition: guc.c:1531
@ PGC_S_DYNAMIC_DEFAULT
Definition: guc.h:114
@ PGC_S_ARGV
Definition: guc.h:117
@ PGC_INTERNAL
Definition: guc.h:73
@ PGC_POSTMASTER
Definition: guc.h:74
Assert(PointerIsAligned(start, uint64))
HeapTuple heap_getnext(TableScanDesc sscan, ScanDirection direction)
Definition: heapam.c:1346
void simple_heap_insert(Relation relation, HeapTuple tup)
Definition: heapam.c:2720
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition: heaptuple.c:1117
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1435
static void * GETSTRUCT(const HeapTupleData *tuple)
Definition: htup_details.h:728
void index_build(Relation heapRelation, Relation indexRelation, IndexInfo *indexInfo, bool isreindex, bool parallel)
Definition: index.c:3002
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:177
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition: indexam.c:133
static struct @169 value
void proc_exit(int code)
Definition: ipc.c:104
void CreateSharedMemoryAndSemaphores(void)
Definition: ipci.c:200
i
int i
Definition: isn.c:77
List * lappend(List *list, void *datum)
Definition: list.c:339
void list_free_deep(List *list)
Definition: list.c:1560
#define NoLock
Definition: lockdefs.h:34
DispatchOption parse_dispatch_option(const char *name)
Definition: main.c:244
const char * progname
Definition: main.c:44
RangeVar * makeRangeVar(char *schemaname, char *relname, int location)
Definition: makefuncs.c:473
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:1263
char * pstrdup(const char *in)
Definition: mcxt.c:1759
void pfree(void *pointer)
Definition: mcxt.c:1594
MemoryContext TopMemoryContext
Definition: mcxt.c:166
void * palloc(Size size)
Definition: mcxt.c:1365
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:160
@ NormalProcessing
Definition: miscadmin.h:471
@ BootstrapProcessing
Definition: miscadmin.h:469
#define SetProcessingMode(mode)
Definition: miscadmin.h:482
void ChangeToDataDir(void)
Definition: miscinit.c:409
void InitStandaloneProcess(const char *argv0)
Definition: miscinit.c:175
bool IgnoreSystemIndexes
Definition: miscinit.c:81
void checkDataDir(void)
Definition: miscinit.c:296
void CreateDataDirLockFile(bool amPostmaster)
Definition: miscinit.c:1463
void namestrcpy(Name name, const char *str)
Definition: name.c:233
#define copyObject(obj)
Definition: nodes.h:232
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
NameData attname
Definition: pg_attribute.h:41
#define ATTRIBUTE_FIXED_PART_SIZE
Definition: pg_attribute.h:194
int16 attnum
Definition: pg_attribute.h:74
int16 attlen
Definition: pg_attribute.h:59
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:202
bool attnotnull
Definition: pg_attribute.h:123
NameData relname
Definition: pg_class.h:38
#define NAMEDATALEN
#define MAXPGPATH
PGDLLIMPORT int optind
Definition: getopt.c:51
int getopt(int nargc, char *const *nargv, const char *ostr)
Definition: getopt.c:72
PGDLLIMPORT char * optarg
Definition: getopt.c:53
#define lfirst(lc)
Definition: pg_list.h:172
#define NIL
Definition: pg_list.h:68
char typalign
Definition: pg_type.h:176
FormData_pg_type
Definition: pg_type.h:254
FormData_pg_type * Form_pg_type
Definition: pg_type.h:261
void InitPostmasterChildSlots(void)
Definition: pmchild.c:97
#define pqsignal
Definition: port.h:531
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
static const char * userDoption
Definition: postgres.c:153
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:332
uint64_t Datum
Definition: postgres.h:70
#define InvalidOid
Definition: postgres_ext.h:37
unsigned int Oid
Definition: postgres_ext.h:32
void InitializeMaxBackends(void)
Definition: postinit.c:554
void BaseInit(void)
Definition: postinit.c:611
void InitializeFastPathLocks(void)
Definition: postinit.c:579
void InitPostgres(const char *in_dbname, Oid dboid, const char *username, Oid useroid, bits32 flags, char *out_dbname)
Definition: postinit.c:711
@ DISPATCH_POSTMASTER
Definition: postmaster.h:139
char * psprintf(const char *fmt,...)
Definition: psprintf.c:43
#define RelationGetNumberOfAttributes(relation)
Definition: rel.h:520
#define RelationGetRelationName(relation)
Definition: rel.h:548
void RelationMapFinishBootstrap(void)
Definition: relmapper.c:625
@ ForwardScanDirection
Definition: sdir.h:28
void InitProcess(void)
Definition: proc.c:390
uint16 * ii_ExclusionStrats
Definition: execnodes.h:192
ExprState * ii_PredicateState
Definition: execnodes.h:185
Oid * ii_ExclusionOps
Definition: execnodes.h:188
List * ii_ExpressionsState
Definition: execnodes.h:180
List * ii_Expressions
Definition: execnodes.h:178
Oid * ii_ExclusionProcs
Definition: execnodes.h:190
List * ii_Predicate
Definition: execnodes.h:183
Definition: pg_list.h:54
Definition: rel.h:56
TupleDesc rd_att
Definition: rel.h:112
Oid il_heap
Definition: bootstrap.c:166
struct _IndexList * il_next
Definition: bootstrap.c:169
Oid il_ind
Definition: bootstrap.c:167
IndexInfo * il_info
Definition: bootstrap.c:168
Definition: bootstrap.c:74
char align
Definition: bootstrap.c:80
Oid outproc
Definition: bootstrap.c:84
int16 len
Definition: bootstrap.c:78
Oid oid
Definition: bootstrap.c:76
bool byval
Definition: bootstrap.c:79
char name[NAMEDATALEN]
Definition: bootstrap.c:75
Oid collation
Definition: bootstrap.c:82
Oid elem
Definition: bootstrap.c:77
char storage
Definition: bootstrap.c:81
Oid inproc
Definition: bootstrap.c:83
Definition: bootstrap.c:145
Oid am_oid
Definition: bootstrap.c:146
FormData_pg_type am_typ
Definition: bootstrap.c:147
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:40
Relation table_openrv(const RangeVar *relation, LOCKMODE lockmode)
Definition: table.c:83
TableScanDesc table_beginscan_catalog(Relation relation, int nkeys, ScanKeyData *key)
Definition: tableam.c:113
static void table_endscan(TableScanDesc scan)
Definition: tableam.h:985
char * flag(int b)
Definition: test-ctype.c:33
#define InvalidCompressionMethod
TupleDesc CreateTupleDesc(int natts, Form_pg_attribute *attrs)
Definition: tupdesc.c:229
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition: tupdesc.h:160
Definition: pg_list.h:46
const char * type
const char * name
#define SIGHUP
Definition: win32_port.h:158
#define SIGQUIT
Definition: win32_port.h:159
void StartTransactionCommand(void)
Definition: xact.c:3071
void CommitTransactionCommand(void)
Definition: xact.c:3169
void BootStrapXLOG(uint32 data_checksum_version)
Definition: xlog.c:5075

AltStyle によって変換されたページ (->オリジナル) /