OSDN Git Service

748420401ce08054b7f81ed5649f928c639c9fb1
[pg-rex/syncrep.git] / 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-2010, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * IDENTIFICATION
11  *        $PostgreSQL: pgsql/src/backend/bootstrap/bootstrap.c,v 1.255 2010/01/02 16:57:36 momjian Exp $
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16
17 #include <time.h>
18 #include <unistd.h>
19 #include <signal.h>
20 #ifdef HAVE_GETOPT_H
21 #include <getopt.h>
22 #endif
23
24 #include "access/genam.h"
25 #include "access/heapam.h"
26 #include "access/xact.h"
27 #include "bootstrap/bootstrap.h"
28 #include "catalog/index.h"
29 #include "catalog/pg_type.h"
30 #include "libpq/pqsignal.h"
31 #include "miscadmin.h"
32 #include "nodes/makefuncs.h"
33 #include "postmaster/bgwriter.h"
34 #include "postmaster/walwriter.h"
35 #include "storage/bufmgr.h"
36 #include "storage/ipc.h"
37 #include "storage/proc.h"
38 #include "storage/procsignal.h"
39 #include "tcop/tcopprot.h"
40 #include "utils/builtins.h"
41 #include "utils/fmgroids.h"
42 #include "utils/memutils.h"
43 #include "utils/ps_status.h"
44 #include "utils/tqual.h"
45
46 extern int      optind;
47 extern char *optarg;
48
49
50 #define ALLOC(t, c)             ((t *) calloc((unsigned)(c), sizeof(t)))
51
52 static void CheckerModeMain(void);
53 static void BootstrapModeMain(void);
54 static void bootstrap_signals(void);
55 static void ShutdownAuxiliaryProcess(int code, Datum arg);
56 static Form_pg_attribute AllocateAttribute(void);
57 static Oid      gettype(char *type);
58 static void cleanup(void);
59
60 /* ----------------
61  *              global variables
62  * ----------------
63  */
64
65 Relation        boot_reldesc;           /* current relation descriptor */
66
67 Form_pg_attribute attrtypes[MAXATTR];   /* points to attribute info */
68 int                     numattr;                        /* number of attributes for cur. rel */
69
70
71 /*
72  * Basic information associated with each type.  This is used before
73  * pg_type is created.
74  *
75  *              XXX several of these input/output functions do catalog scans
76  *                      (e.g., F_REGPROCIN scans pg_proc).      this obviously creates some
77  *                      order dependencies in the catalog creation process.
78  */
79 struct typinfo
80 {
81         char            name[NAMEDATALEN];
82         Oid                     oid;
83         Oid                     elem;
84         int16           len;
85         bool            byval;
86         char            align;
87         char            storage;
88         Oid                     inproc;
89         Oid                     outproc;
90 };
91
92 static const struct typinfo TypInfo[] = {
93         {"bool", BOOLOID, 0, 1, true, 'c', 'p',
94         F_BOOLIN, F_BOOLOUT},
95         {"bytea", BYTEAOID, 0, -1, false, 'i', 'x',
96         F_BYTEAIN, F_BYTEAOUT},
97         {"char", CHAROID, 0, 1, true, 'c', 'p',
98         F_CHARIN, F_CHAROUT},
99         {"int2", INT2OID, 0, 2, true, 's', 'p',
100         F_INT2IN, F_INT2OUT},
101         {"int4", INT4OID, 0, 4, true, 'i', 'p',
102         F_INT4IN, F_INT4OUT},
103         {"float4", FLOAT4OID, 0, 4, FLOAT4PASSBYVAL, 'i', 'p',
104         F_FLOAT4IN, F_FLOAT4OUT},
105         {"name", NAMEOID, CHAROID, NAMEDATALEN, false, 'c', 'p',
106         F_NAMEIN, F_NAMEOUT},
107         {"regclass", REGCLASSOID, 0, 4, true, 'i', 'p',
108         F_REGCLASSIN, F_REGCLASSOUT},
109         {"regproc", REGPROCOID, 0, 4, true, 'i', 'p',
110         F_REGPROCIN, F_REGPROCOUT},
111         {"regtype", REGTYPEOID, 0, 4, true, 'i', 'p',
112         F_REGTYPEIN, F_REGTYPEOUT},
113         {"text", TEXTOID, 0, -1, false, 'i', 'x',
114         F_TEXTIN, F_TEXTOUT},
115         {"oid", OIDOID, 0, 4, true, 'i', 'p',
116         F_OIDIN, F_OIDOUT},
117         {"tid", TIDOID, 0, 6, false, 's', 'p',
118         F_TIDIN, F_TIDOUT},
119         {"xid", XIDOID, 0, 4, true, 'i', 'p',
120         F_XIDIN, F_XIDOUT},
121         {"cid", CIDOID, 0, 4, true, 'i', 'p',
122         F_CIDIN, F_CIDOUT},
123         {"int2vector", INT2VECTOROID, INT2OID, -1, false, 'i', 'p',
124         F_INT2VECTORIN, F_INT2VECTOROUT},
125         {"oidvector", OIDVECTOROID, OIDOID, -1, false, 'i', 'p',
126         F_OIDVECTORIN, F_OIDVECTOROUT},
127         {"_int4", INT4ARRAYOID, INT4OID, -1, false, 'i', 'x',
128         F_ARRAY_IN, F_ARRAY_OUT},
129         {"_text", 1009, TEXTOID, -1, false, 'i', 'x',
130         F_ARRAY_IN, F_ARRAY_OUT},
131         {"_oid", 1028, OIDOID, -1, false, 'i', 'x',
132         F_ARRAY_IN, F_ARRAY_OUT},
133         {"_char", 1002, CHAROID, -1, false, 'i', 'x',
134         F_ARRAY_IN, F_ARRAY_OUT},
135         {"_aclitem", 1034, ACLITEMOID, -1, false, 'i', 'x',
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 */
143         Oid                     am_oid;
144         FormData_pg_type am_typ;
145 };
146
147 static struct typmap **Typ = NULL;
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 {
163         Oid                     il_heap;
164         Oid                     il_ind;
165         IndexInfo  *il_info;
166         struct _IndexList *il_next;
167 } IndexList;
168
169 static IndexList *ILHead = NULL;
170
171
172 /*
173  *       AuxiliaryProcessMain
174  *
175  *       The main entry point for auxiliary processes, such as the bgwriter,
176  *       walwriter, bootstrapper and the shared memory checker code.
177  *
178  *       This code is here just because of historical reasons.
179  */
180 void
181 AuxiliaryProcessMain(int argc, char *argv[])
182 {
183         char       *progname = argv[0];
184         int                     flag;
185         AuxProcType auxType = CheckerProcess;
186         char       *userDoption = NULL;
187
188         /*
189          * initialize globals
190          */
191         MyProcPid = getpid();
192
193         MyStartTime = time(NULL);
194
195         /*
196          * Fire up essential subsystems: error and memory management
197          *
198          * If we are running under the postmaster, this is done already.
199          */
200         if (!IsUnderPostmaster)
201                 MemoryContextInit();
202
203         /* Compute paths, if we didn't inherit them from postmaster */
204         if (my_exec_path[0] == '\0')
205         {
206                 if (find_my_exec(progname, my_exec_path) < 0)
207                         elog(FATAL, "%s: could not locate my own executable path",
208                                  progname);
209         }
210
211         /*
212          * process command arguments
213          */
214
215         /* Set defaults, to be overriden by explicit options below */
216         if (!IsUnderPostmaster)
217                 InitializeGUCOptions();
218
219         /* Ignore the initial --boot argument, if present */
220         if (argc > 1 && strcmp(argv[1], "--boot") == 0)
221         {
222                 argv++;
223                 argc--;
224         }
225
226         while ((flag = getopt(argc, argv, "B:c:d:D:Fr: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 = optarg;
235                                 break;
236                         case 'd':
237                                 {
238                                         /* Turn on debugging for the bootstrap process. */
239                                         char       *debugstr = palloc(strlen("debug") + strlen(optarg) + 1);
240
241                                         sprintf(debugstr, "debug%s", optarg);
242                                         SetConfigOption("log_min_messages", debugstr,
243                                                                         PGC_POSTMASTER, PGC_S_ARGV);
244                                         SetConfigOption("client_min_messages", debugstr,
245                                                                         PGC_POSTMASTER, PGC_S_ARGV);
246                                         pfree(debugstr);
247                                 }
248                                 break;
249                         case 'F':
250                                 SetConfigOption("fsync", "false", PGC_POSTMASTER, PGC_S_ARGV);
251                                 break;
252                         case 'r':
253                                 strlcpy(OutputFileName, optarg, MAXPGPATH);
254                                 break;
255                         case 'x':
256                                 auxType = atoi(optarg);
257                                 break;
258                         case 'c':
259                         case '-':
260                                 {
261                                         char       *name,
262                                                            *value;
263
264                                         ParseLongOption(optarg, &name, &value);
265                                         if (!value)
266                                         {
267                                                 if (flag == '-')
268                                                         ereport(ERROR,
269                                                                         (errcode(ERRCODE_SYNTAX_ERROR),
270                                                                          errmsg("--%s requires a value",
271                                                                                         optarg)));
272                                                 else
273                                                         ereport(ERROR,
274                                                                         (errcode(ERRCODE_SYNTAX_ERROR),
275                                                                          errmsg("-c %s requires a value",
276                                                                                         optarg)));
277                                         }
278
279                                         SetConfigOption(name, value, PGC_POSTMASTER, PGC_S_ARGV);
280                                         free(name);
281                                         if (value)
282                                                 free(value);
283                                         break;
284                                 }
285                         default:
286                                 write_stderr("Try \"%s --help\" for more information.\n",
287                                                          progname);
288                                 proc_exit(1);
289                                 break;
290                 }
291         }
292
293         if (argc != optind)
294         {
295                 write_stderr("%s: invalid command-line arguments\n", progname);
296                 proc_exit(1);
297         }
298
299         /*
300          * Identify myself via ps
301          */
302         if (IsUnderPostmaster)
303         {
304                 const char *statmsg;
305
306                 switch (auxType)
307                 {
308                         case StartupProcess:
309                                 statmsg = "startup process";
310                                 break;
311                         case BgWriterProcess:
312                                 statmsg = "writer process";
313                                 break;
314                         case WalWriterProcess:
315                                 statmsg = "wal writer 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                 /* If timezone is not set, determine what the OS uses */
330                 pg_timezone_initialize();
331                 /* If timezone_abbreviations is not set, select default */
332                 pg_timezone_abbrev_initialize();
333         }
334
335         /* Validate we have been given a reasonable-looking DataDir */
336         Assert(DataDir);
337         ValidatePgVersion(DataDir);
338
339         /* Change into DataDir (if under postmaster, should be done already) */
340         if (!IsUnderPostmaster)
341                 ChangeToDataDir();
342
343         /* If standalone, create lockfile for data directory */
344         if (!IsUnderPostmaster)
345                 CreateDataDirLockFile(false);
346
347         SetProcessingMode(BootstrapProcessing);
348         IgnoreSystemIndexes = true;
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 (auxType).  Backends use slots indexed
371                  * 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 + auxType + 1);
379
380                 /* finish setting up bufmgr.c */
381                 InitBufferPoolBackend();
382
383                 /* register a shutdown callback for LWLock cleanup */
384                 on_shmem_exit(ShutdownAuxiliaryProcess, 0);
385         }
386
387         /*
388          * XLOG operations
389          */
390         SetProcessingMode(NormalProcessing);
391
392         switch (auxType)
393         {
394                 case CheckerProcess:
395                         bootstrap_signals();
396                         CheckerModeMain();
397                         proc_exit(1);           /* should never return */
398
399                 case BootstrapProcess:
400                         bootstrap_signals();
401                         BootStrapXLOG();
402                         StartupXLOG();
403                         BootstrapModeMain();
404                         proc_exit(1);           /* should never return */
405
406                 case StartupProcess:
407                         /* don't set signals, startup process has its own agenda */
408                         StartupProcessMain();
409                         proc_exit(1);           /* should never return */
410
411                 case BgWriterProcess:
412                         /* don't set signals, bgwriter has its own agenda */
413                         BackgroundWriterMain();
414                         proc_exit(1);           /* should never return */
415
416                 case WalWriterProcess:
417                         /* don't set signals, walwriter has its own agenda */
418                         InitXLOGAccess();
419                         WalWriterMain();
420                         proc_exit(1);           /* should never return */
421
422                 default:
423                         elog(PANIC, "unrecognized process type: %d", auxType);
424                         proc_exit(1);
425         }
426 }
427
428 /*
429  * In shared memory checker mode, all we really want to do is create shared
430  * memory and semaphores (just to prove we can do it with the current GUC
431  * settings).
432  */
433 static void
434 CheckerModeMain(void)
435 {
436         /*
437          * We must be getting invoked for bootstrap mode
438          */
439         Assert(!IsUnderPostmaster);
440
441         SetProcessingMode(BootstrapProcessing);
442
443         /*
444          * Do backend-like initialization for bootstrap mode
445          */
446         InitProcess();
447         InitPostgres(NULL, InvalidOid, NULL, NULL);
448         proc_exit(0);
449 }
450
451 /*
452  *       The main entry point for running the backend in bootstrap mode
453  *
454  *       The bootstrap mode is used to initialize the template database.
455  *       The bootstrap backend doesn't speak SQL, but instead expects
456  *       commands in a special bootstrap language.
457  */
458 static void
459 BootstrapModeMain(void)
460 {
461         int                     i;
462
463         Assert(!IsUnderPostmaster);
464
465         SetProcessingMode(BootstrapProcessing);
466
467         /*
468          * Do backend-like initialization for bootstrap mode
469          */
470         InitProcess();
471         InitPostgres(NULL, InvalidOid, NULL, NULL);
472
473         /* Initialize stuff for bootstrap-file processing */
474         for (i = 0; i < MAXATTR; i++)
475         {
476                 attrtypes[i] = NULL;
477                 Nulls[i] = false;
478         }
479
480         /*
481          * Process bootstrap input.
482          */
483         boot_yyparse();
484
485         /* Perform a checkpoint to ensure everything's down to disk */
486         SetProcessingMode(NormalProcessing);
487         CreateCheckPoint(CHECKPOINT_IS_SHUTDOWN | CHECKPOINT_IMMEDIATE);
488
489         /* Clean up and exit */
490         cleanup();
491         proc_exit(0);
492 }
493
494
495 /* ----------------------------------------------------------------
496  *                                              misc functions
497  * ----------------------------------------------------------------
498  */
499
500 /*
501  * Set up signal handling for a bootstrap process
502  */
503 static void
504 bootstrap_signals(void)
505 {
506         if (IsUnderPostmaster)
507         {
508                 /*
509                  * If possible, make this process a group leader, so that the
510                  * postmaster can signal any child processes too.
511                  */
512 #ifdef HAVE_SETSID
513                 if (setsid() < 0)
514                         elog(FATAL, "setsid() failed: %m");
515 #endif
516
517                 /*
518                  * Properly accept or ignore signals the postmaster might send us
519                  */
520                 pqsignal(SIGHUP, SIG_IGN);
521                 pqsignal(SIGINT, SIG_IGN);              /* ignore query-cancel */
522                 pqsignal(SIGTERM, die);
523                 pqsignal(SIGQUIT, quickdie);
524                 pqsignal(SIGALRM, SIG_IGN);
525                 pqsignal(SIGPIPE, SIG_IGN);
526                 pqsignal(SIGUSR1, SIG_IGN);
527                 pqsignal(SIGUSR2, SIG_IGN);
528
529                 /*
530                  * Reset some signals that are accepted by postmaster but not here
531                  */
532                 pqsignal(SIGCHLD, SIG_DFL);
533                 pqsignal(SIGTTIN, SIG_DFL);
534                 pqsignal(SIGTTOU, SIG_DFL);
535                 pqsignal(SIGCONT, SIG_DFL);
536                 pqsignal(SIGWINCH, SIG_DFL);
537
538                 /*
539                  * Unblock signals (they were blocked when the postmaster forked us)
540                  */
541                 PG_SETMASK(&UnBlockSig);
542         }
543         else
544         {
545                 /* Set up appropriately for interactive use */
546                 pqsignal(SIGHUP, die);
547                 pqsignal(SIGINT, die);
548                 pqsignal(SIGTERM, die);
549                 pqsignal(SIGQUIT, die);
550         }
551 }
552
553 /*
554  * Begin shutdown of an auxiliary process.      This is approximately the equivalent
555  * of ShutdownPostgres() in postinit.c.  We can't run transactions in an
556  * auxiliary process, so most of the work of AbortTransaction() is not needed,
557  * but we do need to make sure we've released any LWLocks we are holding.
558  * (This is only critical during an error exit.)
559  */
560 static void
561 ShutdownAuxiliaryProcess(int code, Datum arg)
562 {
563         LWLockReleaseAll();
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(rel, SnapshotNow, 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(rel, SnapshotNow, 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 = boot_reldesc->rd_rel->relnatts;
622         for (i = 0; i < numattr; i++)
623         {
624                 if (attrtypes[i] == NULL)
625                         attrtypes[i] = AllocateAttribute();
626                 memmove((char *) attrtypes[i],
627                                 (char *) boot_reldesc->rd_att->attrs[i],
628                                 ATTRIBUTE_FIXED_PART_SIZE);
629
630                 {
631                         Form_pg_attribute at = attrtypes[i];
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
645 closerel(char *name)
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)
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)
693                 attrtypes[attnum] = AllocateAttribute();
694         MemSet(attrtypes[attnum], 0, ATTRIBUTE_FIXED_PART_SIZE);
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                 /* if an array type, assume 1-dimensional attribute */
710                 if (Ap->am_typ.typelem != InvalidOid && Ap->am_typ.typlen < 0)
711                         attrtypes[attnum]->attndims = 1;
712                 else
713                         attrtypes[attnum]->attndims = 0;
714         }
715         else
716         {
717                 attrtypes[attnum]->atttypid = TypInfo[typeoid].oid;
718                 attrtypes[attnum]->attlen = TypInfo[typeoid].len;
719                 attrtypes[attnum]->attbyval = TypInfo[typeoid].byval;
720                 attrtypes[attnum]->attstorage = TypInfo[typeoid].storage;
721                 attrtypes[attnum]->attalign = TypInfo[typeoid].align;
722                 /* if an array type, assume 1-dimensional attribute */
723                 if (TypInfo[typeoid].elem != InvalidOid &&
724                         attrtypes[attnum]->attlen < 0)
725                         attrtypes[attnum]->attndims = 1;
726                 else
727                         attrtypes[attnum]->attndims = 0;
728         }
729
730         attrtypes[attnum]->attstattarget = -1;
731         attrtypes[attnum]->attdistinct = 0;
732         attrtypes[attnum]->attcacheoff = -1;
733         attrtypes[attnum]->atttypmod = -1;
734         attrtypes[attnum]->attislocal = true;
735
736         /*
737          * Mark as "not null" if type is fixed-width and prior columns are too.
738          * This corresponds to case where column can be accessed directly via C
739          * struct declaration.
740          *
741          * oidvector and int2vector are also treated as not-nullable, even though
742          * they are no longer fixed-width.
743          */
744 #define MARKNOTNULL(att) \
745         ((att)->attlen > 0 || \
746          (att)->atttypid == OIDVECTOROID || \
747          (att)->atttypid == INT2VECTOROID)
748
749         if (MARKNOTNULL(attrtypes[attnum]))
750         {
751                 int                     i;
752
753                 for (i = 0; i < attnum; i++)
754                 {
755                         if (!MARKNOTNULL(attrtypes[i]))
756                                 break;
757                 }
758                 if (i == attnum)
759                         attrtypes[attnum]->attnotnull = true;
760         }
761 }
762
763
764 /* ----------------
765  *              InsertOneTuple
766  *
767  * If objectid is not zero, it is a specific OID to assign to the tuple.
768  * Otherwise, an OID will be assigned (if necessary) by heap_insert.
769  * ----------------
770  */
771 void
772 InsertOneTuple(Oid objectid)
773 {
774         HeapTuple       tuple;
775         TupleDesc       tupDesc;
776         int                     i;
777
778         elog(DEBUG4, "inserting row oid %u, %d columns", objectid, numattr);
779
780         tupDesc = CreateTupleDesc(numattr,
781                                                           RelationGetForm(boot_reldesc)->relhasoids,
782                                                           attrtypes);
783         tuple = heap_form_tuple(tupDesc, values, Nulls);
784         if (objectid != (Oid) 0)
785                 HeapTupleSetOid(tuple, objectid);
786         pfree(tupDesc);                         /* just free's tupDesc, not the attrtypes */
787
788         simple_heap_insert(boot_reldesc, tuple);
789         heap_freetuple(tuple);
790         elog(DEBUG4, "row inserted");
791
792         /*
793          * Reset null markers for next tuple
794          */
795         for (i = 0; i < numattr; i++)
796                 Nulls[i] = false;
797 }
798
799 /* ----------------
800  *              InsertOneValue
801  * ----------------
802  */
803 void
804 InsertOneValue(char *value, int i)
805 {
806         Oid                     typoid;
807         int16           typlen;
808         bool            typbyval;
809         char            typalign;
810         char            typdelim;
811         Oid                     typioparam;
812         Oid                     typinput;
813         Oid                     typoutput;
814         char       *prt;
815
816         AssertArg(i >= 0 || i < MAXATTR);
817
818         elog(DEBUG4, "inserting column %d value \"%s\"", i, value);
819
820         typoid = boot_reldesc->rd_att->attrs[i]->atttypid;
821
822         boot_get_type_io_data(typoid,
823                                                   &typlen, &typbyval, &typalign,
824                                                   &typdelim, &typioparam,
825                                                   &typinput, &typoutput);
826
827         values[i] = OidInputFunctionCall(typinput, value, typioparam, -1);
828         prt = OidOutputFunctionCall(typoutput, values[i]);
829         elog(DEBUG4, "inserted -> %s", prt);
830         pfree(prt);
831 }
832
833 /* ----------------
834  *              InsertOneNull
835  * ----------------
836  */
837 void
838 InsertOneNull(int i)
839 {
840         elog(DEBUG4, "inserting column %d NULL", i);
841         Assert(i >= 0 || i < MAXATTR);
842         values[i] = PointerGetDatum(NULL);
843         Nulls[i] = true;
844 }
845
846 /* ----------------
847  *              cleanup
848  * ----------------
849  */
850 static void
851 cleanup(void)
852 {
853         if (boot_reldesc != NULL)
854                 closerel(NULL);
855 }
856
857 /* ----------------
858  *              gettype
859  *
860  * NB: this is really ugly; it will return an integer index into TypInfo[],
861  * and not an OID at all, until the first reference to a type not known in
862  * TypInfo[].  At that point it will read and cache pg_type in the Typ array,
863  * and subsequently return a real OID (and set the global pointer Ap to
864  * point at the found row in Typ).      So caller must check whether Typ is
865  * still NULL to determine what the return value is!
866  * ----------------
867  */
868 static Oid
869 gettype(char *type)
870 {
871         int                     i;
872         Relation        rel;
873         HeapScanDesc scan;
874         HeapTuple       tup;
875         struct typmap **app;
876
877         if (Typ != NULL)
878         {
879                 for (app = Typ; *app != NULL; app++)
880                 {
881                         if (strncmp(NameStr((*app)->am_typ.typname), type, NAMEDATALEN) == 0)
882                         {
883                                 Ap = *app;
884                                 return (*app)->am_oid;
885                         }
886                 }
887         }
888         else
889         {
890                 for (i = 0; i < n_types; i++)
891                 {
892                         if (strncmp(type, TypInfo[i].name, NAMEDATALEN) == 0)
893                                 return i;
894                 }
895                 elog(DEBUG4, "external type: %s", type);
896                 rel = heap_open(TypeRelationId, NoLock);
897                 scan = heap_beginscan(rel, SnapshotNow, 0, NULL);
898                 i = 0;
899                 while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
900                         ++i;
901                 heap_endscan(scan);
902                 app = Typ = ALLOC(struct typmap *, i + 1);
903                 while (i-- > 0)
904                         *app++ = ALLOC(struct typmap, 1);
905                 *app = NULL;
906                 scan = heap_beginscan(rel, SnapshotNow, 0, NULL);
907                 app = Typ;
908                 while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
909                 {
910                         (*app)->am_oid = HeapTupleGetOid(tup);
911                         memmove((char *) &(*app++)->am_typ,
912                                         (char *) GETSTRUCT(tup),
913                                         sizeof((*app)->am_typ));
914                 }
915                 heap_endscan(scan);
916                 heap_close(rel, NoLock);
917                 return gettype(type);
918         }
919         elog(ERROR, "unrecognized type \"%s\"", type);
920         /* not reached, here to make compiler happy */
921         return 0;
922 }
923
924 /* ----------------
925  *              boot_get_type_io_data
926  *
927  * Obtain type I/O information at bootstrap time.  This intentionally has
928  * almost the same API as lsyscache.c's get_type_io_data, except that
929  * we only support obtaining the typinput and typoutput routines, not
930  * the binary I/O routines.  It is exported so that array_in and array_out
931  * can be made to work during early bootstrap.
932  * ----------------
933  */
934 void
935 boot_get_type_io_data(Oid typid,
936                                           int16 *typlen,
937                                           bool *typbyval,
938                                           char *typalign,
939                                           char *typdelim,
940                                           Oid *typioparam,
941                                           Oid *typinput,
942                                           Oid *typoutput)
943 {
944         if (Typ != NULL)
945         {
946                 /* We have the boot-time contents of pg_type, so use it */
947                 struct typmap **app;
948                 struct typmap *ap;
949
950                 app = Typ;
951                 while (*app && (*app)->am_oid != typid)
952                         ++app;
953                 ap = *app;
954                 if (ap == NULL)
955                         elog(ERROR, "type OID %u not found in Typ list", typid);
956
957                 *typlen = ap->am_typ.typlen;
958                 *typbyval = ap->am_typ.typbyval;
959                 *typalign = ap->am_typ.typalign;
960                 *typdelim = ap->am_typ.typdelim;
961
962                 /* XXX this logic must match getTypeIOParam() */
963                 if (OidIsValid(ap->am_typ.typelem))
964                         *typioparam = ap->am_typ.typelem;
965                 else
966                         *typioparam = typid;
967
968                 *typinput = ap->am_typ.typinput;
969                 *typoutput = ap->am_typ.typoutput;
970         }
971         else
972         {
973                 /* We don't have pg_type yet, so use the hard-wired TypInfo array */
974                 int                     typeindex;
975
976                 for (typeindex = 0; typeindex < n_types; typeindex++)
977                 {
978                         if (TypInfo[typeindex].oid == typid)
979                                 break;
980                 }
981                 if (typeindex >= n_types)
982                         elog(ERROR, "type OID %u not found in TypInfo", typid);
983
984                 *typlen = TypInfo[typeindex].len;
985                 *typbyval = TypInfo[typeindex].byval;
986                 *typalign = TypInfo[typeindex].align;
987                 /* We assume typdelim is ',' for all boot-time types */
988                 *typdelim = ',';
989
990                 /* XXX this logic must match getTypeIOParam() */
991                 if (OidIsValid(TypInfo[typeindex].elem))
992                         *typioparam = TypInfo[typeindex].elem;
993                 else
994                         *typioparam = typid;
995
996                 *typinput = TypInfo[typeindex].inproc;
997                 *typoutput = TypInfo[typeindex].outproc;
998         }
999 }
1000
1001 /* ----------------
1002  *              AllocateAttribute
1003  *
1004  * Note: bootstrap never sets any per-column ACLs, so we only need
1005  * ATTRIBUTE_FIXED_PART_SIZE space per attribute.
1006  * ----------------
1007  */
1008 static Form_pg_attribute
1009 AllocateAttribute(void)
1010 {
1011         Form_pg_attribute attribute = (Form_pg_attribute) malloc(ATTRIBUTE_FIXED_PART_SIZE);
1012
1013         if (!PointerIsValid(attribute))
1014                 elog(FATAL, "out of memory");
1015         MemSet(attribute, 0, ATTRIBUTE_FIXED_PART_SIZE);
1016
1017         return attribute;
1018 }
1019
1020 /* ----------------
1021  *              MapArrayTypeName
1022  * XXX arrays of "basetype" are always "_basetype".
1023  *         this is an evil hack inherited from rel. 3.1.
1024  * XXX array dimension is thrown away because we
1025  *         don't support fixed-dimension arrays.  again,
1026  *         sickness from 3.1.
1027  *
1028  * the string passed in must have a '[' character in it
1029  *
1030  * the string returned is a pointer to static storage and should NOT
1031  * be freed by the CALLER.
1032  * ----------------
1033  */
1034 char *
1035 MapArrayTypeName(char *s)
1036 {
1037         int                     i,
1038                                 j;
1039         static char newStr[NAMEDATALEN];        /* array type names < NAMEDATALEN long */
1040
1041         if (s == NULL || s[0] == '\0')
1042                 return s;
1043
1044         j = 1;
1045         newStr[0] = '_';
1046         for (i = 0; i < NAMEDATALEN - 1 && s[i] != '['; i++, j++)
1047                 newStr[j] = s[i];
1048
1049         newStr[j] = '\0';
1050
1051         return newStr;
1052 }
1053
1054
1055 /*
1056  *      index_register() -- record an index that has been set up for building
1057  *                                              later.
1058  *
1059  *              At bootstrap time, we define a bunch of indexes on system catalogs.
1060  *              We postpone actually building the indexes until just before we're
1061  *              finished with initialization, however.  This is because the indexes
1062  *              themselves have catalog entries, and those have to be included in the
1063  *              indexes on those catalogs.      Doing it in two phases is the simplest
1064  *              way of making sure the indexes have the right contents at the end.
1065  */
1066 void
1067 index_register(Oid heap,
1068                            Oid ind,
1069                            IndexInfo *indexInfo)
1070 {
1071         IndexList  *newind;
1072         MemoryContext oldcxt;
1073
1074         /*
1075          * XXX mao 10/31/92 -- don't gc index reldescs, associated info at
1076          * bootstrap time.      we'll declare the indexes now, but want to create them
1077          * later.
1078          */
1079
1080         if (nogc == NULL)
1081                 nogc = AllocSetContextCreate(NULL,
1082                                                                          "BootstrapNoGC",
1083                                                                          ALLOCSET_DEFAULT_MINSIZE,
1084                                                                          ALLOCSET_DEFAULT_INITSIZE,
1085                                                                          ALLOCSET_DEFAULT_MAXSIZE);
1086
1087         oldcxt = MemoryContextSwitchTo(nogc);
1088
1089         newind = (IndexList *) palloc(sizeof(IndexList));
1090         newind->il_heap = heap;
1091         newind->il_ind = ind;
1092         newind->il_info = (IndexInfo *) palloc(sizeof(IndexInfo));
1093
1094         memcpy(newind->il_info, indexInfo, sizeof(IndexInfo));
1095         /* expressions will likely be null, but may as well copy it */
1096         newind->il_info->ii_Expressions = (List *)
1097                 copyObject(indexInfo->ii_Expressions);
1098         newind->il_info->ii_ExpressionsState = NIL;
1099         /* predicate will likely be null, but may as well copy it */
1100         newind->il_info->ii_Predicate = (List *)
1101                 copyObject(indexInfo->ii_Predicate);
1102         newind->il_info->ii_PredicateState = NIL;
1103         /* no exclusion constraints at bootstrap time, so no need to copy */
1104         Assert(indexInfo->ii_ExclusionOps == NULL);
1105         Assert(indexInfo->ii_ExclusionProcs == NULL);
1106         Assert(indexInfo->ii_ExclusionStrats == NULL);
1107
1108         newind->il_next = ILHead;
1109         ILHead = newind;
1110
1111         MemoryContextSwitchTo(oldcxt);
1112 }
1113
1114
1115 /*
1116  * build_indices -- fill in all the indexes registered earlier
1117  */
1118 void
1119 build_indices(void)
1120 {
1121         for (; ILHead != NULL; ILHead = ILHead->il_next)
1122         {
1123                 Relation        heap;
1124                 Relation        ind;
1125
1126                 /* need not bother with locks during bootstrap */
1127                 heap = heap_open(ILHead->il_heap, NoLock);
1128                 ind = index_open(ILHead->il_ind, NoLock);
1129
1130                 index_build(heap, ind, ILHead->il_info, false);
1131
1132                 index_close(ind, NoLock);
1133                 heap_close(heap, NoLock);
1134         }
1135 }