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