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-2018, 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/htup_details.h"
21 #include "access/xact.h"
22 #include "access/xlog_internal.h"
23 #include "bootstrap/bootstrap.h"
24 #include "catalog/index.h"
25 #include "catalog/pg_collation.h"
26 #include "catalog/pg_type.h"
27 #include "libpq/pqsignal.h"
28 #include "miscadmin.h"
29 #include "nodes/makefuncs.h"
30 #include "pg_getopt.h"
31 #include "pgstat.h"
32 #include "postmaster/bgwriter.h"
33 #include "postmaster/startup.h"
34 #include "postmaster/walwriter.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/ps_status.h"
46 #include "utils/rel.h"
47 #include "utils/relmapper.h"
48 #include "utils/tqual.h"
49 
51 
52 
53 #define ALLOC(t, c) \
54  ((t *) MemoryContextAllocZero(TopMemoryContext, (unsigned)(c) * sizeof(t)))
55 
56 static void CheckerModeMain(void);
57 static void BootstrapModeMain(void);
58 static void bootstrap_signals(void);
59 static void ShutdownAuxiliaryProcess(int code, Datum arg);
61 static Oid gettype(char *type);
62 static void cleanup(void);
63 
64 /* ----------------
65  * global variables
66  * ----------------
67  */
68 
69 AuxProcType MyAuxProcType = NotAnAuxProcess; /* declared in miscadmin.h */
70 
71 Relation boot_reldesc; /* current relation descriptor */
72 
73 Form_pg_attribute attrtypes[MAXATTR]; /* points to attribute info */
74 int numattr; /* number of attributes for cur. rel */
75 
76 
77 /*
78  * Basic information associated with each type. This is used before
79  * pg_type is filled, so it has to cover the datatypes used as column types
80  * in the core "bootstrapped" catalogs.
81  *
82  * XXX several of these input/output functions do catalog scans
83  * (e.g., F_REGPROCIN scans pg_proc). this obviously creates some
84  * order dependencies in the catalog creation process.
85  */
86 struct typinfo
87 {
92  bool byval;
93  char align;
94  char storage;
98 };
99 
100 static const struct typinfo TypInfo[] = {
101  {"bool", BOOLOID, 0, 1, true, 'c', 'p', InvalidOid,
102  F_BOOLIN, F_BOOLOUT},
103  {"bytea", BYTEAOID, 0, -1, false, 'i', 'x', InvalidOid,
104  F_BYTEAIN, F_BYTEAOUT},
105  {"char", CHAROID, 0, 1, true, 'c', 'p', InvalidOid,
106  F_CHARIN, F_CHAROUT},
107  {"int2", INT2OID, 0, 2, true, 's', 'p', InvalidOid,
108  F_INT2IN, F_INT2OUT},
109  {"int4", INT4OID, 0, 4, true, 'i', 'p', InvalidOid,
110  F_INT4IN, F_INT4OUT},
111  {"float4", FLOAT4OID, 0, 4, FLOAT4PASSBYVAL, 'i', 'p', InvalidOid,
112  F_FLOAT4IN, F_FLOAT4OUT},
113  {"name", NAMEOID, CHAROID, NAMEDATALEN, false, 'c', 'p', InvalidOid,
114  F_NAMEIN, F_NAMEOUT},
115  {"regclass", REGCLASSOID, 0, 4, true, 'i', 'p', InvalidOid,
116  F_REGCLASSIN, F_REGCLASSOUT},
117  {"regproc", REGPROCOID, 0, 4, true, 'i', 'p', InvalidOid,
118  F_REGPROCIN, F_REGPROCOUT},
119  {"regtype", REGTYPEOID, 0, 4, true, 'i', 'p', InvalidOid,
120  F_REGTYPEIN, F_REGTYPEOUT},
121  {"regrole", REGROLEOID, 0, 4, true, 'i', 'p', InvalidOid,
122  F_REGROLEIN, F_REGROLEOUT},
123  {"regnamespace", REGNAMESPACEOID, 0, 4, true, 'i', 'p', InvalidOid,
124  F_REGNAMESPACEIN, F_REGNAMESPACEOUT},
125  {"text", TEXTOID, 0, -1, false, 'i', 'x', DEFAULT_COLLATION_OID,
126  F_TEXTIN, F_TEXTOUT},
127  {"oid", OIDOID, 0, 4, true, 'i', 'p', InvalidOid,
128  F_OIDIN, F_OIDOUT},
129  {"tid", TIDOID, 0, 6, false, 's', 'p', InvalidOid,
130  F_TIDIN, F_TIDOUT},
131  {"xid", XIDOID, 0, 4, true, 'i', 'p', InvalidOid,
132  F_XIDIN, F_XIDOUT},
133  {"cid", CIDOID, 0, 4, true, 'i', 'p', InvalidOid,
134  F_CIDIN, F_CIDOUT},
135  {"pg_node_tree", PGNODETREEOID, 0, -1, false, 'i', 'x', DEFAULT_COLLATION_OID,
136  F_PG_NODE_TREE_IN, F_PG_NODE_TREE_OUT},
137  {"int2vector", INT2VECTOROID, INT2OID, -1, false, 'i', 'p', InvalidOid,
138  F_INT2VECTORIN, F_INT2VECTOROUT},
139  {"oidvector", OIDVECTOROID, OIDOID, -1, false, 'i', 'p', InvalidOid,
140  F_OIDVECTORIN, F_OIDVECTOROUT},
141  {"_int4", INT4ARRAYOID, INT4OID, -1, false, 'i', 'x', InvalidOid,
142  F_ARRAY_IN, F_ARRAY_OUT},
143  {"_text", 1009, TEXTOID, -1, false, 'i', 'x', DEFAULT_COLLATION_OID,
144  F_ARRAY_IN, F_ARRAY_OUT},
145  {"_oid", 1028, OIDOID, -1, false, 'i', 'x', InvalidOid,
146  F_ARRAY_IN, F_ARRAY_OUT},
147  {"_char", 1002, CHAROID, -1, false, 'i', 'x', InvalidOid,
148  F_ARRAY_IN, F_ARRAY_OUT},
149  {"_aclitem", 1034, ACLITEMOID, -1, false, 'i', 'x', InvalidOid,
150  F_ARRAY_IN, F_ARRAY_OUT}
151 };
152 
153 static const int n_types = sizeof(TypInfo) / sizeof(struct typinfo);
154 
155 struct typmap
156 { /* a hack */
159 };
160 
161 static struct typmap **Typ = NULL;
162 static struct typmap *Ap = NULL;
163 
164 static Datum values[MAXATTR]; /* current row's attribute values */
165 static bool Nulls[MAXATTR];
166 
167 static MemoryContext nogc = NULL; /* special no-gc mem context */
168 
169 /*
170  * At bootstrap time, we first declare all the indices to be built, and
171  * then build them. The IndexList structure stores enough information
172  * to allow us to build the indices after they've been declared.
173  */
174 
175 typedef struct _IndexList
176 {
181 } IndexList;
182 
183 static IndexList *ILHead = NULL;
184 
185 
186 /*
187  * AuxiliaryProcessMain
188  *
189  * The main entry point for auxiliary processes, such as the bgwriter,
190  * walwriter, walreceiver, bootstrapper and the shared memory checker code.
191  *
192  * This code is here just because of historical reasons.
193  */
194 void
195 AuxiliaryProcessMain(int argc, char *argv[])
196 {
197  char *progname = argv[0];
198  int flag;
199  char *userDoption = NULL;
200 
201  /*
202  * Initialize process environment (already done if under postmaster, but
203  * not if standalone).
204  */
205  if (!IsUnderPostmaster)
206  InitStandaloneProcess(argv[0]);
207 
208  /*
209  * process command arguments
210  */
211 
212  /* Set defaults, to be overridden by explicit options below */
213  if (!IsUnderPostmaster)
215 
216  /* Ignore the initial --boot argument, if present */
217  if (argc > 1 && strcmp(argv[1], "--boot") == 0)
218  {
219  argv++;
220  argc--;
221  }
222 
223  /* If no -x argument, we are a CheckerProcess */
225 
226  while ((flag = getopt(argc, argv, "B:c:d:D:Fkr:x:X:-:")) != -1)
227  {
228  switch (flag)
229  {
230  case 'B':
231  SetConfigOption("shared_buffers", optarg, PGC_POSTMASTER, PGC_S_ARGV);
232  break;
233  case 'D':
234  userDoption = pstrdup(optarg);
235  break;
236  case 'd':
237  {
238  /* Turn on debugging for the bootstrap process. */
239  char *debugstr;
240 
241  debugstr = psprintf("debug%s", optarg);
242  SetConfigOption("log_min_messages", debugstr,
244  SetConfigOption("client_min_messages", debugstr,
246  pfree(debugstr);
247  }
248  break;
249  case 'F':
250  SetConfigOption("fsync", "false", PGC_POSTMASTER, PGC_S_ARGV);
251  break;
252  case 'k':
254  break;
255  case 'r':
257  break;
258  case 'x':
259  MyAuxProcType = atoi(optarg);
260  break;
261  case 'X':
262  {
263  int WalSegSz = strtoul(optarg, NULL, 0);
264 
265  if (!IsValidWalSegSize(WalSegSz))
266  ereport(ERROR,
267  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
268  errmsg("-X requires a power of 2 value between 1MB and 1GB")));
269  SetConfigOption("wal_segment_size", optarg, PGC_INTERNAL,
271  }
272  break;
273  case 'c':
274  case '-':
275  {
276  char *name,
277  *value;
278 
279  ParseLongOption(optarg, &name, &value);
280  if (!value)
281  {
282  if (flag == '-')
283  ereport(ERROR,
284  (errcode(ERRCODE_SYNTAX_ERROR),
285  errmsg("--%s requires a value",
286  optarg)));
287  else
288  ereport(ERROR,
289  (errcode(ERRCODE_SYNTAX_ERROR),
290  errmsg("-c %s requires a value",
291  optarg)));
292  }
293 
295  free(name);
296  if (value)
297  free(value);
298  break;
299  }
300  default:
301  write_stderr("Try \"%s --help\" for more information.\n",
302  progname);
303  proc_exit(1);
304  break;
305  }
306  }
307 
308  if (argc != optind)
309  {
310  write_stderr("%s: invalid command-line arguments\n", progname);
311  proc_exit(1);
312  }
313 
314  /*
315  * Identify myself via ps
316  */
317  if (IsUnderPostmaster)
318  {
319  const char *statmsg;
320 
321  switch (MyAuxProcType)
322  {
323  case StartupProcess:
325  break;
326  case BgWriterProcess:
328  break;
329  case CheckpointerProcess:
331  break;
332  case WalWriterProcess:
334  break;
335  case WalReceiverProcess:
337  break;
338  default:
339  statmsg = "??? process";
340  break;
341  }
342  init_ps_display(statmsg, "", "", "");
343  }
344 
345  /* Acquire configuration parameters, unless inherited from postmaster */
346  if (!IsUnderPostmaster)
347  {
348  if (!SelectConfigFiles(userDoption, progname))
349  proc_exit(1);
350  }
351 
352  /*
353  * Validate we have been given a reasonable-looking DataDir and change
354  * into it (if under postmaster, should be done already).
355  */
356  if (!IsUnderPostmaster)
357  {
358  checkDataDir();
359  ChangeToDataDir();
360  }
361 
362  /* If standalone, create lockfile for data directory */
363  if (!IsUnderPostmaster)
364  CreateDataDirLockFile(false);
365 
367  IgnoreSystemIndexes = true;
368 
369  /* Initialize MaxBackends (if under postmaster, was done already) */
370  if (!IsUnderPostmaster)
372 
373  BaseInit();
374 
375  /*
376  * When we are an auxiliary process, we aren't going to do the full
377  * InitPostgres pushups, but there are a couple of things that need to get
378  * lit up even in an auxiliary process.
379  */
380  if (IsUnderPostmaster)
381  {
382  /*
383  * Create a PGPROC so we can use LWLocks. In the EXEC_BACKEND case,
384  * this was already done by SubPostmasterMain().
385  */
386 #ifndef EXEC_BACKEND
388 #endif
389 
390  /*
391  * Assign the ProcSignalSlot for an auxiliary process. Since it
392  * doesn't have a BackendId, the slot is statically allocated based on
393  * the auxiliary process type (MyAuxProcType). Backends use slots
394  * indexed in the range from 1 to MaxBackends (inclusive), so we use
395  * MaxBackends + AuxProcType + 1 as the index of the slot for an
396  * auxiliary process.
397  *
398  * This will need rethinking if we ever want more than one of a
399  * particular auxiliary process type.
400  */
402 
403  /* finish setting up bufmgr.c */
405 
406  /* Initialize backend status information */
408  pgstat_bestart();
409 
410  /* register a before-shutdown callback for LWLock cleanup */
412  }
413 
414  /*
415  * XLOG operations
416  */
418 
419  switch (MyAuxProcType)
420  {
421  case CheckerProcess:
422  /* don't set signals, they're useless here */
423  CheckerModeMain();
424  proc_exit(1); /* should never return */
425 
426  case BootstrapProcess:
427 
428  /*
429  * There was a brief instant during which mode was Normal; this is
430  * okay. We need to be in bootstrap mode during BootStrapXLOG for
431  * the sake of multixact initialization.
432  */
435  BootStrapXLOG();
437  proc_exit(1); /* should never return */
438 
439  case StartupProcess:
440  /* don't set signals, startup process has its own agenda */
442  proc_exit(1); /* should never return */
443 
444  case BgWriterProcess:
445  /* don't set signals, bgwriter has its own agenda */
447  proc_exit(1); /* should never return */
448 
449  case CheckpointerProcess:
450  /* don't set signals, checkpointer has its own agenda */
452  proc_exit(1); /* should never return */
453 
454  case WalWriterProcess:
455  /* don't set signals, walwriter has its own agenda */
456  InitXLOGAccess();
457  WalWriterMain();
458  proc_exit(1); /* should never return */
459 
460  case WalReceiverProcess:
461  /* don't set signals, walreceiver has its own agenda */
462  WalReceiverMain();
463  proc_exit(1); /* should never return */
464 
465  default:
466  elog(PANIC, "unrecognized process type: %d", (int) MyAuxProcType);
467  proc_exit(1);
468  }
469 }
470 
471 /*
472  * In shared memory checker mode, all we really want to do is create shared
473  * memory and semaphores (just to prove we can do it with the current GUC
474  * settings). Since, in fact, that was already done by BaseInit(),
475  * we have nothing more to do here.
476  */
477 static void
479 {
480  proc_exit(0);
481 }
482 
483 /*
484  * The main entry point for running the backend in bootstrap mode
485  *
486  * The bootstrap mode is used to initialize the template database.
487  * The bootstrap backend doesn't speak SQL, but instead expects
488  * commands in a special bootstrap language.
489  */
490 static void
492 {
493  int i;
494 
497 
498  /*
499  * Do backend-like initialization for bootstrap mode
500  */
501  InitProcess();
502 
503  InitPostgres(NULL, InvalidOid, NULL, InvalidOid, NULL, false);
504 
505  /* Initialize stuff for bootstrap-file processing */
506  for (i = 0; i < MAXATTR; i++)
507  {
508  attrtypes[i] = NULL;
509  Nulls[i] = false;
510  }
511 
512  /*
513  * Process bootstrap input.
514  */
516  boot_yyparse();
518 
519  /*
520  * We should now know about all mapped relations, so it's okay to write
521  * out the initial relation mapping files.
522  */
524 
525  /* Clean up and exit */
526  cleanup();
527  proc_exit(0);
528 }
529 
530 
531 /* ----------------------------------------------------------------
532  * misc functions
533  * ----------------------------------------------------------------
534  */
535 
536 /*
537  * Set up signal handling for a bootstrap process
538  */
539 static void
541 {
543 
544  /* Set up appropriately for interactive use */
545  pqsignal(SIGHUP, die);
546  pqsignal(SIGINT, die);
547  pqsignal(SIGTERM, die);
548  pqsignal(SIGQUIT, die);
549 }
550 
551 /*
552  * Begin shutdown of an auxiliary process. This is approximately the equivalent
553  * of ShutdownPostgres() in postinit.c. We can't run transactions in an
554  * auxiliary process, so most of the work of AbortTransaction() is not needed,
555  * but we do need to make sure we've released any LWLocks we are holding.
556  * (This is only critical during an error exit.)
557  */
558 static void
560 {
564 }
565 
566 /* ----------------------------------------------------------------
567  * MANUAL BACKEND INTERACTIVE INTERFACE COMMANDS
568  * ----------------------------------------------------------------
569  */
570 
571 /* ----------------
572  * boot_openrel
573  * ----------------
574  */
575 void
576 boot_openrel(char *relname)
577 {
578  int i;
579  struct typmap **app;
580  Relation rel;
581  HeapScanDesc scan;
582  HeapTuple tup;
583 
584  if (strlen(relname) >= NAMEDATALEN)
585  relname[NAMEDATALEN - 1] = '\0';
586 
587  if (Typ == NULL)
588  {
589  /* We can now load the pg_type data */
590  rel = heap_open(TypeRelationId, NoLock);
591  scan = heap_beginscan_catalog(rel, 0, NULL);
592  i = 0;
593  while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
594  ++i;
595  heap_endscan(scan);
596  app = Typ = ALLOC(struct typmap *, i + 1);
597  while (i-- > 0)
598  *app++ = ALLOC(struct typmap, 1);
599  *app = NULL;
600  scan = heap_beginscan_catalog(rel, 0, NULL);
601  app = Typ;
602  while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
603  {
604  (*app)->am_oid = HeapTupleGetOid(tup);
605  memcpy((char *) &(*app)->am_typ,
606  (char *) GETSTRUCT(tup),
607  sizeof((*app)->am_typ));
608  app++;
609  }
610  heap_endscan(scan);
611  heap_close(rel, NoLock);
612  }
613 
614  if (boot_reldesc != NULL)
615  closerel(NULL);
616 
617  elog(DEBUG4, "open relation %s, attrsize %d",
618  relname, (int) ATTRIBUTE_FIXED_PART_SIZE);
619 
620  boot_reldesc = heap_openrv(makeRangeVar(NULL, relname, -1), NoLock);
621  numattr = RelationGetNumberOfAttributes(boot_reldesc);
622  for (i = 0; i < numattr; i++)
623  {
624  if (attrtypes[i] == NULL)
626  memmove((char *) attrtypes[i],
627  (char *) TupleDescAttr(boot_reldesc->rd_att, i),
628  ATTRIBUTE_FIXED_PART_SIZE);
629 
630  {
632 
633  elog(DEBUG4, "create attribute %d name %s len %d num %d type %u",
634  i, NameStr(at->attname), at->attlen, at->attnum,
635  at->atttypid);
636  }
637  }
638 }
639 
640 /* ----------------
641  * closerel
642  * ----------------
643  */
644 void
646 {
647  if (name)
648  {
649  if (boot_reldesc)
650  {
651  if (strcmp(RelationGetRelationName(boot_reldesc), name) != 0)
652  elog(ERROR, "close of %s when %s was expected",
653  name, RelationGetRelationName(boot_reldesc));
654  }
655  else
656  elog(ERROR, "close of %s before any relation was opened",
657  name);
658  }
659 
660  if (boot_reldesc == NULL)
661  elog(ERROR, "no open relation to close");
662  else
663  {
664  elog(DEBUG4, "close relation %s",
665  RelationGetRelationName(boot_reldesc));
666  heap_close(boot_reldesc, NoLock);
667  boot_reldesc = NULL;
668  }
669 }
670 
671 
672 
673 /* ----------------
674  * DEFINEATTR()
675  *
676  * define a <field,type> pair
677  * if there are n fields in a relation to be created, this routine
678  * will be called n times
679  * ----------------
680  */
681 void
682 DefineAttr(char *name, char *type, int attnum, int nullness)
683 {
684  Oid typeoid;
685 
686  if (boot_reldesc != NULL)
687  {
688  elog(WARNING, "no open relations allowed with CREATE command");
689  closerel(NULL);
690  }
691 
692  if (attrtypes[attnum] == NULL)
695 
696  namestrcpy(&attrtypes[attnum]->attname, name);
697  elog(DEBUG4, "column %s %s", NameStr(attrtypes[attnum]->attname), type);
698  attrtypes[attnum]->attnum = attnum + 1; /* fillatt */
699 
700  typeoid = gettype(type);
701 
702  if (Typ != NULL)
703  {
704  attrtypes[attnum]->atttypid = Ap->am_oid;
705  attrtypes[attnum]->attlen = Ap->am_typ.typlen;
706  attrtypes[attnum]->attbyval = Ap->am_typ.typbyval;
707  attrtypes[attnum]->attstorage = Ap->am_typ.typstorage;
708  attrtypes[attnum]->attalign = Ap->am_typ.typalign;
709  attrtypes[attnum]->attcollation = Ap->am_typ.typcollation;
710  /* if an array type, assume 1-dimensional attribute */
711  if (Ap->am_typ.typelem != InvalidOid && Ap->am_typ.typlen < 0)
712  attrtypes[attnum]->attndims = 1;
713  else
714  attrtypes[attnum]->attndims = 0;
715  }
716  else
717  {
718  attrtypes[attnum]->atttypid = TypInfo[typeoid].oid;
719  attrtypes[attnum]->attlen = TypInfo[typeoid].len;
720  attrtypes[attnum]->attbyval = TypInfo[typeoid].byval;
721  attrtypes[attnum]->attstorage = TypInfo[typeoid].storage;
722  attrtypes[attnum]->attalign = TypInfo[typeoid].align;
723  attrtypes[attnum]->attcollation = TypInfo[typeoid].collation;
724  /* if an array type, assume 1-dimensional attribute */
725  if (TypInfo[typeoid].elem != InvalidOid &&
726  attrtypes[attnum]->attlen < 0)
727  attrtypes[attnum]->attndims = 1;
728  else
729  attrtypes[attnum]->attndims = 0;
730  }
731 
732  attrtypes[attnum]->attstattarget = -1;
733  attrtypes[attnum]->attcacheoff = -1;
734  attrtypes[attnum]->atttypmod = -1;
735  attrtypes[attnum]->attislocal = true;
736 
737  if (nullness == BOOTCOL_NULL_FORCE_NOT_NULL)
738  {
739  attrtypes[attnum]->attnotnull = true;
740  }
741  else if (nullness == BOOTCOL_NULL_FORCE_NULL)
742  {
743  attrtypes[attnum]->attnotnull = false;
744  }
745  else
746  {
747  Assert(nullness == BOOTCOL_NULL_AUTO);
748 
749  /*
750  * Mark as "not null" if type is fixed-width and prior columns are
751  * too. This corresponds to case where column can be accessed
752  * directly via C struct declaration.
753  *
754  * oidvector and int2vector are also treated as not-nullable, even
755  * though they are no longer fixed-width.
756  */
757 #define MARKNOTNULL(att) \
758  ((att)->attlen > 0 || \
759  (att)->atttypid == OIDVECTOROID || \
760  (att)->atttypid == INT2VECTOROID)
761 
762  if (MARKNOTNULL(attrtypes[attnum]))
763  {
764  int i;
765 
766  /* check earlier attributes */
767  for (i = 0; i < attnum; i++)
768  {
769  if (!attrtypes[i]->attnotnull)
770  break;
771  }
772  if (i == attnum)
773  attrtypes[attnum]->attnotnull = true;
774  }
775  }
776 }
777 
778 
779 /* ----------------
780  * InsertOneTuple
781  *
782  * If objectid is not zero, it is a specific OID to assign to the tuple.
783  * Otherwise, an OID will be assigned (if necessary) by heap_insert.
784  * ----------------
785  */
786 void
788 {
789  HeapTuple tuple;
790  TupleDesc tupDesc;
791  int i;
792 
793  elog(DEBUG4, "inserting row oid %u, %d columns", objectid, numattr);
794 
795  tupDesc = CreateTupleDesc(numattr,
796  RelationGetForm(boot_reldesc)->relhasoids,
797  attrtypes);
798  tuple = heap_form_tuple(tupDesc, values, Nulls);
799  if (objectid != (Oid) 0)
800  HeapTupleSetOid(tuple, objectid);
801  pfree(tupDesc); /* just free's tupDesc, not the attrtypes */
802 
803  simple_heap_insert(boot_reldesc, tuple);
804  heap_freetuple(tuple);
805  elog(DEBUG4, "row inserted");
806 
807  /*
808  * Reset null markers for next tuple
809  */
810  for (i = 0; i < numattr; i++)
811  Nulls[i] = false;
812 }
813 
814 /* ----------------
815  * InsertOneValue
816  * ----------------
817  */
818 void
819 InsertOneValue(char *value, int i)
820 {
821  Oid typoid;
822  int16 typlen;
823  bool typbyval;
824  char typalign;
825  char typdelim;
826  Oid typioparam;
827  Oid typinput;
828  Oid typoutput;
829 
830  AssertArg(i >= 0 && i < MAXATTR);
831 
832  elog(DEBUG4, "inserting column %d value \"%s\"", i, value);
833 
834  typoid = TupleDescAttr(boot_reldesc->rd_att, i)->atttypid;
835 
836  boot_get_type_io_data(typoid,
837  &typlen, &typbyval, &typalign,
838  &typdelim, &typioparam,
839  &typinput, &typoutput);
840 
841  values[i] = OidInputFunctionCall(typinput, value, typioparam, -1);
842 
843  /*
844  * We use ereport not elog here so that parameters aren't evaluated unless
845  * the message is going to be printed, which generally it isn't
846  */
847  ereport(DEBUG4,
848  (errmsg_internal("inserted -> %s",
849  OidOutputFunctionCall(typoutput, values[i]))));
850 }
851 
852 /* ----------------
853  * InsertOneNull
854  * ----------------
855  */
856 void
858 {
859  elog(DEBUG4, "inserting column %d NULL", i);
860  Assert(i >= 0 && i < MAXATTR);
861  if (TupleDescAttr(boot_reldesc->rd_att, i)->attnotnull)
862  elog(ERROR,
863  "NULL value specified for not-null column \"%s\" of relation \"%s\"",
864  NameStr(TupleDescAttr(boot_reldesc->rd_att, i)->attname),
865  RelationGetRelationName(boot_reldesc));
866  values[i] = PointerGetDatum(NULL);
867  Nulls[i] = true;
868 }
869 
870 /* ----------------
871  * cleanup
872  * ----------------
873  */
874 static void
875 cleanup(void)
876 {
877  if (boot_reldesc != NULL)
878  closerel(NULL);
879 }
880 
881 /* ----------------
882  * gettype
883  *
884  * NB: this is really ugly; it will return an integer index into TypInfo[],
885  * and not an OID at all, until the first reference to a type not known in
886  * TypInfo[]. At that point it will read and cache pg_type in the Typ array,
887  * and subsequently return a real OID (and set the global pointer Ap to
888  * point at the found row in Typ). So caller must check whether Typ is
889  * still NULL to determine what the return value is!
890  * ----------------
891  */
892 static Oid
893 gettype(char *type)
894 {
895  int i;
896  Relation rel;
897  HeapScanDesc scan;
898  HeapTuple tup;
899  struct typmap **app;
900 
901  if (Typ != NULL)
902  {
903  for (app = Typ; *app != NULL; app++)
904  {
905  if (strncmp(NameStr((*app)->am_typ.typname), type, NAMEDATALEN) == 0)
906  {
907  Ap = *app;
908  return (*app)->am_oid;
909  }
910  }
911  }
912  else
913  {
914  for (i = 0; i < n_types; i++)
915  {
916  if (strncmp(type, TypInfo[i].name, NAMEDATALEN) == 0)
917  return i;
918  }
919  elog(DEBUG4, "external type: %s", type);
920  rel = heap_open(TypeRelationId, NoLock);
921  scan = heap_beginscan_catalog(rel, 0, NULL);
922  i = 0;
923  while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
924  ++i;
925  heap_endscan(scan);
926  app = Typ = ALLOC(struct typmap *, i + 1);
927  while (i-- > 0)
928  *app++ = ALLOC(struct typmap, 1);
929  *app = NULL;
930  scan = heap_beginscan_catalog(rel, 0, NULL);
931  app = Typ;
932  while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
933  {
934  (*app)->am_oid = HeapTupleGetOid(tup);
935  memmove((char *) &(*app++)->am_typ,
936  (char *) GETSTRUCT(tup),
937  sizeof((*app)->am_typ));
938  }
939  heap_endscan(scan);
940  heap_close(rel, NoLock);
941  return gettype(type);
942  }
943  elog(ERROR, "unrecognized type \"%s\"", type);
944  /* not reached, here to make compiler happy */
945  return 0;
946 }
947 
948 /* ----------------
949  * boot_get_type_io_data
950  *
951  * Obtain type I/O information at bootstrap time. This intentionally has
952  * almost the same API as lsyscache.c's get_type_io_data, except that
953  * we only support obtaining the typinput and typoutput routines, not
954  * the binary I/O routines. It is exported so that array_in and array_out
955  * can be made to work during early bootstrap.
956  * ----------------
957  */
958 void
960  int16 *typlen,
961  bool *typbyval,
962  char *typalign,
963  char *typdelim,
964  Oid *typioparam,
965  Oid *typinput,
966  Oid *typoutput)
967 {
968  if (Typ != NULL)
969  {
970  /* We have the boot-time contents of pg_type, so use it */
971  struct typmap **app;
972  struct typmap *ap;
973 
974  app = Typ;
975  while (*app && (*app)->am_oid != typid)
976  ++app;
977  ap = *app;
978  if (ap == NULL)
979  elog(ERROR, "type OID %u not found in Typ list", typid);
980 
981  *typlen = ap->am_typ.typlen;
982  *typbyval = ap->am_typ.typbyval;
983  *typalign = ap->am_typ.typalign;
984  *typdelim = ap->am_typ.typdelim;
985 
986  /* XXX this logic must match getTypeIOParam() */
987  if (OidIsValid(ap->am_typ.typelem))
988  *typioparam = ap->am_typ.typelem;
989  else
990  *typioparam = typid;
991 
992  *typinput = ap->am_typ.typinput;
993  *typoutput = ap->am_typ.typoutput;
994  }
995  else
996  {
997  /* We don't have pg_type yet, so use the hard-wired TypInfo array */
998  int typeindex;
999 
1000  for (typeindex = 0; typeindex < n_types; typeindex++)
1001  {
1002  if (TypInfo[typeindex].oid == typid)
1003  break;
1004  }
1005  if (typeindex >= n_types)
1006  elog(ERROR, "type OID %u not found in TypInfo", typid);
1007 
1008  *typlen = TypInfo[typeindex].len;
1009  *typbyval = TypInfo[typeindex].byval;
1010  *typalign = TypInfo[typeindex].align;
1011  /* We assume typdelim is ',' for all boot-time types */
1012  *typdelim = ',';
1013 
1014  /* XXX this logic must match getTypeIOParam() */
1015  if (OidIsValid(TypInfo[typeindex].elem))
1016  *typioparam = TypInfo[typeindex].elem;
1017  else
1018  *typioparam = typid;
1019 
1020  *typinput = TypInfo[typeindex].inproc;
1021  *typoutput = TypInfo[typeindex].outproc;
1022  }
1023 }
1024 
1025 /* ----------------
1026  * AllocateAttribute
1027  *
1028  * Note: bootstrap never sets any per-column ACLs, so we only need
1029  * ATTRIBUTE_FIXED_PART_SIZE space per attribute.
1030  * ----------------
1031  */
1032 static Form_pg_attribute
1034 {
1035  return (Form_pg_attribute)
1037 }
1038 
1039 /*
1040  * index_register() -- record an index that has been set up for building
1041  * later.
1042  *
1043  * At bootstrap time, we define a bunch of indexes on system catalogs.
1044  * We postpone actually building the indexes until just before we're
1045  * finished with initialization, however. This is because the indexes
1046  * themselves have catalog entries, and those have to be included in the
1047  * indexes on those catalogs. Doing it in two phases is the simplest
1048  * way of making sure the indexes have the right contents at the end.
1049  */
1050 void
1052  Oid ind,
1053  IndexInfo *indexInfo)
1054 {
1055  IndexList *newind;
1056  MemoryContext oldcxt;
1057 
1058  /*
1059  * XXX mao 10/31/92 -- don't gc index reldescs, associated info at
1060  * bootstrap time. we'll declare the indexes now, but want to create them
1061  * later.
1062  */
1063 
1064  if (nogc == NULL)
1065  nogc = AllocSetContextCreate(NULL,
1066  "BootstrapNoGC",
1068 
1069  oldcxt = MemoryContextSwitchTo(nogc);
1070 
1071  newind = (IndexList *) palloc(sizeof(IndexList));
1072  newind->il_heap = heap;
1073  newind->il_ind = ind;
1074  newind->il_info = (IndexInfo *) palloc(sizeof(IndexInfo));
1075 
1076  memcpy(newind->il_info, indexInfo, sizeof(IndexInfo));
1077  /* expressions will likely be null, but may as well copy it */
1078  newind->il_info->ii_Expressions =
1079  copyObject(indexInfo->ii_Expressions);
1080  newind->il_info->ii_ExpressionsState = NIL;
1081  /* predicate will likely be null, but may as well copy it */
1082  newind->il_info->ii_Predicate =
1083  copyObject(indexInfo->ii_Predicate);
1084  newind->il_info->ii_PredicateState = NULL;
1085  /* no exclusion constraints at bootstrap time, so no need to copy */
1086  Assert(indexInfo->ii_ExclusionOps == NULL);
1087  Assert(indexInfo->ii_ExclusionProcs == NULL);
1088  Assert(indexInfo->ii_ExclusionStrats == NULL);
1089 
1090  newind->il_next = ILHead;
1091  ILHead = newind;
1092 
1093  MemoryContextSwitchTo(oldcxt);
1094 }
1095 
1096 
1097 /*
1098  * build_indices -- fill in all the indexes registered earlier
1099  */
1100 void
1102 {
1103  for (; ILHead != NULL; ILHead = ILHead->il_next)
1104  {
1105  Relation heap;
1106  Relation ind;
1107 
1108  /* need not bother with locks during bootstrap */
1109  heap = heap_open(ILHead->il_heap, NoLock);
1110  ind = index_open(ILHead->il_ind, NoLock);
1111 
1112  index_build(heap, ind, ILHead->il_info, false, false, false);
1113 
1114  index_close(ind, NoLock);
1115  heap_close(heap, NoLock);
1116  }
1117 }
AuxProcType
Definition: miscadmin.h:393
bool relhasoids
Definition: pg_class.h:60
void InitAuxiliaryProcess(void)
Definition: proc.c:491
struct _IndexList * il_next
Definition: bootstrap.c:180
signed short int16
Definition: c.h:312
#define IsValidWalSegSize(size)
Definition: xlog_internal.h:97
#define NIL
Definition: pg_list.h:69
void RelationMapFinishBootstrap(void)
Definition: relmapper.c:538
#define SIGQUIT
Definition: win32_port.h:164
#define BOOTCOL_NULL_FORCE_NULL
Definition: bootstrap.h:27
Oid il_ind
Definition: bootstrap.c:178
int16 len
Definition: bootstrap.c:91
#define GETSTRUCT(TUP)
Definition: htup_details.h:673
List * ii_Predicate
Definition: execnodes.h:156
void heap_endscan(HeapScanDesc scan)
Definition: heapam.c:1572
void WalWriterMain(void)
Definition: walwriter.c:98
int16 typlen
Definition: pg_type.h:55
bool attnotnull
Definition: pg_attribute.h:132
char align
Definition: bootstrap.c:93
int numattr
Definition: bootstrap.c:74
char name[NAMEDATALEN]
Definition: bootstrap.c:88
#define RelationGetNumberOfAttributes(relation)
Definition: rel.h:413
#define PointerGetDatum(X)
Definition: postgres.h:539
void InsertOneTuple(Oid objectid)
Definition: bootstrap.c:787
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:93
ExprState * ii_PredicateState
Definition: execnodes.h:157
#define RelationGetForm(relation)
Definition: rel.h:401
char * pstrdup(const char *in)
Definition: mcxt.c:1161
void CommitTransactionCommand(void)
Definition: xact.c:2744
const char * pgstat_get_backend_desc(BackendType backendType)
Definition: pgstat.c:4099
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
void InitStandaloneProcess(const char *argv0)
Definition: miscinit.c:315
Relation boot_reldesc
Definition: bootstrap.c:71
void ParseLongOption(const char *string, char **name, char **value)
Definition: guc.c:9633
#define BOOTCOL_NULL_AUTO
Definition: bootstrap.h:26
void StartupProcessMain(void)
Definition: startup.c:179
#define MARKNOTNULL(att)
int boot_yyparse(void)
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define write_stderr(str)
Definition: parallel.c:182
static IndexList * ILHead
Definition: bootstrap.c:183
Oid * ii_ExclusionProcs
Definition: execnodes.h:159
void proc_exit(int code)
Definition: ipc.c:104
int errcode(int sqlerrcode)
Definition: elog.c:575
#define MemSet(start, val, len)
Definition: c.h:908
#define SetProcessingMode(mode)
Definition: miscadmin.h:378
void BootStrapXLOG(void)
Definition: xlog.c:5016
void BaseInit(void)
Definition: postinit.c:524
bool IgnoreSystemIndexes
Definition: miscinit.c:75
static MemoryContext nogc
Definition: bootstrap.c:167
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1074
Oid inproc
Definition: bootstrap.c:96
#define heap_close(r, l)
Definition: heapam.h:97
void checkDataDir(void)
Definition: miscinit.c:97
TupleDesc CreateTupleDesc(int natts, bool hasoid, Form_pg_attribute *attrs)
Definition: tupdesc.c:91
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1773
unsigned int Oid
Definition: postgres_ext.h:31
void closerel(char *name)
Definition: bootstrap.c:645
void index_build(Relation heapRelation, Relation indexRelation, IndexInfo *indexInfo, bool isprimary, bool isreindex, bool parallel)
Definition: index.c:2229
static void bootstrap_signals(void)
Definition: bootstrap.c:540
static struct typmap * Ap
Definition: bootstrap.c:162
int namestrcpy(Name name, const char *str)
Definition: name.c:216
const char * progname
Definition: pg_standby.c:37
#define DEBUG4
Definition: elog.h:22
#define OidIsValid(objectId)
Definition: c.h:605
#define BOOTCOL_NULL_FORCE_NOT_NULL
Definition: bootstrap.h:28
#define PANIC
Definition: elog.h:53
static void BootstrapModeMain(void)
Definition: bootstrap.c:491
int getopt(int nargc, char *const *nargv, const char *ostr)
Definition: getopt.c:72
FormData_pg_type
Definition: pg_type.h:240
#define HeapTupleSetOid(tuple, oid)
Definition: htup_details.h:715
#define NAMEDATALEN
void WalReceiverMain(void)
Definition: walreceiver.c:189
List * ii_ExpressionsState
Definition: execnodes.h:155
void pfree(void *pointer)
Definition: mcxt.c:1031
char typalign
Definition: pg_type.h:167
int optind
Definition: getopt.c:51
void ConditionVariableCancelSleep(void)
void InsertOneValue(char *value, int i)
Definition: bootstrap.c:819
#define ERROR
Definition: elog.h:43
void CreateDataDirLockFile(bool amPostmaster)
Definition: miscinit.c:1173
NameData attname
Definition: pg_attribute.h:40
void InitXLOGAccess(void)
Definition: xlog.c:8186
Oid elem
Definition: bootstrap.c:90
#define MAXPGPATH
int MaxBackends
Definition: globals.c:136
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
void InitProcess(void)
Definition: proc.c:288
struct _IndexList IndexList
#define NoLock
Definition: lockdefs.h:34
char OutputFileName[MAXPGPATH]
Definition: globals.c:71
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition: guc.c:6903
#define memmove(d, s, c)
Definition: c.h:1100
bool IsUnderPostmaster
Definition: globals.c:110
IndexInfo * il_info
Definition: bootstrap.c:179
char * flag(int b)
Definition: test-ctype.c:33
#define SIGHUP
Definition: win32_port.h:163
void before_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:331
#define RelationGetRelationName(relation)
Definition: rel.h:441
void ChangeToDataDir(void)
Definition: miscinit.c:211
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:197
unsigned int uint32
Definition: c.h:325
void boot_openrel(char *relname)
Definition: bootstrap.c:576
void pgstat_initialize(void)
Definition: pgstat.c:2745
static void pgstat_report_wait_end(void)
Definition: pgstat.h:1260
HeapScanDesc heap_beginscan_catalog(Relation relation, int nkeys, ScanKey key)
Definition: heapam.c:1412
#define MAXATTR
Definition: bootstrap.h:24
#define ATTRIBUTE_FIXED_PART_SIZE
Definition: pg_attribute.h:189
void index_register(Oid heap, Oid ind, IndexInfo *indexInfo)
Definition: bootstrap.c:1051
#define ereport(elevel, rest)
Definition: elog.h:122
void DefineAttr(char *name, char *type, int attnum, int nullness)
Definition: bootstrap.c:682
#define AssertArg(condition)
Definition: c.h:701
MemoryContext TopMemoryContext
Definition: mcxt.c:44
#define AllocSetContextCreate(parent, name, allocparams)
Definition: memutils.h:170
int16 attlen
Definition: pg_attribute.h:64
#define WARNING
Definition: elog.h:40
Oid collation
Definition: bootstrap.c:95
static struct typmap ** Typ
Definition: bootstrap.c:161
static void ShutdownAuxiliaryProcess(int code, Datum arg)
Definition: bootstrap.c:559
uintptr_t Datum
Definition: postgres.h:365
bool SelectConfigFiles(const char *userDoption, const char *progname)
Definition: guc.c:4920
AuxProcType MyAuxProcType
Definition: bootstrap.c:69
static void cleanup(void)
Definition: bootstrap.c:875
Oid simple_heap_insert(Relation relation, HeapTuple tup)
Definition: heapam.c:2986
HeapTuple heap_getnext(HeapScanDesc scan, ScanDirection direction)
Definition: heapam.c:1835
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1294
static struct @131 value
TupleDesc rd_att
Definition: rel.h:85
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:814
Relation heap_openrv(const RangeVar *relation, LOCKMODE lockmode)
Definition: heapam.c:1323
#define InvalidOid
Definition: postgres_ext.h:36
void CheckpointerMain(void)
Definition: checkpointer.c:193
int16 attnum
Definition: pg_attribute.h:79
void InitPostgres(const char *in_dbname, Oid dboid, const char *username, Oid useroid, char *out_dbname, bool override_allow_connections)
Definition: postinit.c:565
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:168
#define free(a)
Definition: header.h:65
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
int errmsg_internal(const char *fmt,...)
Definition: elog.c:827
List * ii_Expressions
Definition: execnodes.h:154
#define Assert(condition)
Definition: c.h:699
#define ALLOC(t, c)
Definition: bootstrap.c:53
void StartTransactionCommand(void)
Definition: xact.c:2673
char storage
Definition: bootstrap.c:94
FormData_pg_type am_typ
Definition: bootstrap.c:158
void InitializeMaxBackends(void)
Definition: postinit.c:502
static Form_pg_attribute AllocateAttribute(void)
Definition: bootstrap.c:1033
static const int n_types
Definition: bootstrap.c:153
static const struct typinfo TypInfo[]
Definition: bootstrap.c:100
void BackgroundWriterMain(void)
Definition: bgwriter.c:110
bool byval
Definition: bootstrap.c:92
Oid il_heap
Definition: bootstrap.c:177
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:176
int WalSegSz
Definition: pg_standby.c:39
static const char * userDoption
Definition: postgres.c:160
static Datum values[MAXATTR]
Definition: bootstrap.c:164
#define IsBootstrapProcessingMode()
Definition: miscadmin.h:372
void ProcSignalInit(int pss_idx)
Definition: procsignal.c:105
#define PG_DATA_CHECKSUM_VERSION
Definition: bufpage.h:196
static Oid gettype(char *type)
Definition: bootstrap.c:893
Oid * ii_ExclusionOps
Definition: execnodes.h:158
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:1832
Oid am_oid
Definition: bootstrap.c:157
void * palloc(Size size)
Definition: mcxt.c:924
int errmsg(const char *fmt,...)
Definition: elog.c:797
void pgstat_bestart(void)
Definition: pgstat.c:2785
char * optarg
Definition: getopt.c:53
void die(SIGNAL_ARGS)
Definition: postgres.c:2657
int i
#define NameStr(name)
Definition: c.h:576
static void CheckerModeMain(void)
Definition: bootstrap.c:478
void * arg
void AuxiliaryProcessMain(int argc, char *argv[])
Definition: bootstrap.c:195
void InitBufferPoolBackend(void)
Definition: bufmgr.c:2461
uint32 bootstrap_data_checksum_version
Definition: bootstrap.c:50
Oid outproc
Definition: bootstrap.c:97
Oid oid
Definition: bootstrap.c:89
#define elog
Definition: elog.h:219
void build_indices(void)
Definition: bootstrap.c:1101
void LWLockReleaseAll(void)
Definition: lwlock.c:1824
#define HeapTupleGetOid(tuple)
Definition: htup_details.h:712
#define copyObject(obj)
Definition: nodes.h:630
uint16 * ii_ExclusionStrats
Definition: execnodes.h:160
void InsertOneNull(int i)
Definition: bootstrap.c:857
Datum OidInputFunctionCall(Oid functionId, char *str, Oid typioparam, int32 typmod)
Definition: fmgr.c:1823
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition: indexam.c:150
RangeVar * makeRangeVar(char *schemaname, char *relname, int location)
Definition: makefuncs.c:421
void init_ps_display(const char *username, const char *dbname, const char *host_info, const char *initial_str)
Definition: ps_status.c:244
void InitializeGUCOptions(void)
Definition: guc.c:4698
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:959
static bool Nulls[MAXATTR]
Definition: bootstrap.c:165
Form_pg_attribute attrtypes[MAXATTR]
Definition: bootstrap.c:73
bool typbyval
Definition: pg_type.h:65