PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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"
28#include "catalog/pg_authid.h"
30#include "catalog/pg_proc.h"
31#include "catalog/pg_type.h"
32#include "common/link-canary.h"
33#include "miscadmin.h"
34#include "nodes/makefuncs.h"
35#include "port/pg_getopt_ctx.h"
37#include "storage/bufpage.h"
38#include "storage/checksum.h"
39#include "storage/fd.h"
40#include "storage/ipc.h"
41#include "storage/proc.h"
43#include "utils/builtins.h"
44#include "utils/fmgroids.h"
45#include "utils/guc.h"
46#include "utils/memutils.h"
47#include "utils/rel.h"
48#include "utils/relmapper.h"
49
50
51static void CheckerModeMain(void);
52static void bootstrap_signals(void);
54static void InsertOneProargdefaultsValue(char *value);
55static void populate_typ_list(void);
56static Oid gettype(char *type);
57static void cleanup(void);
58
59/* ----------------
60 * global variables
61 * ----------------
62 */
63
64Relation boot_reldesc; /* current relation descriptor */
65
66Form_pg_attribute attrtypes[MAXATTR]; /* points to attribute info */
67int numattr; /* number of attributes for cur. rel */
68
69
70/*
71 * Basic information associated with each type. This is used before
72 * pg_type is filled, so it has to cover the datatypes used as column types
73 * in the core "bootstrapped" catalogs.
74 *
75 * XXX several of these input/output functions do catalog scans
76 * (e.g., F_REGPROCIN scans pg_proc). this obviously creates some
77 * order dependencies in the catalog creation process.
78 */
92
93static const struct typinfo TypInfo[] = {
94 {"bool", BOOLOID, 0, 1, true, TYPALIGN_CHAR, TYPSTORAGE_PLAIN, InvalidOid,
96 {"bytea", BYTEAOID, 0, -1, false, TYPALIGN_INT, TYPSTORAGE_EXTENDED, InvalidOid,
98 {"char", CHAROID, 0, 1, true, TYPALIGN_CHAR, TYPSTORAGE_PLAIN, InvalidOid,
100 {"cstring", CSTRINGOID, 0, -2, false, TYPALIGN_CHAR, TYPSTORAGE_PLAIN, InvalidOid,
102 {"int2", INT2OID, 0, 2, true, TYPALIGN_SHORT, TYPSTORAGE_PLAIN, InvalidOid,
104 {"int4", INT4OID, 0, 4, true, TYPALIGN_INT, TYPSTORAGE_PLAIN, InvalidOid,
106 {"int8", INT8OID, 0, 8, true, TYPALIGN_DOUBLE, TYPSTORAGE_PLAIN, InvalidOid,
108 {"float4", FLOAT4OID, 0, 4, true, TYPALIGN_INT, TYPSTORAGE_PLAIN, InvalidOid,
110 {"float8", FLOAT8OID, 0, 8, true, TYPALIGN_DOUBLE, TYPSTORAGE_PLAIN, InvalidOid,
114 {"regproc", REGPROCOID, 0, 4, true, TYPALIGN_INT, TYPSTORAGE_PLAIN, InvalidOid,
118 {"jsonb", JSONBOID, 0, -1, false, TYPALIGN_INT, TYPSTORAGE_EXTENDED, InvalidOid,
120 {"oid", OIDOID, 0, 4, true, TYPALIGN_INT, TYPSTORAGE_PLAIN, InvalidOid,
122 {"aclitem", ACLITEMOID, 0, 16, false, TYPALIGN_DOUBLE, TYPSTORAGE_PLAIN, InvalidOid,
126 {"int2vector", INT2VECTOROID, INT2OID, -1, false, TYPALIGN_INT, TYPSTORAGE_PLAIN, InvalidOid,
128 {"oidvector", OIDVECTOROID, OIDOID, -1, false, TYPALIGN_INT, TYPSTORAGE_PLAIN, InvalidOid,
140};
141
142static const int n_types = sizeof(TypInfo) / sizeof(struct typinfo);
143
144struct typmap
145{ /* a hack */
148};
149
150static List *Typ = NIL; /* List of struct typmap* */
151static struct typmap *Ap = NULL;
152
153/*
154 * Basic information about built-in roles.
155 *
156 * Presently, this need only list roles that are mentioned in aclitem arrays
157 * in the catalog .dat files. We might as well list everything that is in
158 * pg_authid.dat, since there aren't that many. Like pg_authid.dat, we
159 * represent the bootstrap superuser's name as "POSTGRES", even though it
160 * (probably) won't be that in the finished installation; this means aclitem
161 * entries in .dat files must spell it like that.
162 */
164{
165 const char *rolname;
167};
168
169static const struct rolinfo RolInfo[] = {
170 {"POSTGRES", BOOTSTRAP_SUPERUSERID},
171 {"pg_database_owner", ROLE_PG_DATABASE_OWNER},
172 {"pg_read_all_data", ROLE_PG_READ_ALL_DATA},
173 {"pg_write_all_data", ROLE_PG_WRITE_ALL_DATA},
174 {"pg_monitor", ROLE_PG_MONITOR},
175 {"pg_read_all_settings", ROLE_PG_READ_ALL_SETTINGS},
176 {"pg_read_all_stats", ROLE_PG_READ_ALL_STATS},
177 {"pg_stat_scan_tables", ROLE_PG_STAT_SCAN_TABLES},
178 {"pg_read_server_files", ROLE_PG_READ_SERVER_FILES},
179 {"pg_write_server_files", ROLE_PG_WRITE_SERVER_FILES},
180 {"pg_execute_server_program", ROLE_PG_EXECUTE_SERVER_PROGRAM},
181 {"pg_signal_backend", ROLE_PG_SIGNAL_BACKEND},
182 {"pg_checkpoint", ROLE_PG_CHECKPOINT},
183 {"pg_maintain", ROLE_PG_MAINTAIN},
184 {"pg_use_reserved_connections", ROLE_PG_USE_RESERVED_CONNECTIONS},
185 {"pg_create_subscription", ROLE_PG_CREATE_SUBSCRIPTION},
186 {"pg_signal_autovacuum_worker", ROLE_PG_SIGNAL_AUTOVACUUM_WORKER}
187};
188
189
190static Datum values[MAXATTR]; /* current row's attribute values */
191static bool Nulls[MAXATTR];
192
193static MemoryContext nogc = NULL; /* special no-gc mem context */
194
195/*
196 * At bootstrap time, we first declare all the indices to be built, and
197 * then build them. The IndexList structure stores enough information
198 * to allow us to build the indices after they've been declared.
199 */
200
208
210
211
212/*
213 * In shared memory checker mode, all we really want to do is create shared
214 * memory and semaphores (just to prove we can do it with the current GUC
215 * settings). Since, in fact, that was already done by
216 * CreateSharedMemoryAndSemaphores(), we have nothing more to do here.
217 */
218static void
220{
221 proc_exit(0);
222}
223
224/*
225 * The main entry point for running the backend in bootstrap mode
226 *
227 * The bootstrap mode is used to initialize the template database.
228 * The bootstrap backend doesn't speak SQL, but instead expects
229 * commands in a special bootstrap language.
230 *
231 * When check_only is true, startup is done only far enough to verify that
232 * the current configuration, particularly the passed in options pertaining
233 * to shared memory sizing, options work (or at least do not cause an error
234 * up to shared memory creation).
235 */
236void
237BootstrapModeMain(int argc, char *argv[], bool check_only)
238{
239 int i;
240 char *progname = argv[0];
242 int flag;
243 char *userDoption = NULL;
245 yyscan_t scanner;
246
248
249 InitStandaloneProcess(argv[0]);
250
251 /* Set defaults, to be overridden by explicit options below */
253
254 /* an initial --boot or --check should be present */
255 Assert(argc > 1
256 && (strcmp(argv[1], "--boot") == 0
257 || strcmp(argv[1], "--check") == 0));
258 argv++;
259 argc--;
260
261 pg_getopt_start(&optctx, argc, argv, "B:c:d:D:Fkr:X:-:");
262 while ((flag = pg_getopt_next(&optctx)) != -1)
263 {
264 switch (flag)
265 {
266 case 'B':
267 SetConfigOption("shared_buffers", optctx.optarg, PGC_POSTMASTER, PGC_S_ARGV);
268 break;
269 case '-':
270
271 /*
272 * Error if the user misplaced a special must-be-first option
273 * for dispatching to a subprogram. parse_dispatch_option()
274 * returns DISPATCH_POSTMASTER if it doesn't find a match, so
275 * error for anything else.
276 */
280 errmsg("--%s must be first argument", optctx.optarg)));
281
283 case 'c':
284 {
285 char *name,
286 *value;
287
288 ParseLongOption(optctx.optarg, &name, &value);
289 if (!value)
290 {
291 if (flag == '-')
294 errmsg("--%s requires a value",
295 optctx.optarg)));
296 else
299 errmsg("-c %s requires a value",
300 optctx.optarg)));
301 }
302
304 pfree(name);
305 pfree(value);
306 break;
307 }
308 case 'D':
309 userDoption = pstrdup(optctx.optarg);
310 break;
311 case 'd':
312 {
313 /* Turn on debugging for the bootstrap process. */
314 char *debugstr;
315
316 debugstr = psprintf("debug%s", optctx.optarg);
317 SetConfigOption("log_min_messages", debugstr,
319 SetConfigOption("client_min_messages", debugstr,
322 }
323 break;
324 case 'F':
325 SetConfigOption("fsync", "false", PGC_POSTMASTER, PGC_S_ARGV);
326 break;
327 case 'k':
329 break;
330 case 'r':
332 break;
333 case 'X':
334 SetConfigOption("wal_segment_size", optctx.optarg, PGC_INTERNAL, PGC_S_DYNAMIC_DEFAULT);
335 break;
336 default:
337 write_stderr("Try \"%s --help\" for more information.\n",
338 progname);
339 proc_exit(1);
340 break;
341 }
342 }
343
344 if (argc != optctx.optind)
345 {
346 write_stderr("%s: invalid command-line arguments\n", progname);
347 proc_exit(1);
348 }
349
350 /* Acquire configuration parameters */
352 proc_exit(1);
353
354 /*
355 * Validate we have been given a reasonable-looking DataDir and change
356 * into it
357 */
358 checkDataDir();
360
362
364 IgnoreSystemIndexes = true;
365
367
369
370 /*
371 * Even though bootstrapping runs in single-process mode, initialize
372 * postmaster child slots array so that --check can detect running out of
373 * shared memory or other resources if max_connections is set too high.
374 */
376
378
381
382 /*
383 * Estimate number of openable files. This is essential too in --check
384 * mode, because on some platforms semaphores count as open files.
385 */
387
388 /*
389 * XXX: It might make sense to move this into its own function at some
390 * point. Right now it seems like it'd cause more code duplication than
391 * it's worth.
392 */
393 if (check_only)
394 {
397 abort();
398 }
399
400 /*
401 * Do backend-like initialization for bootstrap mode
402 */
403 InitProcess();
404
405 BaseInit();
406
409
410 /*
411 * To ensure that src/common/link-canary.c is linked into the backend, we
412 * must call it from somewhere. Here is as good as anywhere.
413 */
415 elog(ERROR, "backend is incorrectly linked to frontend functions");
416
418
419 /* Initialize stuff for bootstrap-file processing */
420 for (i = 0; i < MAXATTR; i++)
421 {
422 attrtypes[i] = NULL;
423 Nulls[i] = false;
424 }
425
426 if (boot_yylex_init(&scanner) != 0)
427 elog(ERROR, "yylex_init() failed: %m");
428
429 /*
430 * Process bootstrap input.
431 */
433 boot_yyparse(scanner);
435
436 /*
437 * We should now know about all mapped relations, so it's okay to write
438 * out the initial relation mapping files.
439 */
441
442 /* Clean up and exit */
443 cleanup();
444 proc_exit(0);
445}
446
447
448/* ----------------------------------------------------------------
449 * misc functions
450 * ----------------------------------------------------------------
451 */
452
453/*
454 * Set up signal handling for a bootstrap process
455 */
456static void
458{
460
461 /*
462 * We don't actually need any non-default signal handling in bootstrap
463 * mode; "curl up and die" is a sufficient response for all these cases.
464 * Let's set that handling explicitly, as documentation if nothing else.
465 */
470}
471
472/* ----------------------------------------------------------------
473 * MANUAL BACKEND INTERACTIVE INTERFACE COMMANDS
474 * ----------------------------------------------------------------
475 */
476
477/* ----------------
478 * boot_openrel
479 *
480 * Execute BKI OPEN command.
481 * ----------------
482 */
483void
485{
486 int i;
487
488 if (strlen(relname) >= NAMEDATALEN)
489 relname[NAMEDATALEN - 1] = '\0';
490
491 /*
492 * pg_type must be filled before any OPEN command is executed, hence we
493 * can now populate Typ if we haven't yet.
494 */
495 if (Typ == NIL)
497
498 if (boot_reldesc != NULL)
499 closerel(NULL);
500
501 elog(DEBUG4, "open relation %s, attrsize %d",
503
506 for (i = 0; i < numattr; i++)
507 {
508 if (attrtypes[i] == NULL)
513
514 {
516
517 elog(DEBUG4, "create attribute %d name %s len %d num %d type %u",
518 i, NameStr(at->attname), at->attlen, at->attnum,
519 at->atttypid);
520 }
521 }
522}
523
524/* ----------------
525 * closerel
526 * ----------------
527 */
528void
530{
531 if (relname)
532 {
533 if (boot_reldesc)
534 {
536 elog(ERROR, "close of %s when %s was expected",
538 }
539 else
540 elog(ERROR, "close of %s before any relation was opened",
541 relname);
542 }
543
544 if (boot_reldesc == NULL)
545 elog(ERROR, "no open relation to close");
546 else
547 {
548 elog(DEBUG4, "close relation %s",
552 }
553}
554
555
556
557/* ----------------
558 * DEFINEATTR()
559 *
560 * define a <field,type> pair
561 * if there are n fields in a relation to be created, this routine
562 * will be called n times
563 * ----------------
564 */
565void
566DefineAttr(char *name, char *type, int attnum, int nullness)
567{
568 Oid typeoid;
569
570 if (boot_reldesc != NULL)
571 {
572 elog(WARNING, "no open relations allowed with CREATE command");
573 closerel(NULL);
574 }
575
576 if (attrtypes[attnum] == NULL)
579
581 elog(DEBUG4, "column %s %s", NameStr(attrtypes[attnum]->attname), type);
582 attrtypes[attnum]->attnum = attnum + 1;
583
584 typeoid = gettype(type);
585
586 if (Typ != NIL)
587 {
588 attrtypes[attnum]->atttypid = Ap->am_oid;
589 attrtypes[attnum]->attlen = Ap->am_typ.typlen;
590 attrtypes[attnum]->attbyval = Ap->am_typ.typbyval;
591 attrtypes[attnum]->attalign = Ap->am_typ.typalign;
592 attrtypes[attnum]->attstorage = Ap->am_typ.typstorage;
593 attrtypes[attnum]->attcompression = InvalidCompressionMethod;
594 attrtypes[attnum]->attcollation = Ap->am_typ.typcollation;
595 /* if an array type, assume 1-dimensional attribute */
596 if (Ap->am_typ.typelem != InvalidOid && Ap->am_typ.typlen < 0)
597 attrtypes[attnum]->attndims = 1;
598 else
599 attrtypes[attnum]->attndims = 0;
600 }
601 else
602 {
603 attrtypes[attnum]->atttypid = TypInfo[typeoid].oid;
604 attrtypes[attnum]->attlen = TypInfo[typeoid].len;
605 attrtypes[attnum]->attbyval = TypInfo[typeoid].byval;
606 attrtypes[attnum]->attalign = TypInfo[typeoid].align;
607 attrtypes[attnum]->attstorage = TypInfo[typeoid].storage;
608 attrtypes[attnum]->attcompression = InvalidCompressionMethod;
609 attrtypes[attnum]->attcollation = TypInfo[typeoid].collation;
610 /* if an array type, assume 1-dimensional attribute */
611 if (TypInfo[typeoid].elem != InvalidOid &&
612 attrtypes[attnum]->attlen < 0)
613 attrtypes[attnum]->attndims = 1;
614 else
615 attrtypes[attnum]->attndims = 0;
616 }
617
618 /*
619 * If a system catalog column is collation-aware, force it to use C
620 * collation, so that its behavior is independent of the database's
621 * collation. This is essential to allow template0 to be cloned with a
622 * different database collation.
623 */
624 if (OidIsValid(attrtypes[attnum]->attcollation))
625 attrtypes[attnum]->attcollation = C_COLLATION_OID;
626
627 attrtypes[attnum]->atttypmod = -1;
628 attrtypes[attnum]->attislocal = true;
629
631 {
632 attrtypes[attnum]->attnotnull = true;
633 }
635 {
636 attrtypes[attnum]->attnotnull = false;
637 }
638 else
639 {
641
642 /*
643 * Mark as "not null" if type is fixed-width and prior columns are
644 * likewise fixed-width and not-null. This corresponds to case where
645 * column can be accessed directly via C struct declaration.
646 */
647 if (attrtypes[attnum]->attlen > 0)
648 {
649 int i;
650
651 /* check earlier attributes */
652 for (i = 0; i < attnum; i++)
653 {
654 if (attrtypes[i]->attlen <= 0 ||
656 break;
657 }
658 if (i == attnum)
659 attrtypes[attnum]->attnotnull = true;
660 }
661 }
662}
663
664
665/* ----------------
666 * InsertOneTuple
667 *
668 * If objectid is not zero, it is a specific OID to assign to the tuple.
669 * Otherwise, an OID will be assigned (if necessary) by heap_insert.
670 * ----------------
671 */
672void
674{
675 HeapTuple tuple;
676 TupleDesc tupDesc;
677 int i;
678
679 elog(DEBUG4, "inserting row with %d columns", numattr);
680
682 tuple = heap_form_tuple(tupDesc, values, Nulls);
683 pfree(tupDesc); /* just free's tupDesc, not the attrtypes */
684
686 heap_freetuple(tuple);
687 elog(DEBUG4, "row inserted");
688
689 /*
690 * Reset null markers for next tuple
691 */
692 for (i = 0; i < numattr; i++)
693 Nulls[i] = false;
694}
695
696/* ----------------
697 * InsertOneValue
698 * ----------------
699 */
700void
702{
704 Oid typoid;
705 int16 typlen;
706 bool typbyval;
707 char typalign;
708 char typdelim;
709 Oid typioparam;
711 Oid typoutput;
712 Oid typcollation;
713
714 Assert(i >= 0 && i < MAXATTR);
715
716 elog(DEBUG4, "inserting column %d value \"%s\"", i, value);
717
719 typoid = attr->atttypid;
720
722 &typlen, &typbyval, &typalign,
723 &typdelim, &typioparam,
724 &typinput, &typoutput,
725 &typcollation);
726
727 /*
728 * pg_node_tree values can't be inserted normally (pg_node_tree_in would
729 * just error out), so provide special cases for such columns that we
730 * would like to fill during bootstrap.
731 */
732 if (typoid == PG_NODE_TREEOID)
733 {
734 /* pg_proc.proargdefaults */
738 else /* maybe other cases later */
739 elog(ERROR, "can't handle pg_node_tree input for %s.%s",
741 NameStr(attr->attname));
742 }
743 else
744 {
745 /* Normal case */
746 values[i] = OidInputFunctionCall(typinput, value, typioparam, -1);
747 }
748
749 /*
750 * We use ereport not elog here so that parameters aren't evaluated unless
751 * the message is going to be printed, which generally it isn't
752 */
754 (errmsg_internal("inserted -> %s",
755 OidOutputFunctionCall(typoutput, values[i]))));
756}
757
758/* ----------------
759 * InsertOneProargdefaultsValue
760 *
761 * In general, proargdefaults can be a list of any expressions, but
762 * for bootstrap we only support a list of Const nodes. The input
763 * has the form of a text array, and we feed non-null elements to the
764 * typinput functions for the appropriate parameters.
765 * ----------------
766 */
767static void
769{
770 int pronargs;
774 bool *array_nulls;
775 int array_count;
777 char *nodestring;
778
779 /* The pg_proc columns we need to use must have been filled already */
781 "pronargs must come before proargdefaults");
783 "pronargdefaults must come before proargdefaults");
785 "proargtypes must come before proargdefaults");
787 elog(ERROR, "pronargs must not be null");
789 elog(ERROR, "proargtypes must not be null");
792 Assert(pronargs == proargtypes->dim1);
793
794 /* Parse the input string as an array value, then deconstruct to Datums */
798 Int32GetDatum(-1));
801
802 /* The values should correspond to the last N argtypes */
803 if (array_count > pronargs)
804 elog(ERROR, "too many proargdefaults entries");
805
806 /* Build the List of Const nodes */
808 for (int i = 0; i < array_count; i++)
809 {
810 Oid argtype = proargtypes->values[pronargs - array_count + i];
811 int16 typlen;
812 bool typbyval;
813 char typalign;
814 char typdelim;
815 Oid typioparam;
817 Oid typoutput;
818 Oid typcollation;
819 Datum defval;
820 bool defnull;
822
823 boot_get_type_io_data(argtype,
824 &typlen, &typbyval, &typalign,
825 &typdelim, &typioparam,
826 &typinput, &typoutput,
827 &typcollation);
828
830 if (defnull)
831 defval = (Datum) 0;
832 else
835 typioparam, -1);
836
837 defConst = makeConst(argtype,
838 -1, /* never any typmod */
839 typcollation,
840 typlen,
841 defval,
842 defnull,
843 typbyval);
845 }
846
847 /*
848 * Flatten the List to a node-tree string, then convert to a text datum,
849 * which is the storage representation of pg_node_tree.
850 */
854
855 /*
856 * Hack: fill in pronargdefaults with the right value. This is surely
857 * ugly, but it beats making the programmer do it.
858 */
861}
862
863/* ----------------
864 * InsertOneNull
865 * ----------------
866 */
867void
869{
870 elog(DEBUG4, "inserting column %d NULL", i);
871 Assert(i >= 0 && i < MAXATTR);
872 if (TupleDescAttr(boot_reldesc->rd_att, i)->attnotnull)
873 elog(ERROR,
874 "NULL value specified for not-null column \"%s\" of relation \"%s\"",
878 Nulls[i] = true;
879}
880
881/* ----------------
882 * cleanup
883 * ----------------
884 */
885static void
887{
888 if (boot_reldesc != NULL)
889 closerel(NULL);
890}
891
892/* ----------------
893 * populate_typ_list
894 *
895 * Load the Typ list by reading pg_type.
896 * ----------------
897 */
898static void
900{
901 Relation rel;
902 TableScanDesc scan;
905
906 Assert(Typ == NIL);
907
909 scan = table_beginscan_catalog(rel, 0, NULL);
911 while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
912 {
914 struct typmap *newtyp;
915
916 newtyp = palloc_object(struct typmap);
917 Typ = lappend(Typ, newtyp);
918
919 newtyp->am_oid = typForm->oid;
920 memcpy(&newtyp->am_typ, typForm, sizeof(newtyp->am_typ));
921 }
923 table_endscan(scan);
924 table_close(rel, NoLock);
925}
926
927/* ----------------
928 * gettype
929 *
930 * NB: this is really ugly; it will return an integer index into TypInfo[],
931 * and not an OID at all, until the first reference to a type not known in
932 * TypInfo[]. At that point it will read and cache pg_type in Typ,
933 * and subsequently return a real OID (and set the global pointer Ap to
934 * point at the found row in Typ). So caller must check whether Typ is
935 * still NIL to determine what the return value is!
936 * ----------------
937 */
938static Oid
940{
941 if (Typ != NIL)
942 {
943 ListCell *lc;
944
945 foreach(lc, Typ)
946 {
947 struct typmap *app = lfirst(lc);
948
949 if (strncmp(NameStr(app->am_typ.typname), type, NAMEDATALEN) == 0)
950 {
951 Ap = app;
952 return app->am_oid;
953 }
954 }
955
956 /*
957 * The type wasn't known; reload the pg_type contents and check again
958 * to handle composite types, added since last populating the list.
959 */
960
962 Typ = NIL;
964
965 /*
966 * Calling gettype would result in infinite recursion for types
967 * missing in pg_type, so just repeat the lookup.
968 */
969 foreach(lc, Typ)
970 {
971 struct typmap *app = lfirst(lc);
972
973 if (strncmp(NameStr(app->am_typ.typname), type, NAMEDATALEN) == 0)
974 {
975 Ap = app;
976 return app->am_oid;
977 }
978 }
979 }
980 else
981 {
982 int i;
983
984 for (i = 0; i < n_types; i++)
985 {
986 if (strncmp(type, TypInfo[i].name, NAMEDATALEN) == 0)
987 return i;
988 }
989 /* Not in TypInfo, so we'd better be able to read pg_type now */
990 elog(DEBUG4, "external type: %s", type);
992 return gettype(type);
993 }
994 elog(ERROR, "unrecognized type \"%s\"", type);
995 /* not reached, here to make compiler happy */
996 return 0;
997}
998
999/* ----------------
1000 * boot_get_type_io_data
1001 *
1002 * Obtain type I/O information at bootstrap time. This intentionally has
1003 * an API very close to that of lsyscache.c's get_type_io_data, except that
1004 * we only support obtaining the typinput and typoutput routines, not
1005 * the binary I/O routines, and we also return the type's collation.
1006 * This is exported so that array_in and array_out can be made to work
1007 * during early bootstrap.
1008 * ----------------
1009 */
1010void
1012 int16 *typlen,
1013 bool *typbyval,
1014 char *typalign,
1015 char *typdelim,
1016 Oid *typioparam,
1017 Oid *typinput,
1018 Oid *typoutput,
1019 Oid *typcollation)
1020{
1021 if (Typ != NIL)
1022 {
1023 /* We have the boot-time contents of pg_type, so use it */
1024 struct typmap *ap = NULL;
1025 ListCell *lc;
1026
1027 foreach(lc, Typ)
1028 {
1029 ap = lfirst(lc);
1030 if (ap->am_oid == typid)
1031 break;
1032 }
1033
1034 if (!ap || ap->am_oid != typid)
1035 elog(ERROR, "type OID %u not found in Typ list", typid);
1036
1037 *typlen = ap->am_typ.typlen;
1038 *typbyval = ap->am_typ.typbyval;
1039 *typalign = ap->am_typ.typalign;
1040 *typdelim = ap->am_typ.typdelim;
1041
1042 /* XXX this logic must match getTypeIOParam() */
1043 if (OidIsValid(ap->am_typ.typelem))
1044 *typioparam = ap->am_typ.typelem;
1045 else
1046 *typioparam = typid;
1047
1048 *typinput = ap->am_typ.typinput;
1049 *typoutput = ap->am_typ.typoutput;
1050
1051 *typcollation = ap->am_typ.typcollation;
1052 }
1053 else
1054 {
1055 /* We don't have pg_type yet, so use the hard-wired TypInfo array */
1056 int typeindex;
1057
1058 for (typeindex = 0; typeindex < n_types; typeindex++)
1059 {
1060 if (TypInfo[typeindex].oid == typid)
1061 break;
1062 }
1063 if (typeindex >= n_types)
1064 elog(ERROR, "type OID %u not found in TypInfo", typid);
1065
1066 *typlen = TypInfo[typeindex].len;
1067 *typbyval = TypInfo[typeindex].byval;
1069 /* We assume typdelim is ',' for all boot-time types */
1070 *typdelim = ',';
1071
1072 /* XXX this logic must match getTypeIOParam() */
1073 if (OidIsValid(TypInfo[typeindex].elem))
1074 *typioparam = TypInfo[typeindex].elem;
1075 else
1076 *typioparam = typid;
1077
1079 *typoutput = TypInfo[typeindex].outproc;
1080
1081 *typcollation = TypInfo[typeindex].collation;
1082 }
1083}
1084
1085/* ----------------
1086 * boot_get_role_oid
1087 *
1088 * Look up a role name at bootstrap time. This is equivalent to
1089 * get_role_oid(rolname, true): return the role OID or InvalidOid if
1090 * not found. We only need to cope with built-in role names.
1091 * ----------------
1092 */
1093Oid
1095{
1096 for (int i = 0; i < lengthof(RolInfo); i++)
1097 {
1098 if (strcmp(RolInfo[i].rolname, rolname) == 0)
1099 return RolInfo[i].oid;
1100 }
1101 return InvalidOid;
1102}
1103
1104/* ----------------
1105 * AllocateAttribute
1106 *
1107 * Note: bootstrap never sets any per-column ACLs, so we only need
1108 * ATTRIBUTE_FIXED_PART_SIZE space per attribute.
1109 * ----------------
1110 */
1111static Form_pg_attribute
1117
1118/*
1119 * index_register() -- record an index that has been set up for building
1120 * later.
1121 *
1122 * At bootstrap time, we define a bunch of indexes on system catalogs.
1123 * We postpone actually building the indexes until just before we're
1124 * finished with initialization, however. This is because the indexes
1125 * themselves have catalog entries, and those have to be included in the
1126 * indexes on those catalogs. Doing it in two phases is the simplest
1127 * way of making sure the indexes have the right contents at the end.
1128 */
1129void
1131 Oid ind,
1132 const IndexInfo *indexInfo)
1133{
1136
1137 /*
1138 * XXX mao 10/31/92 -- don't gc index reldescs, associated info at
1139 * bootstrap time. we'll declare the indexes now, but want to create them
1140 * later.
1141 */
1142
1143 if (nogc == NULL)
1145 "BootstrapNoGC",
1147
1149
1151 newind->il_heap = heap;
1152 newind->il_ind = ind;
1153 newind->il_info = palloc_object(IndexInfo);
1154
1155 memcpy(newind->il_info, indexInfo, sizeof(IndexInfo));
1156 /* expressions will likely be null, but may as well copy it */
1157 newind->il_info->ii_Expressions =
1158 copyObject(indexInfo->ii_Expressions);
1159 newind->il_info->ii_ExpressionsState = NIL;
1160 /* predicate will likely be null, but may as well copy it */
1161 newind->il_info->ii_Predicate =
1162 copyObject(indexInfo->ii_Predicate);
1163 newind->il_info->ii_PredicateState = NULL;
1164 /* no exclusion constraints at bootstrap time, so no need to copy */
1165 Assert(indexInfo->ii_ExclusionOps == NULL);
1166 Assert(indexInfo->ii_ExclusionProcs == NULL);
1167 Assert(indexInfo->ii_ExclusionStrats == NULL);
1168
1169 newind->il_next = ILHead;
1170 ILHead = newind;
1171
1173}
1174
1175
1176/*
1177 * build_indices -- fill in all the indexes registered earlier
1178 */
1179void
1181{
1182 for (; ILHead != NULL; ILHead = ILHead->il_next)
1183 {
1184 Relation heap;
1185 Relation ind;
1186
1187 /* need not bother with locks during bootstrap */
1188 heap = table_open(ILHead->il_heap, NoLock);
1190
1191 index_build(heap, ind, ILHead->il_info, false, false, false);
1192
1194 table_close(heap, NoLock);
1195 }
1196}
#define DatumGetArrayTypeP(X)
Definition array.h:261
void deconstruct_array_builtin(const ArrayType *array, Oid elmtype, Datum **elemsp, bool **nullsp, int *nelemsp)
#define write_stderr(str)
Definition parallel.c:186
static Oid gettype(char *type)
Definition bootstrap.c:939
static Datum values[MAXATTR]
Definition bootstrap.c:190
void closerel(char *relname)
Definition bootstrap.c:529
static void populate_typ_list(void)
Definition bootstrap.c:899
static void CheckerModeMain(void)
Definition bootstrap.c:219
void build_indices(void)
Definition bootstrap.c:1180
void InsertOneValue(char *value, int i)
Definition bootstrap.c:701
static void cleanup(void)
Definition bootstrap.c:886
static const int n_types
Definition bootstrap.c:142
void InsertOneNull(int i)
Definition bootstrap.c:868
static const struct rolinfo RolInfo[]
Definition bootstrap.c:169
static const struct typinfo TypInfo[]
Definition bootstrap.c:93
Relation boot_reldesc
Definition bootstrap.c:64
static struct typmap * Ap
Definition bootstrap.c:151
static List * Typ
Definition bootstrap.c:150
void InsertOneTuple(void)
Definition bootstrap.c:673
void boot_get_type_io_data(Oid typid, int16 *typlen, bool *typbyval, char *typalign, char *typdelim, Oid *typioparam, Oid *typinput, Oid *typoutput, Oid *typcollation)
Definition bootstrap.c:1011
static void InsertOneProargdefaultsValue(char *value)
Definition bootstrap.c:768
struct _IndexList IndexList
static void bootstrap_signals(void)
Definition bootstrap.c:457
Form_pg_attribute attrtypes[MAXATTR]
Definition bootstrap.c:66
Oid boot_get_role_oid(const char *rolname)
Definition bootstrap.c:1094
int numattr
Definition bootstrap.c:67
static Form_pg_attribute AllocateAttribute(void)
Definition bootstrap.c:1112
static MemoryContext nogc
Definition bootstrap.c:193
void BootstrapModeMain(int argc, char *argv[], bool check_only)
Definition bootstrap.c:237
void DefineAttr(char *name, char *type, int attnum, int nullness)
Definition bootstrap.c:566
static bool Nulls[MAXATTR]
Definition bootstrap.c:191
void index_register(Oid heap, Oid ind, const IndexInfo *indexInfo)
Definition bootstrap.c:1130
void boot_openrel(char *relname)
Definition bootstrap.c:484
static IndexList * ILHead
Definition bootstrap.c:209
#define BOOTCOL_NULL_FORCE_NULL
Definition bootstrap.h:29
int boot_yylex_init(yyscan_t *yyscannerp)
#define MAXATTR
Definition bootstrap.h:26
#define BOOTCOL_NULL_FORCE_NOT_NULL
Definition bootstrap.h:30
#define BOOTCOL_NULL_AUTO
Definition bootstrap.h:28
int boot_yyparse(yyscan_t yyscanner)
#define CStringGetTextDatum(s)
Definition builtins.h:98
#define NameStr(name)
Definition c.h:835
#define Assert(condition)
Definition c.h:943
int16_t int16
Definition c.h:619
uint32_t uint32
Definition c.h:624
#define lengthof(array)
Definition c.h:873
#define pg_fallthrough
Definition c.h:161
#define MemSet(start, val, len)
Definition c.h:1107
#define StaticAssertDecl(condition, errmessage)
Definition c.h:1008
#define OidIsValid(objectId)
Definition c.h:858
@ PG_DATA_CHECKSUM_VERSION
Definition checksum.h:29
@ PG_DATA_CHECKSUM_OFF
Definition checksum.h:28
memcpy(sums, checksumBaseOffsets, sizeof(checksumBaseOffsets))
void * yyscan_t
Definition cubedata.h:65
int errcode(int sqlerrcode)
Definition elog.c:874
int int errmsg_internal(const char *fmt,...) pg_attribute_printf(1
#define WARNING
Definition elog.h:37
#define ERROR
Definition elog.h:40
#define elog(elevel,...)
Definition elog.h:228
#define ereport(elevel,...)
Definition elog.h:152
#define DEBUG4
Definition elog.h:28
void set_max_safe_fds(void)
Definition fd.c:1045
#define palloc_object(type)
Definition fe_memutils.h:74
Datum OidInputFunctionCall(Oid functionId, char *str, Oid typioparam, int32 typmod)
Definition fmgr.c:1755
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition fmgr.c:1764
#define OidFunctionCall3(functionId, arg1, arg2, arg3)
Definition fmgr.h:726
bool IsUnderPostmaster
Definition globals.c:122
char OutputFileName[MAXPGPATH]
Definition globals.c:81
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition guc.c:4234
bool SelectConfigFiles(const char *userDoption, const char *progname)
Definition guc.c:1656
void ParseLongOption(const char *string, char **name, char **value)
Definition guc.c:6243
void InitializeGUCOptions(void)
Definition guc.c:1408
@ 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
HeapTuple heap_getnext(TableScanDesc sscan, ScanDirection direction)
Definition heapam.c:1435
void simple_heap_insert(Relation relation, HeapTuple tup)
Definition heapam.c:2659
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition heaptuple.c:1025
void heap_freetuple(HeapTuple htup)
Definition heaptuple.c:1372
static void * GETSTRUCT(const HeapTupleData *tuple)
void index_build(Relation heapRelation, Relation indexRelation, IndexInfo *indexInfo, bool isreindex, bool parallel, bool progress)
Definition index.c:3021
void index_close(Relation relation, LOCKMODE lockmode)
Definition indexam.c:178
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition indexam.c:134
static struct @177 value
void proc_exit(int code)
Definition ipc.c:105
void RegisterBuiltinShmemCallbacks(void)
Definition ipci.c:168
void CreateSharedMemoryAndSemaphores(void)
Definition ipci.c:120
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
Const * makeConst(Oid consttype, int32 consttypmod, Oid constcollid, int constlen, Datum constvalue, bool constisnull, bool constbyval)
Definition makefuncs.c:350
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:490
@ BootstrapProcessing
Definition miscadmin.h:488
#define SetProcessingMode(mode)
Definition miscadmin.h:501
void ChangeToDataDir(void)
Definition miscinit.c:410
void InitStandaloneProcess(const char *argv0)
Definition miscinit.c:176
bool IgnoreSystemIndexes
Definition miscinit.c:82
void checkDataDir(void)
Definition miscinit.c:297
void CreateDataDirLockFile(bool amPostmaster)
Definition miscinit.c:1465
void namestrcpy(Name name, const char *str)
Definition name.c:233
#define copyObject(obj)
Definition nodes.h:232
static char * errmsg
char * nodeToString(const void *obj)
Definition outfuncs.c:811
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:124
NameData attname
#define ATTRIBUTE_FIXED_PART_SIZE
int16 attnum
int16 attlen
FormData_pg_attribute * Form_pg_attribute
bool attnotnull
NameData rolname
Definition pg_authid.h:36
NameData relname
Definition pg_class.h:40
#define NAMEDATALEN
#define MAXPGPATH
int pg_getopt_next(pg_getopt_ctx *ctx)
void pg_getopt_start(pg_getopt_ctx *ctx, int nargc, char *const *nargv, const char *ostr)
#define lfirst(lc)
Definition pg_list.h:172
#define NIL
Definition pg_list.h:68
int16 pronargs
Definition pg_proc.h:83
END_CATALOG_STRUCT typedef FormData_pg_type * Form_pg_type
Definition pg_type.h:265
char typalign
Definition pg_type.h:178
FormData_pg_type
Definition pg_type.h:256
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:167
static Datum PointerGetDatum(const void *X)
Definition postgres.h:342
static Datum Int16GetDatum(int16 X)
Definition postgres.h:172
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:252
static char * DatumGetCString(Datum X)
Definition postgres.h:355
uint64_t Datum
Definition postgres.h:70
static Pointer DatumGetPointer(Datum X)
Definition postgres.h:332
static Datum CStringGetDatum(const char *X)
Definition postgres.h:370
static Datum Int32GetDatum(int32 X)
Definition postgres.h:212
static int16 DatumGetInt16(Datum X)
Definition postgres.h:162
#define InvalidOid
unsigned int Oid
void InitializeMaxBackends(void)
Definition postinit.c:559
void BaseInit(void)
Definition postinit.c:616
void InitializeFastPathLocks(void)
Definition postinit.c:584
void InitPostgres(const char *in_dbname, Oid dboid, const char *username, Oid useroid, uint32 flags, char *out_dbname)
Definition postinit.c:719
@ DISPATCH_POSTMASTER
Definition postmaster.h:139
static int fb(int x)
char * psprintf(const char *fmt,...)
Definition psprintf.c:43
#define RelationGetRelid(relation)
Definition rel.h:516
#define RelationGetDescr(relation)
Definition rel.h:542
#define RelationGetNumberOfAttributes(relation)
Definition rel.h:522
#define RelationGetRelationName(relation)
Definition rel.h:550
void RelationMapFinishBootstrap(void)
Definition relmapper.c:626
@ ForwardScanDirection
Definition sdir.h:28
void ShmemCallRequestCallbacks(void)
Definition shmem.c:979
void InitProcess(void)
Definition proc.c:392
uint16 * ii_ExclusionStrats
Definition execnodes.h:206
Oid * ii_ExclusionOps
Definition execnodes.h:202
List * ii_Expressions
Definition execnodes.h:192
Oid * ii_ExclusionProcs
Definition execnodes.h:204
List * ii_Predicate
Definition execnodes.h:197
Definition pg_list.h:54
TupleDesc rd_att
Definition rel.h:112
struct _IndexList * il_next
Definition bootstrap.c:206
IndexInfo * il_info
Definition bootstrap.c:205
Definition c.h:815
const char * rolname
Definition bootstrap.c:165
Oid oid
Definition bootstrap.c:166
char align
Definition bootstrap.c:86
Oid outproc
Definition bootstrap.c:90
int16 len
Definition bootstrap.c:84
Oid oid
Definition bootstrap.c:82
bool byval
Definition bootstrap.c:85
char name[NAMEDATALEN]
Definition bootstrap.c:81
Oid collation
Definition bootstrap.c:88
Oid elem
Definition bootstrap.c:83
char storage
Definition bootstrap.c:87
Oid inproc
Definition bootstrap.c:89
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:1061
char * flag(int b)
Definition test-ctype.c:33
#define InvalidCompressionMethod
TupleDesc CreateTupleDesc(int natts, Form_pg_attribute *attrs)
Definition tupdesc.c:216
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition tupdesc.h:178
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:3109
void CommitTransactionCommand(void)
Definition xact.c:3207
void BootStrapXLOG(uint32 data_checksum_version)
Definition xlog.c:5454