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