1 /*-------------------------------------------------------------------------
4 * routines to support running postgres in 'bootstrap' mode
5 * bootstrap mode is used to create the initial template database
7 * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
11 * $Header: /cvsroot/pgsql/src/backend/bootstrap/bootstrap.c,v 1.160 2003/05/28 18:19:09 tgl Exp $
13 *-------------------------------------------------------------------------
25 #define BOOTSTRAP_INCLUDE /* mask out stuff in tcop/tcopprot.h */
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"
49 #define ALLOC(t, c) ((t *) calloc((unsigned)(c), sizeof(t)))
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);
66 Relation boot_reldesc; /* current relation descriptor */
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.
78 #define STRTABLESIZE 10000
79 #define HASHTABLESIZE 503
81 /* Hash function numbers */
86 char *strtable[STRTABLESIZE];
87 hashnode *hashtable[HASHTABLESIZE];
89 static int strtable_end = -1; /* Tells us last occupied string space */
92 * Basic information associated with each type. This is used before
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.
101 char name[NAMEDATALEN];
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}
131 static int n_types = sizeof(Procid) / sizeof(struct typinfo);
136 FormData_pg_type am_typ;
139 static struct typmap **Typ = (struct typmap **) NULL;
140 static struct typmap *Ap = (struct typmap *) NULL;
142 static int Warnings = 0;
143 static char Blanks[MAXATTR];
145 static char *relname; /* current relation name */
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 */
151 static MemoryContext nogc = NULL; /* special no-gc mem context */
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.
162 typedef struct _IndexList
167 struct _IndexList *il_next;
170 static IndexList *ILHead = (IndexList *) NULL;
173 /* ----------------------------------------------------------------
175 * ----------------------------------------------------------------
179 * error handling / abort routines
190 usage help for the bootstrap backen
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"));
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.
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 * ----------------------------------------------------------------
226 int xlogop = BS_XLOG_NOP;
227 char *potential_DataDir = NULL;
233 MyProcPid = getpid();
236 * Fire up essential subsystems: error and memory management
238 * If we are running under the postmaster, this is done already.
240 if (!IsUnderPostmaster /* when exec || ExecBackend */)
244 * process command arguments
247 /* Set defaults, to be overriden by explicit options below */
249 if (!IsUnderPostmaster /* when exec || ExecBackend*/)
251 InitializeGUCOptions();
252 potential_DataDir = getenv("PGDATA"); /* Null if no PGDATA
256 while ((flag = getopt(argc, argv, "B:d:D:Fo:p:x:")) != -1)
261 potential_DataDir = optarg;
265 /* Turn on debugging for the bootstrap process. */
266 char *debugstr = palloc(strlen("debug") + strlen(optarg) + 1);
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);
278 SetConfigOption("fsync", "false", PGC_POSTMASTER, PGC_S_ARGV);
281 StrNCpy(OutputFileName, optarg, MAXPGPATH);
284 xlogop = atoi(optarg);
288 /* indicates fork from postmaster */
292 sscanf(optarg, "%d,%p,", &UsedShmemSegID, &UsedShmemSegAddr);
293 p = strchr(optarg, ',');
295 p = strchr(p+1, ',');
297 dbname = strdup(p+1);
299 dbname = strdup(optarg);
304 SetConfigOption("shared_buffers", optarg, PGC_POSTMASTER, PGC_S_ARGV);
312 if (!dbname && argc - optind == 1)
314 dbname = argv[optind];
317 if (!dbname || argc != optind)
321 if (IsUnderPostmaster && ExecBackend && MyProc /* ordinary backend */)
323 AttachSharedMemoryAndSemaphores();
326 if (!IsUnderPostmaster /* when exec || ExecBackend*/)
328 if (!potential_DataDir)
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"),
338 SetDataDir(potential_DataDir);
341 /* Validate we have been given a reasonable-looking DataDir */
343 ValidatePgVersion(DataDir);
346 read_nondefault_variables();
349 if (IsUnderPostmaster)
352 * Properly accept or ignore signals the postmaster might send us
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);
364 * Reset some signals that are accepted by postmaster but not here
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);
373 * Unblock signals (they were blocked when the postmaster forked
376 PG_SETMASK(&UnBlockSig);
380 /* Set up appropriately for interactive use */
381 pqsignal(SIGHUP, die);
382 pqsignal(SIGINT, die);
383 pqsignal(SIGTERM, die);
384 pqsignal(SIGQUIT, die);
387 * Create lockfile for data directory.
389 if (!CreateDataDirLockFile(DataDir, false))
393 SetProcessingMode(BootstrapProcessing);
394 IgnoreSystemIndexes(true);
400 if (IsUnderPostmaster)
401 InitDummyProcess(); /* needed to get LWLocks */
406 SetProcessingMode(NormalProcessing);
414 case BS_XLOG_BOOTSTRAP:
419 case BS_XLOG_CHECKPOINT:
421 CreateCheckPoint(false, false);
422 SetSavedRedoRecPtr(); /* pass redo ptr back to
424 proc_exit(0); /* done */
426 case BS_XLOG_STARTUP:
429 proc_exit(0); /* done */
431 case BS_XLOG_SHUTDOWN:
434 proc_exit(0); /* done */
437 elog(PANIC, "Unsupported XLOG op %d", xlogop);
441 SetProcessingMode(BootstrapProcessing);
444 * backend initialization
446 InitPostgres(dbname, NULL);
448 for (i = 0; i < MAXATTR; i++)
450 attrtypes[i] = (Form_pg_attribute) NULL;
453 for (i = 0; i < STRTABLESIZE; ++i)
455 for (i = 0; i < HASHTABLESIZE; ++i)
459 * abort processing resumes here
461 if (sigsetjmp(Warn_restart, 1) != 0)
464 AbortCurrentTransaction();
472 * the sed script boot.sed renamed yyparse to Int_yyparse for the
473 * bootstrap parser to avoid conflicts with the normal SQL parser
477 SetProcessingMode(NormalProcessing);
478 CreateCheckPoint(true, true);
479 SetProcessingMode(BootstrapProcessing);
481 /* clean up processing */
482 StartTransactionCommand();
485 /* not reached, here to make compiler happy */
490 /* ----------------------------------------------------------------
491 * MANUAL BACKEND INTERACTIVE INTERFACE COMMANDS
492 * ----------------------------------------------------------------
500 boot_openrel(char *relname)
508 if (strlen(relname) >= NAMEDATALEN - 1)
509 relname[NAMEDATALEN - 1] = '\0';
511 if (Typ == (struct typmap **) NULL)
513 rel = heap_openr(TypeRelationName, NoLock);
514 scan = heap_beginscan(rel, SnapshotNow, 0, (ScanKey) NULL);
516 while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
519 app = Typ = ALLOC(struct typmap *, i + 1);
521 *app++ = ALLOC(struct typmap, 1);
522 *app = (struct typmap *) NULL;
523 scan = heap_beginscan(rel, SnapshotNow, 0, (ScanKey) NULL);
525 while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
527 (*app)->am_oid = HeapTupleGetOid(tup);
528 memcpy((char *) &(*app)->am_typ,
529 (char *) GETSTRUCT(tup),
530 sizeof((*app)->am_typ));
534 heap_close(rel, NoLock);
537 if (boot_reldesc != NULL)
540 elog(DEBUG4, "open relation %s, attrsize %d", relname ? relname : "(null)",
541 (int) ATTRIBUTE_TUPLE_SIZE);
543 boot_reldesc = heap_openr(relname, NoLock);
544 numattr = boot_reldesc->rd_rel->relnatts;
545 for (i = 0; i < numattr; i++)
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);
554 Form_pg_attribute at = attrtypes[i];
556 elog(DEBUG4, "create attribute %d name %s len %d num %d type %u",
557 i, NameStr(at->attname), at->attlen, at->attnum,
574 if (strcmp(RelationGetRelationName(boot_reldesc), name) != 0)
575 elog(ERROR, "closerel: close of '%s' when '%s' was expected",
576 name, relname ? relname : "(null)");
579 elog(ERROR, "closerel: close of '%s' before any relation was opened",
583 if (boot_reldesc == NULL)
584 elog(ERROR, "no open relation to close");
587 elog(DEBUG4, "close relation %s", relname ? relname : "(null)");
588 heap_close(boot_reldesc, NoLock);
589 boot_reldesc = (Relation) NULL;
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
604 DefineAttr(char *name, char *type, int attnum)
609 if (boot_reldesc != NULL)
611 elog(LOG, "warning: no open relations allowed with 'create' command");
615 if (attrtypes[attnum] == (Form_pg_attribute) NULL)
616 attrtypes[attnum] = AllocateAttribute();
617 MemSet(attrtypes[attnum], 0, ATTRIBUTE_TUPLE_SIZE);
619 namestrcpy(&attrtypes[attnum]->attname, name);
620 elog(DEBUG4, "column %s %s", NameStr(attrtypes[attnum]->attname), type);
621 attrtypes[attnum]->attnum = attnum + 1; /* fillatt */
623 typeoid = gettype(type);
625 if (Typ != (struct typmap **) NULL)
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;
635 attrtypes[attnum]->atttypid = Procid[typeoid].oid;
636 attlen = attrtypes[attnum]->attlen = Procid[typeoid].len;
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[].
645 attrtypes[attnum]->attbyval = true;
646 attrtypes[attnum]->attstorage = 'p';
647 attrtypes[attnum]->attalign = 'c';
650 attrtypes[attnum]->attbyval = true;
651 attrtypes[attnum]->attstorage = 'p';
652 attrtypes[attnum]->attalign = 's';
655 attrtypes[attnum]->attbyval = true;
656 attrtypes[attnum]->attstorage = 'p';
657 attrtypes[attnum]->attalign = 'i';
660 attrtypes[attnum]->attbyval = false;
661 attrtypes[attnum]->attstorage = 'x';
662 attrtypes[attnum]->attalign = 'i';
665 /* TID and fixed-length arrays, such as oidvector */
666 attrtypes[attnum]->attbyval = false;
667 attrtypes[attnum]->attstorage = 'p';
668 attrtypes[attnum]->attalign = 'i';
672 attrtypes[attnum]->attcacheoff = -1;
673 attrtypes[attnum]->atttypmod = -1;
674 attrtypes[attnum]->attislocal = true;
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.
685 for (i = 0; i < attnum; i++)
687 if (attrtypes[i]->attlen <= 0)
691 attrtypes[attnum]->attnotnull = true;
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.
704 InsertOneTuple(Oid objectid)
710 elog(DEBUG4, "inserting row oid %u, %d columns", objectid, numattr);
712 tupDesc = CreateTupleDesc(numattr,
713 RelationGetForm(boot_reldesc)->relhasoids,
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 */
720 simple_heap_insert(boot_reldesc, tuple);
721 heap_freetuple(tuple);
722 elog(DEBUG4, "row inserted");
725 * Reset blanks for next tuple
727 for (i = 0; i < numattr; i++)
736 InsertOneValue(char *value, int i)
742 AssertArg(i >= 0 || i < MAXATTR);
744 elog(DEBUG4, "inserting column %d value '%s'", i, value);
746 if (Typ != (struct typmap **) NULL)
750 elog(DEBUG4, "Typ != NULL");
752 while (*app && (*app)->am_oid != boot_reldesc->rd_att->attrs[i]->atttypid)
757 elog(FATAL, "unable to find atttypid %u in Typ list",
758 boot_reldesc->rd_att->attrs[i]->atttypid);
760 values[i] = OidFunctionCall3(ap->am_typ.typinput,
761 CStringGetDatum(value),
762 ObjectIdGetDatum(ap->am_typ.typelem),
764 prt = DatumGetCString(OidFunctionCall3(ap->am_typ.typoutput,
766 ObjectIdGetDatum(ap->am_typ.typelem),
768 elog(DEBUG4, " -> %s", prt);
773 for (typeindex = 0; typeindex < n_types; typeindex++)
775 if (Procid[typeindex].oid == attrtypes[i]->atttypid)
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),
785 prt = DatumGetCString(OidFunctionCall3(Procid[typeindex].outproc,
787 ObjectIdGetDatum(Procid[typeindex].elem),
789 elog(DEBUG4, " -> %s", prt);
792 elog(DEBUG4, "inserted");
802 elog(DEBUG4, "inserting column %d NULL", i);
803 Assert(i >= 0 || i < MAXATTR);
804 values[i] = PointerGetDatum(NULL);
808 #define MORE_THAN_THE_NUMBER_OF_CATALOGS 256
811 BootstrapAlreadySeen(Oid id)
813 static Oid seenArray[MORE_THAN_THE_NUMBER_OF_CATALOGS];
814 static int nseen = 0;
820 for (i = 0; i < nseen; i++)
822 if (seenArray[i] == id)
830 seenArray[nseen] = id;
843 static int beenhere = 0;
849 elog(FATAL, "Memory manager fault: cleanup called twice");
852 if (boot_reldesc != NULL)
854 CommitTransactionCommand();
855 proc_exit(Warnings ? 1 : 0);
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!
878 if (Typ != (struct typmap **) NULL)
880 for (app = Typ; *app != (struct typmap *) NULL; app++)
882 if (strncmp(NameStr((*app)->am_typ.typname), type, NAMEDATALEN) == 0)
885 return (*app)->am_oid;
891 for (i = 0; i < n_types; i++)
893 if (strncmp(type, Procid[i].name, NAMEDATALEN) == 0)
896 elog(DEBUG4, "external type: %s", type);
897 rel = heap_openr(TypeRelationName, NoLock);
898 scan = heap_beginscan(rel, SnapshotNow, 0, (ScanKey) NULL);
900 while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
903 app = Typ = ALLOC(struct typmap *, i + 1);
905 *app++ = ALLOC(struct typmap, 1);
906 *app = (struct typmap *) NULL;
907 scan = heap_beginscan(rel, SnapshotNow, 0, (ScanKey) NULL);
909 while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
911 (*app)->am_oid = HeapTupleGetOid(tup);
912 memmove((char *) &(*app++)->am_typ,
913 (char *) GETSTRUCT(tup),
914 sizeof((*app)->am_typ));
917 heap_close(rel, NoLock);
918 return gettype(type);
920 elog(ERROR, "Error: unknown type '%s'.\n", type);
922 /* not reached, here to make compiler happy */
930 static Form_pg_attribute
931 AllocateAttribute(void)
933 Form_pg_attribute attribute = (Form_pg_attribute) malloc(ATTRIBUTE_TUPLE_SIZE);
935 if (!PointerIsValid(attribute))
936 elog(FATAL, "AllocateAttribute: malloc failed");
937 MemSet(attribute, 0, ATTRIBUTE_TUPLE_SIZE);
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,
950 * the string passed in must have a '[' character in it
952 * the string returned is a pointer to static storage and should NOT
953 * be freed by the CALLER.
957 MapArrayTypeName(char *s)
961 static char newStr[NAMEDATALEN]; /* array type names < NAMEDATALEN
964 if (s == NULL || s[0] == '\0')
969 for (i = 0; i < NAMEDATALEN - 1 && s[i] != '['; i++, j++)
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.
984 EnterString(char *str)
991 node = FindStr(str, len, 0);
996 node = AddStr(str, len, 0);
1003 * when given an idnum into the 'string-table' return the string
1004 * associated with the idnum
1008 LexIDStr(int ident_num)
1010 return strtable[ident_num];
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.
1024 CompHash(char *str, int len)
1028 result = (NUM * str[0] + NUMSQR * str[len - 1] + NUMCUBE * str[(len - 1) / 2]);
1030 return result % HASHTABLESIZE;
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.
1043 FindStr(char *str, int length, hashnode *mderef)
1047 node = hashtable[CompHash(str, length)];
1048 while (node != NULL)
1051 * We must differentiate between string constants that might have
1052 * the same value as a identifier and the identifier itself.
1054 if (!strcmp(str, strtable[node->strnum]))
1056 return node; /* no need to check */
1061 /* Couldn't find it in the list */
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.
1075 AddStr(char *str, int strlength, int mderef)
1083 if (++strtable_end == STRTABLESIZE)
1085 /* Error, string table overflow, so we Punt */
1087 "There are too many string constants and identifiers for the compiler to handle.");
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.
1100 if ((len = strlength + 1) < NAMEDATALEN)
1103 strtable[strtable_end] = malloc((unsigned) len);
1104 strcpy(strtable[strtable_end], str);
1106 /* Now put a node in the hash table */
1108 newnode = (hashnode *) malloc(sizeof(hashnode) * 1);
1109 newnode->strnum = strtable_end;
1110 newnode->next = NULL;
1112 /* Find out where it goes */
1114 hashresult = CompHash(str, strlength);
1115 if (hashtable[hashresult] == NULL)
1116 hashtable[hashresult] = newnode;
1118 { /* There is something in the list */
1119 trail = hashtable[hashresult];
1121 while (temp != NULL)
1126 trail->next = newnode;
1134 * index_register() -- record an index that has been set up for building
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.
1144 index_register(Oid heap,
1146 IndexInfo *indexInfo)
1149 MemoryContext oldcxt;
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
1158 nogc = AllocSetContextCreate((MemoryContext) NULL,
1160 ALLOCSET_DEFAULT_MINSIZE,
1161 ALLOCSET_DEFAULT_INITSIZE,
1162 ALLOCSET_DEFAULT_MAXSIZE);
1164 oldcxt = MemoryContextSwitchTo(nogc);
1166 newind = (IndexList *) palloc(sizeof(IndexList));
1167 newind->il_heap = heap;
1168 newind->il_ind = ind;
1169 newind->il_info = (IndexInfo *) palloc(sizeof(IndexInfo));
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;
1181 newind->il_next = ILHead;
1184 MemoryContextSwitchTo(oldcxt);
1190 for (; ILHead != (IndexList *) NULL; ILHead = ILHead->il_next)
1195 heap = heap_open(ILHead->il_heap, NoLock);
1196 ind = index_open(ILHead->il_ind);
1197 index_build(heap, ind, ILHead->il_info);
1200 * In normal processing mode, index_build would close the heap and
1201 * index, but in bootstrap mode it will not.
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.
1220 if (!BootstrapAlreadySeen(RelationGetRelid(heap)))
1221 UpdateStats(RelationGetRelid(heap), 0);
1223 /* XXX Probably we ought to close the heap and index here? */