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