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