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