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"
29#include "catalog/pg_proc.h"
30#include "catalog/pg_type.h"
31#include "common/link-canary.h"
32#include "miscadmin.h"
33#include "nodes/makefuncs.h"
34#include "pg_getopt.h"
36#include "storage/bufpage.h"
37#include "storage/ipc.h"
38#include "storage/proc.h"
39#include "utils/builtins.h"
40#include "utils/fmgroids.h"
41#include "utils/guc.h"
42#include "utils/memutils.h"
43#include "utils/rel.h"
44#include "utils/relmapper.h"
45
46
47static void CheckerModeMain(void);
48static void bootstrap_signals(void);
50static void InsertOneProargdefaultsValue(char *value);
51static void populate_typ_list(void);
52static Oid gettype(char *type);
53static void cleanup(void);
54
55/* ----------------
56 * global variables
57 * ----------------
58 */
59
60Relation boot_reldesc; /* current relation descriptor */
61
62Form_pg_attribute attrtypes[MAXATTR]; /* points to attribute info */
63int numattr; /* number of attributes for cur. rel */
64
65
66/*
67 * Basic information associated with each type. This is used before
68 * pg_type is filled, so it has to cover the datatypes used as column types
69 * in the core "bootstrapped" catalogs.
70 *
71 * XXX several of these input/output functions do catalog scans
72 * (e.g., F_REGPROCIN scans pg_proc). this obviously creates some
73 * order dependencies in the catalog creation process.
74 */
88
89static const struct typinfo TypInfo[] = {
90 {"bool", BOOLOID, 0, 1, true, TYPALIGN_CHAR, TYPSTORAGE_PLAIN, InvalidOid,
92 {"bytea", BYTEAOID, 0, -1, false, TYPALIGN_INT, TYPSTORAGE_EXTENDED, InvalidOid,
94 {"char", CHAROID, 0, 1, true, TYPALIGN_CHAR, TYPSTORAGE_PLAIN, InvalidOid,
96 {"cstring", CSTRINGOID, 0, -2, false, TYPALIGN_CHAR, TYPSTORAGE_PLAIN, InvalidOid,
98 {"int2", INT2OID, 0, 2, true, TYPALIGN_SHORT, TYPSTORAGE_PLAIN, InvalidOid,
100 {"int4", INT4OID, 0, 4, true, TYPALIGN_INT, TYPSTORAGE_PLAIN, InvalidOid,
102 {"int8", INT8OID, 0, 8, true, TYPALIGN_DOUBLE, TYPSTORAGE_PLAIN, InvalidOid,
104 {"float4", FLOAT4OID, 0, 4, true, TYPALIGN_INT, TYPSTORAGE_PLAIN, InvalidOid,
106 {"float8", FLOAT8OID, 0, 8, true, TYPALIGN_DOUBLE, TYPSTORAGE_PLAIN, InvalidOid,
110 {"regproc", REGPROCOID, 0, 4, true, TYPALIGN_INT, TYPSTORAGE_PLAIN, InvalidOid,
114 {"jsonb", JSONBOID, 0, -1, false, TYPALIGN_INT, TYPSTORAGE_EXTENDED, InvalidOid,
116 {"oid", OIDOID, 0, 4, true, TYPALIGN_INT, TYPSTORAGE_PLAIN, InvalidOid,
120 {"int2vector", INT2VECTOROID, INT2OID, -1, false, TYPALIGN_INT, TYPSTORAGE_PLAIN, InvalidOid,
122 {"oidvector", OIDVECTOROID, OIDOID, -1, false, TYPALIGN_INT, TYPSTORAGE_PLAIN, InvalidOid,
134};
135
136static const int n_types = sizeof(TypInfo) / sizeof(struct typinfo);
137
138struct typmap
139{ /* a hack */
142};
143
144static List *Typ = NIL; /* List of struct typmap* */
145static struct typmap *Ap = NULL;
146
147static Datum values[MAXATTR]; /* current row's attribute values */
148static bool Nulls[MAXATTR];
149
150static MemoryContext nogc = NULL; /* special no-gc mem context */
151
152/*
153 * At bootstrap time, we first declare all the indices to be built, and
154 * then build them. The IndexList structure stores enough information
155 * to allow us to build the indices after they've been declared.
156 */
157
165
167
168
169/*
170 * In shared memory checker mode, all we really want to do is create shared
171 * memory and semaphores (just to prove we can do it with the current GUC
172 * settings). Since, in fact, that was already done by
173 * CreateSharedMemoryAndSemaphores(), we have nothing more to do here.
174 */
175static void
177{
178 proc_exit(0);
179}
180
181/*
182 * The main entry point for running the backend in bootstrap mode
183 *
184 * The bootstrap mode is used to initialize the template database.
185 * The bootstrap backend doesn't speak SQL, but instead expects
186 * commands in a special bootstrap language.
187 *
188 * When check_only is true, startup is done only far enough to verify that
189 * the current configuration, particularly the passed in options pertaining
190 * to shared memory sizing, options work (or at least do not cause an error
191 * up to shared memory creation).
192 */
193void
194BootstrapModeMain(int argc, char *argv[], bool check_only)
195{
196 int i;
197 char *progname = argv[0];
198 int flag;
199 char *userDoption = NULL;
200 uint32 bootstrap_data_checksum_version = 0; /* No checksum */
201 yyscan_t scanner;
202
204
205 InitStandaloneProcess(argv[0]);
206
207 /* Set defaults, to be overridden by explicit options below */
209
210 /* an initial --boot or --check should be present */
211 Assert(argc > 1
212 && (strcmp(argv[1], "--boot") == 0
213 || strcmp(argv[1], "--check") == 0));
214 argv++;
215 argc--;
216
217 while ((flag = getopt(argc, argv, "B:c:d:D:Fkr:X:-:")) != -1)
218 {
219 switch (flag)
220 {
221 case 'B':
222 SetConfigOption("shared_buffers", optarg, PGC_POSTMASTER, PGC_S_ARGV);
223 break;
224 case '-':
225
226 /*
227 * Error if the user misplaced a special must-be-first option
228 * for dispatching to a subprogram. parse_dispatch_option()
229 * returns DISPATCH_POSTMASTER if it doesn't find a match, so
230 * error for anything else.
231 */
235 errmsg("--%s must be first argument", optarg)));
236
238 case 'c':
239 {
240 char *name,
241 *value;
242
244 if (!value)
245 {
246 if (flag == '-')
249 errmsg("--%s requires a value",
250 optarg)));
251 else
254 errmsg("-c %s requires a value",
255 optarg)));
256 }
257
259 pfree(name);
260 pfree(value);
261 break;
262 }
263 case 'D':
265 break;
266 case 'd':
267 {
268 /* Turn on debugging for the bootstrap process. */
269 char *debugstr;
270
271 debugstr = psprintf("debug%s", optarg);
272 SetConfigOption("log_min_messages", debugstr,
274 SetConfigOption("client_min_messages", debugstr,
277 }
278 break;
279 case 'F':
280 SetConfigOption("fsync", "false", PGC_POSTMASTER, PGC_S_ARGV);
281 break;
282 case 'k':
284 break;
285 case 'r':
287 break;
288 case 'X':
290 break;
291 default:
292 write_stderr("Try \"%s --help\" for more information.\n",
293 progname);
294 proc_exit(1);
295 break;
296 }
297 }
298
299 if (argc != optind)
300 {
301 write_stderr("%s: invalid command-line arguments\n", progname);
302 proc_exit(1);
303 }
304
305 /* Acquire configuration parameters */
307 proc_exit(1);
308
309 /*
310 * Validate we have been given a reasonable-looking DataDir and change
311 * into it
312 */
313 checkDataDir();
315
317
319 IgnoreSystemIndexes = true;
320
322
323 /*
324 * Even though bootstrapping runs in single-process mode, initialize
325 * postmaster child slots array so that --check can detect running out of
326 * shared memory or other resources if max_connections is set too high.
327 */
329
331
333
334 /*
335 * Estimate number of openable files. This is essential too in --check
336 * mode, because on some platforms semaphores count as open files.
337 */
339
340 /*
341 * XXX: It might make sense to move this into its own function at some
342 * point. Right now it seems like it'd cause more code duplication than
343 * it's worth.
344 */
345 if (check_only)
346 {
349 abort();
350 }
351
352 /*
353 * Do backend-like initialization for bootstrap mode
354 */
355 InitProcess();
356
357 BaseInit();
358
361
362 /*
363 * To ensure that src/common/link-canary.c is linked into the backend, we
364 * must call it from somewhere. Here is as good as anywhere.
365 */
367 elog(ERROR, "backend is incorrectly linked to frontend functions");
368
370
371 /* Initialize stuff for bootstrap-file processing */
372 for (i = 0; i < MAXATTR; i++)
373 {
374 attrtypes[i] = NULL;
375 Nulls[i] = false;
376 }
377
378 if (boot_yylex_init(&scanner) != 0)
379 elog(ERROR, "yylex_init() failed: %m");
380
381 /*
382 * Process bootstrap input.
383 */
385 boot_yyparse(scanner);
387
388 /*
389 * We should now know about all mapped relations, so it's okay to write
390 * out the initial relation mapping files.
391 */
393
394 /* Clean up and exit */
395 cleanup();
396 proc_exit(0);
397}
398
399
400/* ----------------------------------------------------------------
401 * misc functions
402 * ----------------------------------------------------------------
403 */
404
405/*
406 * Set up signal handling for a bootstrap process
407 */
408static void
410{
412
413 /*
414 * We don't actually need any non-default signal handling in bootstrap
415 * mode; "curl up and die" is a sufficient response for all these cases.
416 * Let's set that handling explicitly, as documentation if nothing else.
417 */
422}
423
424/* ----------------------------------------------------------------
425 * MANUAL BACKEND INTERACTIVE INTERFACE COMMANDS
426 * ----------------------------------------------------------------
427 */
428
429/* ----------------
430 * boot_openrel
431 *
432 * Execute BKI OPEN command.
433 * ----------------
434 */
435void
437{
438 int i;
439
440 if (strlen(relname) >= NAMEDATALEN)
441 relname[NAMEDATALEN - 1] = '\0';
442
443 /*
444 * pg_type must be filled before any OPEN command is executed, hence we
445 * can now populate Typ if we haven't yet.
446 */
447 if (Typ == NIL)
449
450 if (boot_reldesc != NULL)
451 closerel(NULL);
452
453 elog(DEBUG4, "open relation %s, attrsize %d",
455
458 for (i = 0; i < numattr; i++)
459 {
460 if (attrtypes[i] == NULL)
465
466 {
468
469 elog(DEBUG4, "create attribute %d name %s len %d num %d type %u",
470 i, NameStr(at->attname), at->attlen, at->attnum,
471 at->atttypid);
472 }
473 }
474}
475
476/* ----------------
477 * closerel
478 * ----------------
479 */
480void
482{
483 if (relname)
484 {
485 if (boot_reldesc)
486 {
488 elog(ERROR, "close of %s when %s was expected",
490 }
491 else
492 elog(ERROR, "close of %s before any relation was opened",
493 relname);
494 }
495
496 if (boot_reldesc == NULL)
497 elog(ERROR, "no open relation to close");
498 else
499 {
500 elog(DEBUG4, "close relation %s",
504 }
505}
506
507
508
509/* ----------------
510 * DEFINEATTR()
511 *
512 * define a <field,type> pair
513 * if there are n fields in a relation to be created, this routine
514 * will be called n times
515 * ----------------
516 */
517void
518DefineAttr(char *name, char *type, int attnum, int nullness)
519{
520 Oid typeoid;
521
522 if (boot_reldesc != NULL)
523 {
524 elog(WARNING, "no open relations allowed with CREATE command");
525 closerel(NULL);
526 }
527
528 if (attrtypes[attnum] == NULL)
531
533 elog(DEBUG4, "column %s %s", NameStr(attrtypes[attnum]->attname), type);
534 attrtypes[attnum]->attnum = attnum + 1;
535
536 typeoid = gettype(type);
537
538 if (Typ != NIL)
539 {
540 attrtypes[attnum]->atttypid = Ap->am_oid;
541 attrtypes[attnum]->attlen = Ap->am_typ.typlen;
542 attrtypes[attnum]->attbyval = Ap->am_typ.typbyval;
543 attrtypes[attnum]->attalign = Ap->am_typ.typalign;
544 attrtypes[attnum]->attstorage = Ap->am_typ.typstorage;
545 attrtypes[attnum]->attcompression = InvalidCompressionMethod;
546 attrtypes[attnum]->attcollation = Ap->am_typ.typcollation;
547 /* if an array type, assume 1-dimensional attribute */
548 if (Ap->am_typ.typelem != InvalidOid && Ap->am_typ.typlen < 0)
549 attrtypes[attnum]->attndims = 1;
550 else
551 attrtypes[attnum]->attndims = 0;
552 }
553 else
554 {
555 attrtypes[attnum]->atttypid = TypInfo[typeoid].oid;
556 attrtypes[attnum]->attlen = TypInfo[typeoid].len;
557 attrtypes[attnum]->attbyval = TypInfo[typeoid].byval;
558 attrtypes[attnum]->attalign = TypInfo[typeoid].align;
559 attrtypes[attnum]->attstorage = TypInfo[typeoid].storage;
560 attrtypes[attnum]->attcompression = InvalidCompressionMethod;
561 attrtypes[attnum]->attcollation = TypInfo[typeoid].collation;
562 /* if an array type, assume 1-dimensional attribute */
563 if (TypInfo[typeoid].elem != InvalidOid &&
564 attrtypes[attnum]->attlen < 0)
565 attrtypes[attnum]->attndims = 1;
566 else
567 attrtypes[attnum]->attndims = 0;
568 }
569
570 /*
571 * If a system catalog column is collation-aware, force it to use C
572 * collation, so that its behavior is independent of the database's
573 * collation. This is essential to allow template0 to be cloned with a
574 * different database collation.
575 */
576 if (OidIsValid(attrtypes[attnum]->attcollation))
577 attrtypes[attnum]->attcollation = C_COLLATION_OID;
578
579 attrtypes[attnum]->atttypmod = -1;
580 attrtypes[attnum]->attislocal = true;
581
583 {
584 attrtypes[attnum]->attnotnull = true;
585 }
587 {
588 attrtypes[attnum]->attnotnull = false;
589 }
590 else
591 {
593
594 /*
595 * Mark as "not null" if type is fixed-width and prior columns are
596 * likewise fixed-width and not-null. This corresponds to case where
597 * column can be accessed directly via C struct declaration.
598 */
599 if (attrtypes[attnum]->attlen > 0)
600 {
601 int i;
602
603 /* check earlier attributes */
604 for (i = 0; i < attnum; i++)
605 {
606 if (attrtypes[i]->attlen <= 0 ||
608 break;
609 }
610 if (i == attnum)
611 attrtypes[attnum]->attnotnull = true;
612 }
613 }
614}
615
616
617/* ----------------
618 * InsertOneTuple
619 *
620 * If objectid is not zero, it is a specific OID to assign to the tuple.
621 * Otherwise, an OID will be assigned (if necessary) by heap_insert.
622 * ----------------
623 */
624void
626{
627 HeapTuple tuple;
628 TupleDesc tupDesc;
629 int i;
630
631 elog(DEBUG4, "inserting row with %d columns", numattr);
632
634 tuple = heap_form_tuple(tupDesc, values, Nulls);
635 pfree(tupDesc); /* just free's tupDesc, not the attrtypes */
636
638 heap_freetuple(tuple);
639 elog(DEBUG4, "row inserted");
640
641 /*
642 * Reset null markers for next tuple
643 */
644 for (i = 0; i < numattr; i++)
645 Nulls[i] = false;
646}
647
648/* ----------------
649 * InsertOneValue
650 * ----------------
651 */
652void
654{
656 Oid typoid;
657 int16 typlen;
658 bool typbyval;
659 char typalign;
660 char typdelim;
661 Oid typioparam;
663 Oid typoutput;
664 Oid typcollation;
665
666 Assert(i >= 0 && i < MAXATTR);
667
668 elog(DEBUG4, "inserting column %d value \"%s\"", i, value);
669
671 typoid = attr->atttypid;
672
674 &typlen, &typbyval, &typalign,
675 &typdelim, &typioparam,
676 &typinput, &typoutput,
677 &typcollation);
678
679 /*
680 * pg_node_tree values can't be inserted normally (pg_node_tree_in would
681 * just error out), so provide special cases for such columns that we
682 * would like to fill during bootstrap.
683 */
684 if (typoid == PG_NODE_TREEOID)
685 {
686 /* pg_proc.proargdefaults */
690 else /* maybe other cases later */
691 elog(ERROR, "can't handle pg_node_tree input for %s.%s",
693 NameStr(attr->attname));
694 }
695 else
696 {
697 /* Normal case */
698 values[i] = OidInputFunctionCall(typinput, value, typioparam, -1);
699 }
700
701 /*
702 * We use ereport not elog here so that parameters aren't evaluated unless
703 * the message is going to be printed, which generally it isn't
704 */
706 (errmsg_internal("inserted -> %s",
707 OidOutputFunctionCall(typoutput, values[i]))));
708}
709
710/* ----------------
711 * InsertOneProargdefaultsValue
712 *
713 * In general, proargdefaults can be a list of any expressions, but
714 * for bootstrap we only support a list of Const nodes. The input
715 * has the form of a text array, and we feed non-null elements to the
716 * typinput functions for the appropriate parameters.
717 * ----------------
718 */
719static void
721{
722 int pronargs;
726 bool *array_nulls;
727 int array_count;
729 char *nodestring;
730
731 /* The pg_proc columns we need to use must have been filled already */
733 "pronargs must come before proargdefaults");
735 "pronargdefaults must come before proargdefaults");
737 "proargtypes must come before proargdefaults");
739 elog(ERROR, "pronargs must not be null");
741 elog(ERROR, "proargtypes must not be null");
744 Assert(pronargs == proargtypes->dim1);
745
746 /* Parse the input string as an array value, then deconstruct to Datums */
750 Int32GetDatum(-1));
753
754 /* The values should correspond to the last N argtypes */
755 if (array_count > pronargs)
756 elog(ERROR, "too many proargdefaults entries");
757
758 /* Build the List of Const nodes */
760 for (int i = 0; i < array_count; i++)
761 {
762 Oid argtype = proargtypes->values[pronargs - array_count + i];
763 int16 typlen;
764 bool typbyval;
765 char typalign;
766 char typdelim;
767 Oid typioparam;
769 Oid typoutput;
770 Oid typcollation;
771 Datum defval;
772 bool defnull;
774
775 boot_get_type_io_data(argtype,
776 &typlen, &typbyval, &typalign,
777 &typdelim, &typioparam,
778 &typinput, &typoutput,
779 &typcollation);
780
782 if (defnull)
783 defval = (Datum) 0;
784 else
787 typioparam, -1);
788
789 defConst = makeConst(argtype,
790 -1, /* never any typmod */
791 typcollation,
792 typlen,
793 defval,
794 defnull,
795 typbyval);
797 }
798
799 /*
800 * Flatten the List to a node-tree string, then convert to a text datum,
801 * which is the storage representation of pg_node_tree.
802 */
806
807 /*
808 * Hack: fill in pronargdefaults with the right value. This is surely
809 * ugly, but it beats making the programmer do it.
810 */
813}
814
815/* ----------------
816 * InsertOneNull
817 * ----------------
818 */
819void
821{
822 elog(DEBUG4, "inserting column %d NULL", i);
823 Assert(i >= 0 && i < MAXATTR);
824 if (TupleDescAttr(boot_reldesc->rd_att, i)->attnotnull)
825 elog(ERROR,
826 "NULL value specified for not-null column \"%s\" of relation \"%s\"",
830 Nulls[i] = true;
831}
832
833/* ----------------
834 * cleanup
835 * ----------------
836 */
837static void
839{
840 if (boot_reldesc != NULL)
841 closerel(NULL);
842}
843
844/* ----------------
845 * populate_typ_list
846 *
847 * Load the Typ list by reading pg_type.
848 * ----------------
849 */
850static void
852{
853 Relation rel;
854 TableScanDesc scan;
857
858 Assert(Typ == NIL);
859
861 scan = table_beginscan_catalog(rel, 0, NULL);
863 while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
864 {
866 struct typmap *newtyp;
867
868 newtyp = palloc_object(struct typmap);
869 Typ = lappend(Typ, newtyp);
870
871 newtyp->am_oid = typForm->oid;
872 memcpy(&newtyp->am_typ, typForm, sizeof(newtyp->am_typ));
873 }
875 table_endscan(scan);
876 table_close(rel, NoLock);
877}
878
879/* ----------------
880 * gettype
881 *
882 * NB: this is really ugly; it will return an integer index into TypInfo[],
883 * and not an OID at all, until the first reference to a type not known in
884 * TypInfo[]. At that point it will read and cache pg_type in Typ,
885 * and subsequently return a real OID (and set the global pointer Ap to
886 * point at the found row in Typ). So caller must check whether Typ is
887 * still NIL to determine what the return value is!
888 * ----------------
889 */
890static Oid
892{
893 if (Typ != NIL)
894 {
895 ListCell *lc;
896
897 foreach(lc, Typ)
898 {
899 struct typmap *app = lfirst(lc);
900
901 if (strncmp(NameStr(app->am_typ.typname), type, NAMEDATALEN) == 0)
902 {
903 Ap = app;
904 return app->am_oid;
905 }
906 }
907
908 /*
909 * The type wasn't known; reload the pg_type contents and check again
910 * to handle composite types, added since last populating the list.
911 */
912
914 Typ = NIL;
916
917 /*
918 * Calling gettype would result in infinite recursion for types
919 * missing in pg_type, so just repeat the lookup.
920 */
921 foreach(lc, Typ)
922 {
923 struct typmap *app = lfirst(lc);
924
925 if (strncmp(NameStr(app->am_typ.typname), type, NAMEDATALEN) == 0)
926 {
927 Ap = app;
928 return app->am_oid;
929 }
930 }
931 }
932 else
933 {
934 int i;
935
936 for (i = 0; i < n_types; i++)
937 {
938 if (strncmp(type, TypInfo[i].name, NAMEDATALEN) == 0)
939 return i;
940 }
941 /* Not in TypInfo, so we'd better be able to read pg_type now */
942 elog(DEBUG4, "external type: %s", type);
944 return gettype(type);
945 }
946 elog(ERROR, "unrecognized type \"%s\"", type);
947 /* not reached, here to make compiler happy */
948 return 0;
949}
950
951/* ----------------
952 * boot_get_type_io_data
953 *
954 * Obtain type I/O information at bootstrap time. This intentionally has
955 * an API very close to that of lsyscache.c's get_type_io_data, except that
956 * we only support obtaining the typinput and typoutput routines, not
957 * the binary I/O routines, and we also return the type's collation.
958 * This is exported so that array_in and array_out can be made to work
959 * during early bootstrap.
960 * ----------------
961 */
962void
964 int16 *typlen,
965 bool *typbyval,
966 char *typalign,
967 char *typdelim,
968 Oid *typioparam,
969 Oid *typinput,
970 Oid *typoutput,
971 Oid *typcollation)
972{
973 if (Typ != NIL)
974 {
975 /* We have the boot-time contents of pg_type, so use it */
976 struct typmap *ap = NULL;
977 ListCell *lc;
978
979 foreach(lc, Typ)
980 {
981 ap = lfirst(lc);
982 if (ap->am_oid == typid)
983 break;
984 }
985
986 if (!ap || ap->am_oid != typid)
987 elog(ERROR, "type OID %u not found in Typ list", typid);
988
989 *typlen = ap->am_typ.typlen;
990 *typbyval = ap->am_typ.typbyval;
991 *typalign = ap->am_typ.typalign;
992 *typdelim = ap->am_typ.typdelim;
993
994 /* XXX this logic must match getTypeIOParam() */
995 if (OidIsValid(ap->am_typ.typelem))
996 *typioparam = ap->am_typ.typelem;
997 else
998 *typioparam = typid;
999
1000 *typinput = ap->am_typ.typinput;
1001 *typoutput = ap->am_typ.typoutput;
1002
1003 *typcollation = ap->am_typ.typcollation;
1004 }
1005 else
1006 {
1007 /* We don't have pg_type yet, so use the hard-wired TypInfo array */
1008 int typeindex;
1009
1010 for (typeindex = 0; typeindex < n_types; typeindex++)
1011 {
1012 if (TypInfo[typeindex].oid == typid)
1013 break;
1014 }
1015 if (typeindex >= n_types)
1016 elog(ERROR, "type OID %u not found in TypInfo", typid);
1017
1018 *typlen = TypInfo[typeindex].len;
1019 *typbyval = TypInfo[typeindex].byval;
1021 /* We assume typdelim is ',' for all boot-time types */
1022 *typdelim = ',';
1023
1024 /* XXX this logic must match getTypeIOParam() */
1025 if (OidIsValid(TypInfo[typeindex].elem))
1026 *typioparam = TypInfo[typeindex].elem;
1027 else
1028 *typioparam = typid;
1029
1031 *typoutput = TypInfo[typeindex].outproc;
1032
1033 *typcollation = TypInfo[typeindex].collation;
1034 }
1035}
1036
1037/* ----------------
1038 * AllocateAttribute
1039 *
1040 * Note: bootstrap never sets any per-column ACLs, so we only need
1041 * ATTRIBUTE_FIXED_PART_SIZE space per attribute.
1042 * ----------------
1043 */
1044static Form_pg_attribute
1050
1051/*
1052 * index_register() -- record an index that has been set up for building
1053 * later.
1054 *
1055 * At bootstrap time, we define a bunch of indexes on system catalogs.
1056 * We postpone actually building the indexes until just before we're
1057 * finished with initialization, however. This is because the indexes
1058 * themselves have catalog entries, and those have to be included in the
1059 * indexes on those catalogs. Doing it in two phases is the simplest
1060 * way of making sure the indexes have the right contents at the end.
1061 */
1062void
1064 Oid ind,
1065 const IndexInfo *indexInfo)
1066{
1069
1070 /*
1071 * XXX mao 10/31/92 -- don't gc index reldescs, associated info at
1072 * bootstrap time. we'll declare the indexes now, but want to create them
1073 * later.
1074 */
1075
1076 if (nogc == NULL)
1078 "BootstrapNoGC",
1080
1082
1084 newind->il_heap = heap;
1085 newind->il_ind = ind;
1086 newind->il_info = palloc_object(IndexInfo);
1087
1088 memcpy(newind->il_info, indexInfo, sizeof(IndexInfo));
1089 /* expressions will likely be null, but may as well copy it */
1090 newind->il_info->ii_Expressions =
1091 copyObject(indexInfo->ii_Expressions);
1092 newind->il_info->ii_ExpressionsState = NIL;
1093 /* predicate will likely be null, but may as well copy it */
1094 newind->il_info->ii_Predicate =
1095 copyObject(indexInfo->ii_Predicate);
1096 newind->il_info->ii_PredicateState = NULL;
1097 /* no exclusion constraints at bootstrap time, so no need to copy */
1098 Assert(indexInfo->ii_ExclusionOps == NULL);
1099 Assert(indexInfo->ii_ExclusionProcs == NULL);
1100 Assert(indexInfo->ii_ExclusionStrats == NULL);
1101
1102 newind->il_next = ILHead;
1103 ILHead = newind;
1104
1106}
1107
1108
1109/*
1110 * build_indices -- fill in all the indexes registered earlier
1111 */
1112void
1114{
1115 for (; ILHead != NULL; ILHead = ILHead->il_next)
1116 {
1117 Relation heap;
1118 Relation ind;
1119
1120 /* need not bother with locks during bootstrap */
1121 heap = table_open(ILHead->il_heap, NoLock);
1123
1124 index_build(heap, ind, ILHead->il_info, false, false);
1125
1127 table_close(heap, NoLock);
1128 }
1129}
#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:891
static Datum values[MAXATTR]
Definition bootstrap.c:147
void closerel(char *relname)
Definition bootstrap.c:481
static void populate_typ_list(void)
Definition bootstrap.c:851
static void CheckerModeMain(void)
Definition bootstrap.c:176
void build_indices(void)
Definition bootstrap.c:1113
void InsertOneValue(char *value, int i)
Definition bootstrap.c:653
static void cleanup(void)
Definition bootstrap.c:838
static const int n_types
Definition bootstrap.c:136
void InsertOneNull(int i)
Definition bootstrap.c:820
static const struct typinfo TypInfo[]
Definition bootstrap.c:89
Relation boot_reldesc
Definition bootstrap.c:60
static struct typmap * Ap
Definition bootstrap.c:145
static List * Typ
Definition bootstrap.c:144
void InsertOneTuple(void)
Definition bootstrap.c:625
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:963
static void InsertOneProargdefaultsValue(char *value)
Definition bootstrap.c:720
struct _IndexList IndexList
static void bootstrap_signals(void)
Definition bootstrap.c:409
Form_pg_attribute attrtypes[MAXATTR]
Definition bootstrap.c:62
int numattr
Definition bootstrap.c:63
static Form_pg_attribute AllocateAttribute(void)
Definition bootstrap.c:1045
static MemoryContext nogc
Definition bootstrap.c:150
void BootstrapModeMain(int argc, char *argv[], bool check_only)
Definition bootstrap.c:194
void DefineAttr(char *name, char *type, int attnum, int nullness)
Definition bootstrap.c:518
static bool Nulls[MAXATTR]
Definition bootstrap.c:148
void index_register(Oid heap, Oid ind, const IndexInfo *indexInfo)
Definition bootstrap.c:1063
void boot_openrel(char *relname)
Definition bootstrap.c:436
static IndexList * ILHead
Definition bootstrap.c:166
#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:777
#define Assert(condition)
Definition c.h:885
int16_t int16
Definition c.h:553
uint32_t uint32
Definition c.h:558
#define pg_fallthrough
Definition c.h:144
#define MemSet(start, val, len)
Definition c.h:1035
#define StaticAssertDecl(condition, errmessage)
Definition c.h:950
#define OidIsValid(objectId)
Definition c.h:800
void * yyscan_t
Definition cubedata.h:65
int errcode(int sqlerrcode)
Definition elog.c:874
int errmsg(const char *fmt,...)
Definition elog.c:1093
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:1044
#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:2785
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: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
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: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
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 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:625
@ ForwardScanDirection
Definition sdir.h:28
void InitProcess(void)
Definition proc.c:379
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:163
IndexInfo * il_info
Definition bootstrap.c:162
Definition c.h:757
char align
Definition bootstrap.c:82
Oid outproc
Definition bootstrap.c:86
int16 len
Definition bootstrap.c:80
Oid oid
Definition bootstrap.c:78
bool byval
Definition bootstrap.c:81
char name[NAMEDATALEN]
Definition bootstrap.c:77
Oid collation
Definition bootstrap.c:84
Oid elem
Definition bootstrap.c:79
char storage
Definition bootstrap.c:83
Oid inproc
Definition bootstrap.c:85
Oid am_oid
Definition bootstrap.c:140
FormData_pg_type am_typ
Definition bootstrap.c:141
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:3080
void CommitTransactionCommand(void)
Definition xact.c:3178
void BootStrapXLOG(uint32 data_checksum_version)
Definition xlog.c:5126