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