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