]> granicus.if.org Git - postgresql/blob - src/backend/bootstrap/bootstrap.c
Code review for FILLFACTOR patch. Change WITH grammar as per earlier
[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-2006, 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.218 2006/06/27 22:16:43 momjian Exp $
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16
17 #include <unistd.h>
18 #include <signal.h>
19 #ifdef HAVE_GETOPT_H
20 #include <getopt.h>
21 #endif
22
23 #define BOOTSTRAP_INCLUDE               /* mask out stuff in tcop/tcopprot.h */
24
25 #include "access/genam.h"
26 #include "access/heapam.h"
27 #include "access/xlog.h"
28 #include "bootstrap/bootstrap.h"
29 #include "catalog/index.h"
30 #include "catalog/pg_type.h"
31 #include "executor/executor.h"
32 #include "libpq/pqsignal.h"
33 #include "miscadmin.h"
34 #include "nodes/makefuncs.h"
35 #include "postmaster/bgwriter.h"
36 #include "storage/freespace.h"
37 #include "storage/ipc.h"
38 #include "storage/pg_shmem.h"
39 #include "storage/proc.h"
40 #include "tcop/tcopprot.h"
41 #include "utils/builtins.h"
42 #include "utils/flatfiles.h"
43 #include "utils/fmgroids.h"
44 #include "utils/guc.h"
45 #include "utils/lsyscache.h"
46 #include "utils/memutils.h"
47 #include "utils/ps_status.h"
48 #include "utils/relcache.h"
49
50 extern int      optind;
51 extern char *optarg;
52
53
54 #define ALLOC(t, c)             ((t *) calloc((unsigned)(c), sizeof(t)))
55
56 static void bootstrap_signals(void);
57 static void ShutdownDummyProcess(int code, Datum arg);
58 static hashnode *AddStr(char *str, int strlength, int mderef);
59 static Form_pg_attribute AllocateAttribute(void);
60 static int      CompHash(char *str, int len);
61 static hashnode *FindStr(char *str, int length, hashnode *mderef);
62 static Oid      gettype(char *type);
63 static void cleanup(void);
64
65 /* ----------------
66  *              global variables
67  * ----------------
68  */
69
70 Relation        boot_reldesc;           /* current relation descriptor */
71
72 /*
73  * In the lexical analyzer, we need to get the reference number quickly from
74  * the string, and the string from the reference number.  Thus we have
75  * as our data structure a hash table, where the hashing key taken from
76  * the particular string.  The hash table is chained.  One of the fields
77  * of the hash table node is an index into the array of character pointers.
78  * The unique index number that every string is assigned is simply the
79  * position of its string pointer in the array of string pointers.
80  */
81
82 #define STRTABLESIZE    10000
83 #define HASHTABLESIZE   503
84
85 /* Hash function numbers */
86 #define NUM             23
87 #define NUMSQR  529
88 #define NUMCUBE 12167
89
90 char       *strtable[STRTABLESIZE];
91 hashnode   *hashtable[HASHTABLESIZE];
92
93 static int      strtable_end = -1;      /* Tells us last occupied string space */
94
95 /*-
96  * Basic information associated with each type.  This is used before
97  * pg_type is created.
98  *
99  *              XXX several of these input/output functions do catalog scans
100  *                      (e.g., F_REGPROCIN scans pg_proc).      this obviously creates some
101  *                      order dependencies in the catalog creation process.
102  */
103 struct typinfo
104 {
105         char            name[NAMEDATALEN];
106         Oid                     oid;
107         Oid                     elem;
108         int16           len;
109         bool            byval;
110         char            align;
111         char            storage;
112         Oid                     inproc;
113         Oid                     outproc;
114 };
115
116 static const struct typinfo TypInfo[] = {
117         {"bool", BOOLOID, 0, 1, true, 'c', 'p',
118         F_BOOLIN, F_BOOLOUT},
119         {"bytea", BYTEAOID, 0, -1, false, 'i', 'x',
120         F_BYTEAIN, F_BYTEAOUT},
121         {"char", CHAROID, 0, 1, true, 'c', 'p',
122         F_CHARIN, F_CHAROUT},
123         {"name", NAMEOID, CHAROID, NAMEDATALEN, false, 'i', 'p',
124         F_NAMEIN, F_NAMEOUT},
125         {"int2", INT2OID, 0, 2, true, 's', 'p',
126         F_INT2IN, F_INT2OUT},
127         {"int4", INT4OID, 0, 4, true, 'i', 'p',
128         F_INT4IN, F_INT4OUT},
129         {"regproc", REGPROCOID, 0, 4, true, 'i', 'p',
130         F_REGPROCIN, F_REGPROCOUT},
131         {"regclass", REGCLASSOID, 0, 4, true, 'i', 'p',
132         F_REGCLASSIN, F_REGCLASSOUT},
133         {"regtype", REGTYPEOID, 0, 4, true, 'i', 'p',
134         F_REGTYPEIN, F_REGTYPEOUT},
135         {"text", TEXTOID, 0, -1, false, 'i', 'x',
136         F_TEXTIN, F_TEXTOUT},
137         {"oid", OIDOID, 0, 4, true, 'i', 'p',
138         F_OIDIN, F_OIDOUT},
139         {"tid", TIDOID, 0, 6, false, 's', 'p',
140         F_TIDIN, F_TIDOUT},
141         {"xid", XIDOID, 0, 4, true, 'i', 'p',
142         F_XIDIN, F_XIDOUT},
143         {"cid", CIDOID, 0, 4, true, 'i', 'p',
144         F_CIDIN, F_CIDOUT},
145         {"int2vector", INT2VECTOROID, INT2OID, -1, false, 'i', 'p',
146         F_INT2VECTORIN, F_INT2VECTOROUT},
147         {"oidvector", OIDVECTOROID, OIDOID, -1, false, 'i', 'p',
148         F_OIDVECTORIN, F_OIDVECTOROUT},
149         {"_int4", INT4ARRAYOID, INT4OID, -1, false, 'i', 'x',
150         F_ARRAY_IN, F_ARRAY_OUT},
151         {"_text", 1009, TEXTOID, -1, false, 'i', 'x',
152         F_ARRAY_IN, F_ARRAY_OUT},
153         {"_oid", 1028, OIDOID, -1, false, 'i', 'x',
154         F_ARRAY_IN, F_ARRAY_OUT},
155         {"_char", 1002, CHAROID, -1, false, 'i', 'x',
156         F_ARRAY_IN, F_ARRAY_OUT},
157         {"_aclitem", 1034, ACLITEMOID, -1, false, 'i', 'x',
158         F_ARRAY_IN, F_ARRAY_OUT}
159 };
160
161 static const int n_types = sizeof(TypInfo) / sizeof(struct typinfo);
162
163 struct typmap
164 {                                                               /* a hack */
165         Oid                     am_oid;
166         FormData_pg_type am_typ;
167 };
168
169 static struct typmap **Typ = NULL;
170 static struct typmap *Ap = NULL;
171
172 static int      Warnings = 0;
173 static char Blanks[MAXATTR];
174
175 Form_pg_attribute attrtypes[MAXATTR];   /* points to attribute info */
176 static Datum values[MAXATTR];   /* corresponding attribute values */
177 int                     numattr;                        /* number of attributes for cur. rel */
178
179 static MemoryContext nogc = NULL;               /* special no-gc mem context */
180
181 /*
182  *      At bootstrap time, we first declare all the indices to be built, and
183  *      then build them.  The IndexList structure stores enough information
184  *      to allow us to build the indices after they've been declared.
185  */
186
187 typedef struct _IndexList
188 {
189         Oid                     il_heap;
190         Oid                     il_ind;
191         IndexInfo  *il_info;
192         struct _IndexList *il_next;
193 } IndexList;
194
195 static IndexList *ILHead = NULL;
196
197
198 /*
199  *       The main entry point for running the backend in bootstrap mode
200  *
201  *       The bootstrap mode is used to initialize the template database.
202  *       The bootstrap backend doesn't speak SQL, but instead expects
203  *       commands in a special bootstrap language.
204  *
205  *       For historical reasons, BootstrapMain is also used as the control
206  *       routine for non-backend subprocesses launched by the postmaster,
207  *       such as startup and shutdown.
208  */
209 int
210 BootstrapMain(int argc, char *argv[])
211 {
212         char       *progname = argv[0];
213         int                     i;
214         char       *dbname;
215         int                     flag;
216         int                     xlogop = BS_XLOG_NOP;
217         char       *userDoption = NULL;
218
219         /*
220          * initialize globals
221          */
222         MyProcPid = getpid();
223
224         /*
225          * Fire up essential subsystems: error and memory management
226          *
227          * If we are running under the postmaster, this is done already.
228          */
229         if (!IsUnderPostmaster)
230                 MemoryContextInit();
231
232         /* Compute paths, if we didn't inherit them from postmaster */
233         if (my_exec_path[0] == '\0')
234         {
235                 if (find_my_exec(progname, my_exec_path) < 0)
236                         elog(FATAL, "%s: could not locate my own executable path",
237                                  progname);
238         }
239
240         /*
241          * process command arguments
242          */
243
244         /* Set defaults, to be overriden by explicit options below */
245         dbname = NULL;
246         if (!IsUnderPostmaster)
247                 InitializeGUCOptions();
248
249         /* Ignore the initial --boot argument, if present */
250         if (argc > 1 && strcmp(argv[1], "--boot") == 0)
251         {
252                 argv++;
253                 argc--;
254         }
255
256         while ((flag = getopt(argc, argv, "B:c:d:D:Fr:x:y:-:")) != -1)
257         {
258                 switch (flag)
259                 {
260                         case 'B':
261                                 SetConfigOption("shared_buffers", optarg, PGC_POSTMASTER, PGC_S_ARGV);
262                                 break;
263                         case 'D':
264                                 userDoption = optarg;
265                                 break;
266                         case 'd':
267                                 {
268                                         /* Turn on debugging for the bootstrap process. */
269                                         char       *debugstr = palloc(strlen("debug") + strlen(optarg) + 1);
270
271                                         sprintf(debugstr, "debug%s", optarg);
272                                         SetConfigOption("log_min_messages", debugstr,
273                                                                         PGC_POSTMASTER, PGC_S_ARGV);
274                                         SetConfigOption("client_min_messages", debugstr,
275                                                                         PGC_POSTMASTER, PGC_S_ARGV);
276                                         pfree(debugstr);
277                                 }
278                                 break;
279                         case 'F':
280                                 SetConfigOption("fsync", "false", PGC_POSTMASTER, PGC_S_ARGV);
281                                 break;
282                         case 'r':
283                                 StrNCpy(OutputFileName, optarg, MAXPGPATH);
284                                 break;
285                         case 'x':
286                                 xlogop = atoi(optarg);
287                                 break;
288                         case 'y':
289                                 dbname = strdup(optarg);
290                                 break;
291                         case 'c':
292                         case '-':
293                                 {
294                                         char       *name,
295                                                            *value;
296
297                                         ParseLongOption(optarg, &name, &value);
298                                         if (!value)
299                                         {
300                                                 if (flag == '-')
301                                                         ereport(ERROR,
302                                                                         (errcode(ERRCODE_SYNTAX_ERROR),
303                                                                          errmsg("--%s requires a value",
304                                                                                         optarg)));
305                                                 else
306                                                         ereport(ERROR,
307                                                                         (errcode(ERRCODE_SYNTAX_ERROR),
308                                                                          errmsg("-c %s requires a value",
309                                                                                         optarg)));
310                                         }
311
312                                         SetConfigOption(name, value, PGC_POSTMASTER, PGC_S_ARGV);
313                                         free(name);
314                                         if (value)
315                                                 free(value);
316                                         break;
317                                 }
318                         default:
319                                 write_stderr("Try \"%s --help\" for more information.\n",
320                                                          progname);
321                                 proc_exit(1);
322                                 break;
323                 }
324         }
325
326         if (!dbname && argc - optind == 1)
327         {
328                 dbname = argv[optind];
329                 optind++;
330         }
331         if (!dbname || argc != optind)
332         {
333                 write_stderr("%s: invalid command-line arguments\n", progname);
334                 proc_exit(1);
335         }
336
337         /*
338          * Identify myself via ps
339          */
340         if (IsUnderPostmaster)
341         {
342                 const char *statmsg;
343
344                 switch (xlogop)
345                 {
346                         case BS_XLOG_STARTUP:
347                                 statmsg = "startup process";
348                                 break;
349                         case BS_XLOG_BGWRITER:
350                                 statmsg = "writer process";
351                                 break;
352                         default:
353                                 statmsg = "??? process";
354                                 break;
355                 }
356                 init_ps_display(statmsg, "", "", "");
357         }
358
359         /* Acquire configuration parameters, unless inherited from postmaster */
360         if (!IsUnderPostmaster)
361         {
362                 if (!SelectConfigFiles(userDoption, progname))
363                         proc_exit(1);
364                 /* If timezone is not set, determine what the OS uses */
365                 pg_timezone_initialize();
366         }
367
368         /* Validate we have been given a reasonable-looking DataDir */
369         Assert(DataDir);
370         ValidatePgVersion(DataDir);
371
372         /* Change into DataDir (if under postmaster, should be done already) */
373         if (!IsUnderPostmaster)
374                 ChangeToDataDir();
375
376         /* If standalone, create lockfile for data directory */
377         if (!IsUnderPostmaster)
378                 CreateDataDirLockFile(false);
379
380         SetProcessingMode(BootstrapProcessing);
381         IgnoreSystemIndexes = true;
382
383         BaseInit();
384
385         /*
386          * When we are a dummy process, we aren't going to do the full
387          * InitPostgres pushups, but there are a couple of things that need
388          * to get lit up even in a dummy process.
389          */
390         if (IsUnderPostmaster)
391         {
392                 /*
393                  * Create a PGPROC so we can use LWLocks.  In the EXEC_BACKEND case,
394                  * this was already done by SubPostmasterMain().
395                  */
396 #ifndef EXEC_BACKEND
397                 InitDummyProcess();
398 #endif
399
400                 /* finish setting up bufmgr.c */
401                 InitBufferPoolBackend();
402
403                 /* register a shutdown callback for LWLock cleanup */
404                 on_shmem_exit(ShutdownDummyProcess, 0);
405         }
406
407         /*
408          * XLOG operations
409          */
410         SetProcessingMode(NormalProcessing);
411
412         switch (xlogop)
413         {
414                 case BS_XLOG_NOP:
415                         bootstrap_signals();
416                         break;
417
418                 case BS_XLOG_BOOTSTRAP:
419                         bootstrap_signals();
420                         BootStrapXLOG();
421                         StartupXLOG();
422                         break;
423
424                 case BS_XLOG_STARTUP:
425                         bootstrap_signals();
426                         StartupXLOG();
427                         /*
428                          * These next two functions don't consider themselves critical,
429                          * but we'd best PANIC anyway if they fail.
430                          */
431                         START_CRIT_SECTION();
432                         LoadFreeSpaceMap();
433                         BuildFlatFiles(false);
434                         END_CRIT_SECTION();
435                         proc_exit(0);           /* startup done */
436
437                 case BS_XLOG_BGWRITER:
438                         /* don't set signals, bgwriter has its own agenda */
439                         InitXLOGAccess();
440                         BackgroundWriterMain();
441                         proc_exit(1);           /* should never return */
442
443                 default:
444                         elog(PANIC, "unrecognized XLOG op: %d", xlogop);
445                         proc_exit(1);
446         }
447
448         /*
449          * We must be getting invoked for bootstrap mode
450          */
451         Assert(!IsUnderPostmaster);
452
453         SetProcessingMode(BootstrapProcessing);
454
455         /*
456          * Do backend-like initialization for bootstrap mode
457          */
458         InitProcess();
459         (void) InitPostgres(dbname, NULL);
460
461         /*
462          * In NOP mode, all we really want to do is create shared memory and
463          * semaphores (just to prove we can do it with the current GUC settings).
464          * So, quit now.
465          */
466         if (xlogop == BS_XLOG_NOP)
467                 proc_exit(0);
468
469         /* Initialize stuff for bootstrap-file processing */
470         for (i = 0; i < MAXATTR; i++)
471         {
472                 attrtypes[i] = NULL;
473                 Blanks[i] = ' ';
474         }
475         for (i = 0; i < STRTABLESIZE; ++i)
476                 strtable[i] = NULL;
477         for (i = 0; i < HASHTABLESIZE; ++i)
478                 hashtable[i] = NULL;
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(true, true);
488         SetProcessingMode(BootstrapProcessing);
489
490         /* Clean up and exit */
491         StartTransactionCommand();
492         cleanup();
493
494         /* not reached, here to make compiler happy */
495         return 0;
496 }
497
498
499 /* ----------------------------------------------------------------
500  *                                              misc functions
501  * ----------------------------------------------------------------
502  */
503
504 /*
505  * Set up signal handling for a bootstrap process
506  */
507 static void
508 bootstrap_signals(void)
509 {
510         if (IsUnderPostmaster)
511         {
512                 /*
513                  * Properly accept or ignore signals the postmaster might send us
514                  */
515                 pqsignal(SIGHUP, SIG_IGN);
516                 pqsignal(SIGINT, SIG_IGN);              /* ignore query-cancel */
517                 pqsignal(SIGTERM, die);
518                 pqsignal(SIGQUIT, quickdie);
519                 pqsignal(SIGALRM, SIG_IGN);
520                 pqsignal(SIGPIPE, SIG_IGN);
521                 pqsignal(SIGUSR1, SIG_IGN);
522                 pqsignal(SIGUSR2, SIG_IGN);
523
524                 /*
525                  * Reset some signals that are accepted by postmaster but not here
526                  */
527                 pqsignal(SIGCHLD, SIG_DFL);
528                 pqsignal(SIGTTIN, SIG_DFL);
529                 pqsignal(SIGTTOU, SIG_DFL);
530                 pqsignal(SIGCONT, SIG_DFL);
531                 pqsignal(SIGWINCH, SIG_DFL);
532
533                 /*
534                  * Unblock signals (they were blocked when the postmaster forked us)
535                  */
536                 PG_SETMASK(&UnBlockSig);
537         }
538         else
539         {
540                 /* Set up appropriately for interactive use */
541                 pqsignal(SIGHUP, die);
542                 pqsignal(SIGINT, die);
543                 pqsignal(SIGTERM, die);
544                 pqsignal(SIGQUIT, die);
545         }
546 }
547
548 /*
549  * Begin shutdown of a dummy process.  This is approximately the equivalent
550  * of ShutdownPostgres() in postinit.c.  We can't run transactions in a
551  * dummy process, so most of the work of AbortTransaction() is not needed,
552  * but we do need to make sure we've released any LWLocks we are holding.
553  * (This is only critical during an error exit.)
554  */
555 static void
556 ShutdownDummyProcess(int code, Datum arg)
557 {
558         LWLockReleaseAll();
559 }
560
561 /* ----------------
562  *              error handling / abort routines
563  * ----------------
564  */
565 void
566 err_out(void)
567 {
568         Warnings++;
569         cleanup();
570 }
571
572
573 /* ----------------------------------------------------------------
574  *                              MANUAL BACKEND INTERACTIVE INTERFACE COMMANDS
575  * ----------------------------------------------------------------
576  */
577
578 /* ----------------
579  *              boot_openrel
580  * ----------------
581  */
582 void
583 boot_openrel(char *relname)
584 {
585         int                     i;
586         struct typmap **app;
587         Relation        rel;
588         HeapScanDesc scan;
589         HeapTuple       tup;
590
591         if (strlen(relname) >= NAMEDATALEN)
592                 relname[NAMEDATALEN - 1] = '\0';
593
594         if (Typ == NULL)
595         {
596                 rel = heap_open(TypeRelationId, NoLock);
597                 scan = heap_beginscan(rel, SnapshotNow, 0, NULL);
598                 i = 0;
599                 while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
600                         ++i;
601                 heap_endscan(scan);
602                 app = Typ = ALLOC(struct typmap *, i + 1);
603                 while (i-- > 0)
604                         *app++ = ALLOC(struct typmap, 1);
605                 *app = NULL;
606                 scan = heap_beginscan(rel, SnapshotNow, 0, NULL);
607                 app = Typ;
608                 while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
609                 {
610                         (*app)->am_oid = HeapTupleGetOid(tup);
611                         memcpy((char *) &(*app)->am_typ,
612                                    (char *) GETSTRUCT(tup),
613                                    sizeof((*app)->am_typ));
614                         app++;
615                 }
616                 heap_endscan(scan);
617                 heap_close(rel, NoLock);
618         }
619
620         if (boot_reldesc != NULL)
621                 closerel(NULL);
622
623         elog(DEBUG4, "open relation %s, attrsize %d",
624                  relname, (int) ATTRIBUTE_TUPLE_SIZE);
625
626         boot_reldesc = heap_openrv(makeRangeVar(NULL, relname), NoLock);
627         numattr = boot_reldesc->rd_rel->relnatts;
628         for (i = 0; i < numattr; i++)
629         {
630                 if (attrtypes[i] == NULL)
631                         attrtypes[i] = AllocateAttribute();
632                 memmove((char *) attrtypes[i],
633                                 (char *) boot_reldesc->rd_att->attrs[i],
634                                 ATTRIBUTE_TUPLE_SIZE);
635
636                 {
637                         Form_pg_attribute at = attrtypes[i];
638
639                         elog(DEBUG4, "create attribute %d name %s len %d num %d type %u",
640                                  i, NameStr(at->attname), at->attlen, at->attnum,
641                                  at->atttypid);
642                 }
643         }
644 }
645
646 /* ----------------
647  *              closerel
648  * ----------------
649  */
650 void
651 closerel(char *name)
652 {
653         if (name)
654         {
655                 if (boot_reldesc)
656                 {
657                         if (strcmp(RelationGetRelationName(boot_reldesc), name) != 0)
658                                 elog(ERROR, "close of %s when %s was expected",
659                                          name, RelationGetRelationName(boot_reldesc));
660                 }
661                 else
662                         elog(ERROR, "close of %s before any relation was opened",
663                                  name);
664         }
665
666         if (boot_reldesc == NULL)
667                 elog(ERROR, "no open relation to close");
668         else
669         {
670                 elog(DEBUG4, "close relation %s",
671                          RelationGetRelationName(boot_reldesc));
672                 heap_close(boot_reldesc, NoLock);
673                 boot_reldesc = NULL;
674         }
675 }
676
677
678
679 /* ----------------
680  * DEFINEATTR()
681  *
682  * define a <field,type> pair
683  * if there are n fields in a relation to be created, this routine
684  * will be called n times
685  * ----------------
686  */
687 void
688 DefineAttr(char *name, char *type, int attnum)
689 {
690         Oid                     typeoid;
691
692         if (boot_reldesc != NULL)
693         {
694                 elog(WARNING, "no open relations allowed with CREATE command");
695                 closerel(NULL);
696         }
697
698         if (attrtypes[attnum] == NULL)
699                 attrtypes[attnum] = AllocateAttribute();
700         MemSet(attrtypes[attnum], 0, ATTRIBUTE_TUPLE_SIZE);
701
702         namestrcpy(&attrtypes[attnum]->attname, name);
703         elog(DEBUG4, "column %s %s", NameStr(attrtypes[attnum]->attname), type);
704         attrtypes[attnum]->attnum = attnum + 1;         /* fillatt */
705
706         typeoid = gettype(type);
707
708         if (Typ != NULL)
709         {
710                 attrtypes[attnum]->atttypid = Ap->am_oid;
711                 attrtypes[attnum]->attlen = Ap->am_typ.typlen;
712                 attrtypes[attnum]->attbyval = Ap->am_typ.typbyval;
713                 attrtypes[attnum]->attstorage = Ap->am_typ.typstorage;
714                 attrtypes[attnum]->attalign = Ap->am_typ.typalign;
715                 /* if an array type, assume 1-dimensional attribute */
716                 if (Ap->am_typ.typelem != InvalidOid && Ap->am_typ.typlen < 0)
717                         attrtypes[attnum]->attndims = 1;
718                 else
719                         attrtypes[attnum]->attndims = 0;
720         }
721         else
722         {
723                 attrtypes[attnum]->atttypid = TypInfo[typeoid].oid;
724                 attrtypes[attnum]->attlen = TypInfo[typeoid].len;
725                 attrtypes[attnum]->attbyval = TypInfo[typeoid].byval;
726                 attrtypes[attnum]->attstorage = TypInfo[typeoid].storage;
727                 attrtypes[attnum]->attalign = TypInfo[typeoid].align;
728                 /* if an array type, assume 1-dimensional attribute */
729                 if (TypInfo[typeoid].elem != InvalidOid &&
730                         attrtypes[attnum]->attlen < 0)
731                         attrtypes[attnum]->attndims = 1;
732                 else
733                         attrtypes[attnum]->attndims = 0;
734         }
735
736         attrtypes[attnum]->attstattarget = -1;
737         attrtypes[attnum]->attcacheoff = -1;
738         attrtypes[attnum]->atttypmod = -1;
739         attrtypes[attnum]->attislocal = true;
740
741         /*
742          * Mark as "not null" if type is fixed-width and prior columns are too.
743          * This corresponds to case where column can be accessed directly via C
744          * struct declaration.
745          *
746          * oidvector and int2vector are also treated as not-nullable, even though
747          * they are no longer fixed-width.
748          */
749 #define MARKNOTNULL(att) \
750         ((att)->attlen > 0 || \
751          (att)->atttypid == OIDVECTOROID || \
752          (att)->atttypid == INT2VECTOROID)
753
754         if (MARKNOTNULL(attrtypes[attnum]))
755         {
756                 int                     i;
757
758                 for (i = 0; i < attnum; i++)
759                 {
760                         if (!MARKNOTNULL(attrtypes[i]))
761                                 break;
762                 }
763                 if (i == attnum)
764                         attrtypes[attnum]->attnotnull = true;
765         }
766 }
767
768
769 /* ----------------
770  *              InsertOneTuple
771  *
772  * If objectid is not zero, it is a specific OID to assign to the tuple.
773  * Otherwise, an OID will be assigned (if necessary) by heap_insert.
774  * ----------------
775  */
776 void
777 InsertOneTuple(Oid objectid)
778 {
779         HeapTuple       tuple;
780         TupleDesc       tupDesc;
781         int                     i;
782
783         elog(DEBUG4, "inserting row oid %u, %d columns", objectid, numattr);
784
785         tupDesc = CreateTupleDesc(numattr,
786                                                           RelationGetForm(boot_reldesc)->relhasoids,
787                                                           attrtypes);
788         tuple = heap_formtuple(tupDesc, values, Blanks);
789         if (objectid != (Oid) 0)
790                 HeapTupleSetOid(tuple, objectid);
791         pfree(tupDesc);                         /* just free's tupDesc, not the attrtypes */
792
793         simple_heap_insert(boot_reldesc, tuple);
794         heap_freetuple(tuple);
795         elog(DEBUG4, "row inserted");
796
797         /*
798          * Reset blanks for next tuple
799          */
800         for (i = 0; i < numattr; i++)
801                 Blanks[i] = ' ';
802 }
803
804 /* ----------------
805  *              InsertOneValue
806  * ----------------
807  */
808 void
809 InsertOneValue(char *value, int i)
810 {
811         Oid                     typoid;
812         Oid                     typioparam;
813         Oid                     typinput;
814         Oid                     typoutput;
815         char       *prt;
816
817         AssertArg(i >= 0 || i < MAXATTR);
818
819         elog(DEBUG4, "inserting column %d value \"%s\"", i, value);
820
821         if (Typ != NULL)
822         {
823                 struct typmap **app;
824                 struct typmap *ap;
825
826                 elog(DEBUG5, "Typ != NULL");
827                 typoid = boot_reldesc->rd_att->attrs[i]->atttypid;
828                 app = Typ;
829                 while (*app && (*app)->am_oid != typoid)
830                         ++app;
831                 ap = *app;
832                 if (ap == NULL)
833                         elog(ERROR, "could not find atttypid %u in Typ list", typoid);
834
835                 /* XXX this logic should match getTypeIOParam() */
836                 if (OidIsValid(ap->am_typ.typelem))
837                         typioparam = ap->am_typ.typelem;
838                 else
839                         typioparam = typoid;
840
841                 typinput = ap->am_typ.typinput;
842                 typoutput = ap->am_typ.typoutput;
843         }
844         else
845         {
846                 int                     typeindex;
847
848                 /* XXX why is typoid determined differently in this path? */
849                 typoid = attrtypes[i]->atttypid;
850                 for (typeindex = 0; typeindex < n_types; typeindex++)
851                 {
852                         if (TypInfo[typeindex].oid == typoid)
853                                 break;
854                 }
855                 if (typeindex >= n_types)
856                         elog(ERROR, "type oid %u not found", typoid);
857                 elog(DEBUG5, "Typ == NULL, typeindex = %u", typeindex);
858
859                 /* XXX this logic should match getTypeIOParam() */
860                 if (OidIsValid(TypInfo[typeindex].elem))
861                         typioparam = TypInfo[typeindex].elem;
862                 else
863                         typioparam = typoid;
864
865                 typinput = TypInfo[typeindex].inproc;
866                 typoutput = TypInfo[typeindex].outproc;
867         }
868
869         values[i] = OidInputFunctionCall(typinput, value, typioparam, -1);
870         prt = OidOutputFunctionCall(typoutput, values[i]);
871         elog(DEBUG4, "inserted -> %s", prt);
872         pfree(prt);
873 }
874
875 /* ----------------
876  *              InsertOneNull
877  * ----------------
878  */
879 void
880 InsertOneNull(int i)
881 {
882         elog(DEBUG4, "inserting column %d NULL", i);
883         Assert(i >= 0 || i < MAXATTR);
884         values[i] = PointerGetDatum(NULL);
885         Blanks[i] = 'n';
886 }
887
888 /* ----------------
889  *              cleanup
890  * ----------------
891  */
892 static void
893 cleanup(void)
894 {
895         static int      beenhere = 0;
896
897         if (!beenhere)
898                 beenhere = 1;
899         else
900         {
901                 elog(FATAL, "cleanup called twice");
902                 proc_exit(1);
903         }
904         if (boot_reldesc != NULL)
905                 closerel(NULL);
906         CommitTransactionCommand();
907         proc_exit(Warnings ? 1 : 0);
908 }
909
910 /* ----------------
911  *              gettype
912  *
913  * NB: this is really ugly; it will return an integer index into TypInfo[],
914  * and not an OID at all, until the first reference to a type not known in
915  * TypInfo[].  At that point it will read and cache pg_type in the Typ array,
916  * and subsequently return a real OID (and set the global pointer Ap to
917  * point at the found row in Typ).      So caller must check whether Typ is
918  * still NULL to determine what the return value is!
919  * ----------------
920  */
921 static Oid
922 gettype(char *type)
923 {
924         int                     i;
925         Relation        rel;
926         HeapScanDesc scan;
927         HeapTuple       tup;
928         struct typmap **app;
929
930         if (Typ != NULL)
931         {
932                 for (app = Typ; *app != NULL; app++)
933                 {
934                         if (strncmp(NameStr((*app)->am_typ.typname), type, NAMEDATALEN) == 0)
935                         {
936                                 Ap = *app;
937                                 return (*app)->am_oid;
938                         }
939                 }
940         }
941         else
942         {
943                 for (i = 0; i < n_types; i++)
944                 {
945                         if (strncmp(type, TypInfo[i].name, NAMEDATALEN) == 0)
946                                 return i;
947                 }
948                 elog(DEBUG4, "external type: %s", type);
949                 rel = heap_open(TypeRelationId, NoLock);
950                 scan = heap_beginscan(rel, SnapshotNow, 0, NULL);
951                 i = 0;
952                 while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
953                         ++i;
954                 heap_endscan(scan);
955                 app = Typ = ALLOC(struct typmap *, i + 1);
956                 while (i-- > 0)
957                         *app++ = ALLOC(struct typmap, 1);
958                 *app = NULL;
959                 scan = heap_beginscan(rel, SnapshotNow, 0, NULL);
960                 app = Typ;
961                 while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
962                 {
963                         (*app)->am_oid = HeapTupleGetOid(tup);
964                         memmove((char *) &(*app++)->am_typ,
965                                         (char *) GETSTRUCT(tup),
966                                         sizeof((*app)->am_typ));
967                 }
968                 heap_endscan(scan);
969                 heap_close(rel, NoLock);
970                 return gettype(type);
971         }
972         elog(ERROR, "unrecognized type \"%s\"", type);
973         err_out();
974         /* not reached, here to make compiler happy */
975         return 0;
976 }
977
978 /* ----------------
979  *              AllocateAttribute
980  * ----------------
981  */
982 static Form_pg_attribute
983 AllocateAttribute(void)
984 {
985         Form_pg_attribute attribute = (Form_pg_attribute) malloc(ATTRIBUTE_TUPLE_SIZE);
986
987         if (!PointerIsValid(attribute))
988                 elog(FATAL, "out of memory");
989         MemSet(attribute, 0, ATTRIBUTE_TUPLE_SIZE);
990
991         return attribute;
992 }
993
994 /* ----------------
995  *              MapArrayTypeName
996  * XXX arrays of "basetype" are always "_basetype".
997  *         this is an evil hack inherited from rel. 3.1.
998  * XXX array dimension is thrown away because we
999  *         don't support fixed-dimension arrays.  again,
1000  *         sickness from 3.1.
1001  *
1002  * the string passed in must have a '[' character in it
1003  *
1004  * the string returned is a pointer to static storage and should NOT
1005  * be freed by the CALLER.
1006  * ----------------
1007  */
1008 char *
1009 MapArrayTypeName(char *s)
1010 {
1011         int                     i,
1012                                 j;
1013         static char newStr[NAMEDATALEN];        /* array type names < NAMEDATALEN long */
1014
1015         if (s == NULL || s[0] == '\0')
1016                 return s;
1017
1018         j = 1;
1019         newStr[0] = '_';
1020         for (i = 0; i < NAMEDATALEN - 1 && s[i] != '['; i++, j++)
1021                 newStr[j] = s[i];
1022
1023         newStr[j] = '\0';
1024
1025         return newStr;
1026 }
1027
1028 /* ----------------
1029  *              EnterString
1030  *              returns the string table position of the identifier
1031  *              passed to it.  We add it to the table if we can't find it.
1032  * ----------------
1033  */
1034 int
1035 EnterString(char *str)
1036 {
1037         hashnode   *node;
1038         int                     len;
1039
1040         len = strlen(str);
1041
1042         node = FindStr(str, len, NULL);
1043         if (node)
1044                 return node->strnum;
1045         else
1046         {
1047                 node = AddStr(str, len, 0);
1048                 return node->strnum;
1049         }
1050 }
1051
1052 /* ----------------
1053  *              LexIDStr
1054  *              when given an idnum into the 'string-table' return the string
1055  *              associated with the idnum
1056  * ----------------
1057  */
1058 char *
1059 LexIDStr(int ident_num)
1060 {
1061         return strtable[ident_num];
1062 }
1063
1064
1065 /* ----------------
1066  *              CompHash
1067  *
1068  *              Compute a hash function for a given string.  We look at the first,
1069  *              the last, and the middle character of a string to try to get spread
1070  *              the strings out.  The function is rather arbitrary, except that we
1071  *              are mod'ing by a prime number.
1072  * ----------------
1073  */
1074 static int
1075 CompHash(char *str, int len)
1076 {
1077         int                     result;
1078
1079         result = (NUM * str[0] + NUMSQR * str[len - 1] + NUMCUBE * str[(len - 1) / 2]);
1080
1081         return result % HASHTABLESIZE;
1082
1083 }
1084
1085 /* ----------------
1086  *              FindStr
1087  *
1088  *              This routine looks for the specified string in the hash
1089  *              table.  It returns a pointer to the hash node found,
1090  *              or NULL if the string is not in the table.
1091  * ----------------
1092  */
1093 static hashnode *
1094 FindStr(char *str, int length, hashnode *mderef)
1095 {
1096         hashnode   *node;
1097
1098         node = hashtable[CompHash(str, length)];
1099         while (node != NULL)
1100         {
1101                 /*
1102                  * We must differentiate between string constants that might have the
1103                  * same value as a identifier and the identifier itself.
1104                  */
1105                 if (!strcmp(str, strtable[node->strnum]))
1106                 {
1107                         return node;            /* no need to check */
1108                 }
1109                 else
1110                         node = node->next;
1111         }
1112         /* Couldn't find it in the list */
1113         return NULL;
1114 }
1115
1116 /* ----------------
1117  *              AddStr
1118  *
1119  *              This function adds the specified string, along with its associated
1120  *              data, to the hash table and the string table.  We return the node
1121  *              so that the calling routine can find out the unique id that AddStr
1122  *              has assigned to this string.
1123  * ----------------
1124  */
1125 static hashnode *
1126 AddStr(char *str, int strlength, int mderef)
1127 {
1128         hashnode   *temp,
1129                            *trail,
1130                            *newnode;
1131         int                     hashresult;
1132         int                     len;
1133
1134         if (++strtable_end >= STRTABLESIZE)
1135                 elog(FATAL, "bootstrap string table overflow");
1136
1137         /*
1138          * Some of the utilites (eg, define type, create relation) assume that the
1139          * string they're passed is a NAMEDATALEN.  We get array bound read
1140          * violations from purify if we don't allocate at least NAMEDATALEN bytes
1141          * for strings of this sort.  Because we're lazy, we allocate at least
1142          * NAMEDATALEN bytes all the time.
1143          */
1144
1145         if ((len = strlength + 1) < NAMEDATALEN)
1146                 len = NAMEDATALEN;
1147
1148         strtable[strtable_end] = malloc((unsigned) len);
1149         strcpy(strtable[strtable_end], str);
1150
1151         /* Now put a node in the hash table */
1152
1153         newnode = (hashnode *) malloc(sizeof(hashnode) * 1);
1154         newnode->strnum = strtable_end;
1155         newnode->next = NULL;
1156
1157         /* Find out where it goes */
1158
1159         hashresult = CompHash(str, strlength);
1160         if (hashtable[hashresult] == NULL)
1161                 hashtable[hashresult] = newnode;
1162         else
1163         {                                                       /* There is something in the list */
1164                 trail = hashtable[hashresult];
1165                 temp = trail->next;
1166                 while (temp != NULL)
1167                 {
1168                         trail = temp;
1169                         temp = temp->next;
1170                 }
1171                 trail->next = newnode;
1172         }
1173         return newnode;
1174 }
1175
1176
1177
1178 /*
1179  *      index_register() -- record an index that has been set up for building
1180  *                                              later.
1181  *
1182  *              At bootstrap time, we define a bunch of indexes on system catalogs.
1183  *              We postpone actually building the indexes until just before we're
1184  *              finished with initialization, however.  This is because the indexes
1185  *              themselves have catalog entries, and those have to be included in the
1186  *              indexes on those catalogs.  Doing it in two phases is the simplest
1187  *              way of making sure the indexes have the right contents at the end.
1188  */
1189 void
1190 index_register(Oid heap,
1191                            Oid ind,
1192                            IndexInfo *indexInfo)
1193 {
1194         IndexList  *newind;
1195         MemoryContext oldcxt;
1196
1197         /*
1198          * XXX mao 10/31/92 -- don't gc index reldescs, associated info at
1199          * bootstrap time.      we'll declare the indexes now, but want to create them
1200          * later.
1201          */
1202
1203         if (nogc == NULL)
1204                 nogc = AllocSetContextCreate(NULL,
1205                                                                          "BootstrapNoGC",
1206                                                                          ALLOCSET_DEFAULT_MINSIZE,
1207                                                                          ALLOCSET_DEFAULT_INITSIZE,
1208                                                                          ALLOCSET_DEFAULT_MAXSIZE);
1209
1210         oldcxt = MemoryContextSwitchTo(nogc);
1211
1212         newind = (IndexList *) palloc(sizeof(IndexList));
1213         newind->il_heap = heap;
1214         newind->il_ind = ind;
1215         newind->il_info = (IndexInfo *) palloc(sizeof(IndexInfo));
1216
1217         memcpy(newind->il_info, indexInfo, sizeof(IndexInfo));
1218         /* expressions will likely be null, but may as well copy it */
1219         newind->il_info->ii_Expressions = (List *)
1220                 copyObject(indexInfo->ii_Expressions);
1221         newind->il_info->ii_ExpressionsState = NIL;
1222         /* predicate will likely be null, but may as well copy it */
1223         newind->il_info->ii_Predicate = (List *)
1224                 copyObject(indexInfo->ii_Predicate);
1225         newind->il_info->ii_PredicateState = NIL;
1226
1227         newind->il_next = ILHead;
1228         ILHead = newind;
1229
1230         MemoryContextSwitchTo(oldcxt);
1231 }
1232
1233
1234 /*
1235  * build_indices -- fill in all the indexes registered earlier
1236  */
1237 void
1238 build_indices(void)
1239 {
1240         for (; ILHead != NULL; ILHead = ILHead->il_next)
1241         {
1242                 Relation        heap;
1243                 Relation        ind;
1244
1245                 heap = heap_open(ILHead->il_heap, NoLock);
1246                 ind = index_open(ILHead->il_ind);
1247
1248                 index_build(heap, ind, ILHead->il_info, false, false);
1249
1250                 index_close(ind);
1251                 heap_close(heap, NoLock);
1252         }
1253 }