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