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-2003, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
11 * $PostgreSQL: pgsql/src/backend/bootstrap/bootstrap.c,v 1.187 2004/07/17 03:28:37 tgl Exp $
13 *-------------------------------------------------------------------------
24 #define BOOTSTRAP_INCLUDE /* mask out stuff in tcop/tcopprot.h */
26 #include "access/genam.h"
27 #include "access/heapam.h"
28 #include "access/xlog.h"
29 #include "bootstrap/bootstrap.h"
30 #include "catalog/catname.h"
31 #include "catalog/index.h"
32 #include "catalog/pg_type.h"
33 #include "executor/executor.h"
34 #include "libpq/pqsignal.h"
35 #include "miscadmin.h"
36 #include "postmaster/bgwriter.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/ps_status.h"
47 #include "utils/relcache.h"
50 #define ALLOC(t, c) ((t *) calloc((unsigned)(c), sizeof(t)))
52 extern int Int_yyparse(void);
53 static void usage(void);
54 static void bootstrap_signals(void);
55 static hashnode *AddStr(char *str, int strlength, int mderef);
56 static Form_pg_attribute AllocateAttribute(void);
57 static int CompHash(char *str, int len);
58 static hashnode *FindStr(char *str, int length, hashnode *mderef);
59 static Oid gettype(char *type);
60 static void cleanup(void);
67 Relation boot_reldesc; /* current relation descriptor */
70 * In the lexical analyzer, we need to get the reference number quickly from
71 * the string, and the string from the reference number. Thus we have
72 * as our data structure a hash table, where the hashing key taken from
73 * the particular string. The hash table is chained. One of the fields
74 * of the hash table node is an index into the array of character pointers.
75 * The unique index number that every string is assigned is simply the
76 * position of its string pointer in the array of string pointers.
79 #define STRTABLESIZE 10000
80 #define HASHTABLESIZE 503
82 /* Hash function numbers */
87 char *strtable[STRTABLESIZE];
88 hashnode *hashtable[HASHTABLESIZE];
90 static int strtable_end = -1; /* Tells us last occupied string space */
93 * Basic information associated with each type. This is used before
96 * XXX several of these input/output functions do catalog scans
97 * (e.g., F_REGPROCIN scans pg_proc). this obviously creates some
98 * order dependencies in the catalog creation process.
102 char name[NAMEDATALEN];
113 static const struct typinfo TypInfo[] = {
114 {"bool", BOOLOID, 0, 1, true, 'c', 'p',
115 F_BOOLIN, F_BOOLOUT},
116 {"bytea", BYTEAOID, 0, -1, false, 'i', 'x',
117 F_BYTEAIN, F_BYTEAOUT},
118 {"char", CHAROID, 0, 1, true, 'c', 'p',
119 F_CHARIN, F_CHAROUT},
120 {"name", NAMEOID, CHAROID, NAMEDATALEN, false, 'i', 'p',
121 F_NAMEIN, F_NAMEOUT},
122 {"int2", INT2OID, 0, 2, true, 's', 'p',
123 F_INT2IN, F_INT2OUT},
124 {"int4", INT4OID, 0, 4, true, 'i', 'p',
125 F_INT4IN, F_INT4OUT},
126 {"regproc", REGPROCOID, 0, 4, true, 'i', 'p',
127 F_REGPROCIN, F_REGPROCOUT},
128 {"regclass", REGCLASSOID, 0, 4, true, 'i', 'p',
129 F_REGCLASSIN, F_REGCLASSOUT},
130 {"regtype", REGTYPEOID, 0, 4, true, 'i', 'p',
131 F_REGTYPEIN, F_REGTYPEOUT},
132 {"text", TEXTOID, 0, -1, false, 'i', 'x',
133 F_TEXTIN, F_TEXTOUT},
134 {"oid", OIDOID, 0, 4, true, 'i', 'p',
136 {"tid", TIDOID, 0, 6, false, 's', 'p',
138 {"xid", XIDOID, 0, 4, true, 'i', 'p',
140 {"cid", CIDOID, 0, 4, true, 'i', 'p',
142 {"int2vector", INT2VECTOROID, INT2OID, INDEX_MAX_KEYS * 2, false, 's', 'p',
143 F_INT2VECTORIN, F_INT2VECTOROUT},
144 {"oidvector", OIDVECTOROID, OIDOID, INDEX_MAX_KEYS * 4, false, 'i', 'p',
145 F_OIDVECTORIN, F_OIDVECTOROUT},
146 {"_int4", INT4ARRAYOID, INT4OID, -1, false, 'i', 'x',
147 F_ARRAY_IN, F_ARRAY_OUT},
148 {"_text", 1009, TEXTOID, -1, false, 'i', 'x',
149 F_ARRAY_IN, F_ARRAY_OUT},
150 {"_aclitem", 1034, ACLITEMOID, -1, false, 'i', 'x',
151 F_ARRAY_IN, F_ARRAY_OUT}
154 static const int n_types = sizeof(TypInfo) / sizeof(struct typinfo);
159 FormData_pg_type am_typ;
162 static struct typmap **Typ = NULL;
163 static struct typmap *Ap = NULL;
165 static int Warnings = 0;
166 static char Blanks[MAXATTR];
168 static char *relname; /* current relation name */
170 Form_pg_attribute attrtypes[MAXATTR]; /* points to attribute info */
171 static Datum values[MAXATTR]; /* corresponding attribute values */
172 int numattr; /* number of attributes for cur. rel */
174 static MemoryContext nogc = NULL; /* special no-gc mem context */
180 * At bootstrap time, we first declare all the indices to be built, and
181 * then build them. The IndexList structure stores enough information
182 * to allow us to build the indices after they've been declared.
185 typedef struct _IndexList
190 struct _IndexList *il_next;
193 static IndexList *ILHead = NULL;
197 * The main entry point for running the backend in bootstrap mode
199 * The bootstrap mode is used to initialize the template database.
200 * The bootstrap backend doesn't speak SQL, but instead expects
201 * commands in a special bootstrap language.
203 * For historical reasons, BootstrapMain is also used as the control
204 * routine for non-backend subprocesses launched by the postmaster,
205 * such as startup and shutdown.
208 BootstrapMain(int argc, char *argv[])
213 int xlogop = BS_XLOG_NOP;
214 char *userPGDATA = NULL;
219 MyProcPid = getpid();
222 * Fire up essential subsystems: error and memory management
224 * If we are running under the postmaster, this is done already.
226 if (!IsUnderPostmaster)
230 * process command arguments
233 /* Set defaults, to be overriden by explicit options below */
235 if (!IsUnderPostmaster)
237 InitializeGUCOptions();
238 userPGDATA = getenv("PGDATA"); /* Null if no PGDATA variable */
241 /* Ignore the initial -boot argument, if present */
242 if (argc > 1 && strcmp(argv[1], "-boot") == 0)
248 while ((flag = getopt(argc, argv, "B:c:d:D:Fo:p:x:-:")) != -1)
257 /* Turn on debugging for the bootstrap process. */
258 char *debugstr = palloc(strlen("debug") + strlen(optarg) + 1);
260 sprintf(debugstr, "debug%s", optarg);
261 SetConfigOption("log_min_messages", debugstr,
262 PGC_POSTMASTER, PGC_S_ARGV);
263 SetConfigOption("client_min_messages", debugstr,
264 PGC_POSTMASTER, PGC_S_ARGV);
270 SetConfigOption("fsync", "false", PGC_POSTMASTER, PGC_S_ARGV);
273 StrNCpy(OutputFileName, optarg, MAXPGPATH);
276 xlogop = atoi(optarg);
279 dbname = strdup(optarg);
282 SetConfigOption("shared_buffers", optarg, PGC_POSTMASTER, PGC_S_ARGV);
290 ParseLongOption(optarg, &name, &value);
295 (errcode(ERRCODE_SYNTAX_ERROR),
296 errmsg("--%s requires a value",
300 (errcode(ERRCODE_SYNTAX_ERROR),
301 errmsg("-c %s requires a value",
305 SetConfigOption(name, value, PGC_POSTMASTER, PGC_S_ARGV);
317 if (!dbname && argc - optind == 1)
319 dbname = argv[optind];
322 if (!dbname || argc != optind)
325 if (!IsUnderPostmaster)
329 write_stderr("%s does not know where to find the database system data.\n"
330 "You must specify the directory that contains the database system\n"
331 "either by specifying the -D invocation option or by setting the\n"
332 "PGDATA environment variable.\n",
336 SetDataDir(userPGDATA);
339 /* Validate we have been given a reasonable-looking DataDir */
341 ValidatePgVersion(DataDir);
344 * Identify myself via ps
346 if (IsUnderPostmaster)
352 case BS_XLOG_STARTUP:
353 statmsg = "startup process";
355 case BS_XLOG_BGWRITER:
356 statmsg = "writer process";
359 statmsg = "??? process";
362 init_ps_display(statmsg, "", "");
366 /* Acquire configuration parameters, unless inherited from postmaster */
367 if (!IsUnderPostmaster)
369 ProcessConfigFile(PGC_POSTMASTER);
371 /* If timezone is not set, determine what the OS uses */
372 pg_timezone_initialize();
375 /* If standalone, create lockfile for data directory */
376 if (!IsUnderPostmaster)
377 CreateDataDirLockFile(DataDir, false);
379 SetProcessingMode(BootstrapProcessing);
380 IgnoreSystemIndexes(true);
386 /* needed to get LWLocks */
387 if (IsUnderPostmaster)
391 case BS_XLOG_BGWRITER:
392 InitDummyProcess(DUMMY_PROC_BGWRITER);
396 InitDummyProcess(DUMMY_PROC_DEFAULT);
404 SetProcessingMode(NormalProcessing);
412 case BS_XLOG_BOOTSTRAP:
418 case BS_XLOG_STARTUP:
422 proc_exit(0); /* startup done */
424 case BS_XLOG_BGWRITER:
425 /* don't set signals, bgwriter has its own agenda */
427 BackgroundWriterMain();
428 proc_exit(1); /* should never return */
431 elog(PANIC, "unrecognized XLOG op: %d", xlogop);
435 SetProcessingMode(BootstrapProcessing);
438 * backend initialization
440 InitPostgres(dbname, NULL);
443 * In NOP mode, all we really want to do is create shared memory and
444 * semaphores (just to prove we can do it with the current GUC
445 * settings). So, quit now.
447 if (xlogop == BS_XLOG_NOP)
450 /* Initialize stuff for bootstrap-file processing */
451 for (i = 0; i < MAXATTR; i++)
456 for (i = 0; i < STRTABLESIZE; ++i)
458 for (i = 0; i < HASHTABLESIZE; ++i)
462 * abort processing resumes here (this is probably dead code?)
464 if (sigsetjmp(Warn_restart, 1) != 0)
467 AbortCurrentTransaction();
471 * Process bootstrap input.
473 * the sed script boot.sed renamed yyparse to Int_yyparse for the
474 * bootstrap parser to avoid conflicts with the normal SQL parser
478 /* Perform a checkpoint to ensure everything's down to disk */
479 SetProcessingMode(NormalProcessing);
480 CreateCheckPoint(true, true);
481 SetProcessingMode(BootstrapProcessing);
483 /* Clean up and exit */
484 StartTransactionCommand();
487 /* not reached, here to make compiler happy */
492 /* ----------------------------------------------------------------
494 * ----------------------------------------------------------------
498 * usage help for the bootstrap backend
503 write_stderr("Usage:\n"
504 " postgres -boot [OPTION]... DBNAME\n"
505 " -c NAME=VALUE set run-time parameter\n"
506 " -d 1-5 debug level\n"
507 " -D datadir data directory\n"
508 " -F turn off fsync\n"
509 " -o file send debug output to file\n"
510 " -x num internal use\n");
516 * Set up signal handling for a bootstrap process
519 bootstrap_signals(void)
521 if (IsUnderPostmaster)
524 * Properly accept or ignore signals the postmaster might send us
526 pqsignal(SIGHUP, SIG_IGN);
527 pqsignal(SIGINT, SIG_IGN); /* ignore query-cancel */
528 pqsignal(SIGTERM, die);
529 pqsignal(SIGQUIT, quickdie);
530 pqsignal(SIGALRM, SIG_IGN);
531 pqsignal(SIGPIPE, SIG_IGN);
532 pqsignal(SIGUSR1, SIG_IGN);
533 pqsignal(SIGUSR2, SIG_IGN);
536 * Reset some signals that are accepted by postmaster but not here
538 pqsignal(SIGCHLD, SIG_DFL);
539 pqsignal(SIGTTIN, SIG_DFL);
540 pqsignal(SIGTTOU, SIG_DFL);
541 pqsignal(SIGCONT, SIG_DFL);
542 pqsignal(SIGWINCH, SIG_DFL);
545 * Unblock signals (they were blocked when the postmaster forked
548 PG_SETMASK(&UnBlockSig);
552 /* Set up appropriately for interactive use */
553 pqsignal(SIGHUP, die);
554 pqsignal(SIGINT, die);
555 pqsignal(SIGTERM, die);
556 pqsignal(SIGQUIT, die);
561 * error handling / abort routines
572 /* ----------------------------------------------------------------
573 * MANUAL BACKEND INTERACTIVE INTERFACE COMMANDS
574 * ----------------------------------------------------------------
582 boot_openrel(char *relname)
590 if (strlen(relname) >= NAMEDATALEN - 1)
591 relname[NAMEDATALEN - 1] = '\0';
595 rel = heap_openr(TypeRelationName, NoLock);
596 scan = heap_beginscan(rel, SnapshotNow, 0, NULL);
598 while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
601 app = Typ = ALLOC(struct typmap *, i + 1);
603 *app++ = ALLOC(struct typmap, 1);
605 scan = heap_beginscan(rel, SnapshotNow, 0, NULL);
607 while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
609 (*app)->am_oid = HeapTupleGetOid(tup);
610 memcpy((char *) &(*app)->am_typ,
611 (char *) GETSTRUCT(tup),
612 sizeof((*app)->am_typ));
616 heap_close(rel, NoLock);
619 if (boot_reldesc != NULL)
622 elog(DEBUG4, "open relation %s, attrsize %d",
623 relname ? relname : "(null)",
624 (int) ATTRIBUTE_TUPLE_SIZE);
626 boot_reldesc = heap_openr(relname, NoLock);
627 numattr = boot_reldesc->rd_rel->relnatts;
628 for (i = 0; i < numattr; i++)
630 if (attrtypes[i] == NULL)
631 attrtypes[i] = AllocateAttribute();
632 memmove((char *) attrtypes[i],
633 (char *) boot_reldesc->rd_att->attrs[i],
634 ATTRIBUTE_TUPLE_SIZE);
637 Form_pg_attribute at = attrtypes[i];
639 elog(DEBUG4, "create attribute %d name %s len %d num %d type %u",
640 i, NameStr(at->attname), at->attlen, at->attnum,
657 if (strcmp(RelationGetRelationName(boot_reldesc), name) != 0)
658 elog(ERROR, "close of %s when %s was expected",
659 name, relname ? relname : "(null)");
662 elog(ERROR, "close of %s before any relation was opened",
666 if (boot_reldesc == NULL)
667 elog(ERROR, "no open relation to close");
670 elog(DEBUG4, "close relation %s", relname ? relname : "(null)");
671 heap_close(boot_reldesc, NoLock);
681 * define a <field,type> pair
682 * if there are n fields in a relation to be created, this routine
683 * will be called n times
687 DefineAttr(char *name, char *type, int attnum)
692 if (boot_reldesc != NULL)
694 elog(WARNING, "no open relations allowed with CREATE command");
698 if (attrtypes[attnum] == NULL)
699 attrtypes[attnum] = AllocateAttribute();
700 MemSet(attrtypes[attnum], 0, ATTRIBUTE_TUPLE_SIZE);
702 namestrcpy(&attrtypes[attnum]->attname, name);
703 elog(DEBUG4, "column %s %s", NameStr(attrtypes[attnum]->attname), type);
704 attrtypes[attnum]->attnum = attnum + 1; /* fillatt */
706 typeoid = gettype(type);
710 attrtypes[attnum]->atttypid = Ap->am_oid;
711 attlen = attrtypes[attnum]->attlen = Ap->am_typ.typlen;
712 attrtypes[attnum]->attbyval = Ap->am_typ.typbyval;
713 attrtypes[attnum]->attstorage = Ap->am_typ.typstorage;
714 attrtypes[attnum]->attalign = Ap->am_typ.typalign;
715 /* if an array type, assume 1-dimensional attribute */
716 if (Ap->am_typ.typelem != InvalidOid && Ap->am_typ.typlen < 0)
717 attrtypes[attnum]->attndims = 1;
719 attrtypes[attnum]->attndims = 0;
723 attrtypes[attnum]->atttypid = TypInfo[typeoid].oid;
724 attlen = attrtypes[attnum]->attlen = TypInfo[typeoid].len;
725 attrtypes[attnum]->attbyval = TypInfo[typeoid].byval;
726 attrtypes[attnum]->attstorage = TypInfo[typeoid].storage;
727 attrtypes[attnum]->attalign = TypInfo[typeoid].align;
728 /* if an array type, assume 1-dimensional attribute */
729 if (TypInfo[typeoid].elem != InvalidOid && attlen < 0)
730 attrtypes[attnum]->attndims = 1;
732 attrtypes[attnum]->attndims = 0;
735 attrtypes[attnum]->attstattarget = -1;
736 attrtypes[attnum]->attcacheoff = -1;
737 attrtypes[attnum]->atttypmod = -1;
738 attrtypes[attnum]->attislocal = true;
741 * Mark as "not null" if type is fixed-width and prior columns are
742 * too. This corresponds to case where column can be accessed directly
743 * via C struct declaration.
749 for (i = 0; i < attnum; i++)
751 if (attrtypes[i]->attlen <= 0)
755 attrtypes[attnum]->attnotnull = true;
763 * If objectid is not zero, it is a specific OID to assign to the tuple.
764 * Otherwise, an OID will be assigned (if necessary) by heap_insert.
768 InsertOneTuple(Oid objectid)
774 elog(DEBUG4, "inserting row oid %u, %d columns", objectid, numattr);
776 tupDesc = CreateTupleDesc(numattr,
777 RelationGetForm(boot_reldesc)->relhasoids,
779 tuple = heap_formtuple(tupDesc, values, Blanks);
780 if (objectid != (Oid) 0)
781 HeapTupleSetOid(tuple, objectid);
782 pfree(tupDesc); /* just free's tupDesc, not the attrtypes */
784 simple_heap_insert(boot_reldesc, tuple);
785 heap_freetuple(tuple);
786 elog(DEBUG4, "row inserted");
789 * Reset blanks for next tuple
791 for (i = 0; i < numattr; i++)
800 InsertOneValue(char *value, int i)
808 AssertArg(i >= 0 || i < MAXATTR);
810 elog(DEBUG4, "inserting column %d value \"%s\"", i, value);
817 elog(DEBUG5, "Typ != NULL");
818 typoid = boot_reldesc->rd_att->attrs[i]->atttypid;
820 while (*app && (*app)->am_oid != typoid)
824 elog(ERROR, "could not find atttypid %u in Typ list", typoid);
826 /* XXX this should match getTypeIOParam() */
827 if (ap->am_typ.typtype == 'c')
830 typioparam = ap->am_typ.typelem;
832 typinput = ap->am_typ.typinput;
833 typoutput = ap->am_typ.typoutput;
839 /* XXX why is typoid determined differently in this path? */
840 typoid = attrtypes[i]->atttypid;
841 for (typeindex = 0; typeindex < n_types; typeindex++)
843 if (TypInfo[typeindex].oid == typoid)
846 if (typeindex >= n_types)
847 elog(ERROR, "type oid %u not found", typoid);
848 elog(DEBUG5, "Typ == NULL, typeindex = %u", typeindex);
850 /* XXX there are no composite types in TypInfo */
851 typioparam = TypInfo[typeindex].elem;
853 typinput = TypInfo[typeindex].inproc;
854 typoutput = TypInfo[typeindex].outproc;
857 values[i] = OidFunctionCall3(typinput,
858 CStringGetDatum(value),
859 ObjectIdGetDatum(typioparam),
861 prt = DatumGetCString(OidFunctionCall3(typoutput,
863 ObjectIdGetDatum(typioparam),
865 elog(DEBUG4, "inserted -> %s", prt);
876 elog(DEBUG4, "inserting column %d NULL", i);
877 Assert(i >= 0 || i < MAXATTR);
878 values[i] = PointerGetDatum(NULL);
889 static int beenhere = 0;
895 elog(FATAL, "cleanup called twice");
898 if (boot_reldesc != NULL)
900 CommitTransactionCommand();
901 proc_exit(Warnings ? 1 : 0);
907 * NB: this is really ugly; it will return an integer index into TypInfo[],
908 * and not an OID at all, until the first reference to a type not known in
909 * TypInfo[]. At that point it will read and cache pg_type in the Typ array,
910 * and subsequently return a real OID (and set the global pointer Ap to
911 * point at the found row in Typ). So caller must check whether Typ is
912 * still NULL to determine what the return value is!
926 for (app = Typ; *app != NULL; app++)
928 if (strncmp(NameStr((*app)->am_typ.typname), type, NAMEDATALEN) == 0)
931 return (*app)->am_oid;
937 for (i = 0; i < n_types; i++)
939 if (strncmp(type, TypInfo[i].name, NAMEDATALEN) == 0)
942 elog(DEBUG4, "external type: %s", type);
943 rel = heap_openr(TypeRelationName, NoLock);
944 scan = heap_beginscan(rel, SnapshotNow, 0, NULL);
946 while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
949 app = Typ = ALLOC(struct typmap *, i + 1);
951 *app++ = ALLOC(struct typmap, 1);
953 scan = heap_beginscan(rel, SnapshotNow, 0, NULL);
955 while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
957 (*app)->am_oid = HeapTupleGetOid(tup);
958 memmove((char *) &(*app++)->am_typ,
959 (char *) GETSTRUCT(tup),
960 sizeof((*app)->am_typ));
963 heap_close(rel, NoLock);
964 return gettype(type);
966 elog(ERROR, "unrecognized type \"%s\"", type);
968 /* not reached, here to make compiler happy */
976 static Form_pg_attribute
977 AllocateAttribute(void)
979 Form_pg_attribute attribute = (Form_pg_attribute) malloc(ATTRIBUTE_TUPLE_SIZE);
981 if (!PointerIsValid(attribute))
982 elog(FATAL, "out of memory");
983 MemSet(attribute, 0, ATTRIBUTE_TUPLE_SIZE);
990 * XXX arrays of "basetype" are always "_basetype".
991 * this is an evil hack inherited from rel. 3.1.
992 * XXX array dimension is thrown away because we
993 * don't support fixed-dimension arrays. again,
996 * the string passed in must have a '[' character in it
998 * the string returned is a pointer to static storage and should NOT
999 * be freed by the CALLER.
1003 MapArrayTypeName(char *s)
1007 static char newStr[NAMEDATALEN]; /* array type names < NAMEDATALEN
1010 if (s == NULL || s[0] == '\0')
1015 for (i = 0; i < NAMEDATALEN - 1 && s[i] != '['; i++, j++)
1025 * returns the string table position of the identifier
1026 * passed to it. We add it to the table if we can't find it.
1030 EnterString(char *str)
1037 node = FindStr(str, len, 0);
1039 return node->strnum;
1042 node = AddStr(str, len, 0);
1043 return node->strnum;
1049 * when given an idnum into the 'string-table' return the string
1050 * associated with the idnum
1054 LexIDStr(int ident_num)
1056 return strtable[ident_num];
1063 * Compute a hash function for a given string. We look at the first,
1064 * the last, and the middle character of a string to try to get spread
1065 * the strings out. The function is rather arbitrary, except that we
1066 * are mod'ing by a prime number.
1070 CompHash(char *str, int len)
1074 result = (NUM * str[0] + NUMSQR * str[len - 1] + NUMCUBE * str[(len - 1) / 2]);
1076 return result % HASHTABLESIZE;
1083 * This routine looks for the specified string in the hash
1084 * table. It returns a pointer to the hash node found,
1085 * or NULL if the string is not in the table.
1089 FindStr(char *str, int length, hashnode *mderef)
1093 node = hashtable[CompHash(str, length)];
1094 while (node != NULL)
1097 * We must differentiate between string constants that might have
1098 * the same value as a identifier and the identifier itself.
1100 if (!strcmp(str, strtable[node->strnum]))
1102 return node; /* no need to check */
1107 /* Couldn't find it in the list */
1114 * This function adds the specified string, along with its associated
1115 * data, to the hash table and the string table. We return the node
1116 * so that the calling routine can find out the unique id that AddStr
1117 * has assigned to this string.
1121 AddStr(char *str, int strlength, int mderef)
1129 if (++strtable_end >= STRTABLESIZE)
1130 elog(FATAL, "bootstrap string table overflow");
1133 * Some of the utilites (eg, define type, create relation) assume that
1134 * the string they're passed is a NAMEDATALEN. We get array bound
1135 * read violations from purify if we don't allocate at least
1136 * NAMEDATALEN bytes for strings of this sort. Because we're lazy, we
1137 * allocate at least NAMEDATALEN bytes all the time.
1140 if ((len = strlength + 1) < NAMEDATALEN)
1143 strtable[strtable_end] = malloc((unsigned) len);
1144 strcpy(strtable[strtable_end], str);
1146 /* Now put a node in the hash table */
1148 newnode = (hashnode *) malloc(sizeof(hashnode) * 1);
1149 newnode->strnum = strtable_end;
1150 newnode->next = NULL;
1152 /* Find out where it goes */
1154 hashresult = CompHash(str, strlength);
1155 if (hashtable[hashresult] == NULL)
1156 hashtable[hashresult] = newnode;
1158 { /* There is something in the list */
1159 trail = hashtable[hashresult];
1161 while (temp != NULL)
1166 trail->next = newnode;
1174 * index_register() -- record an index that has been set up for building
1177 * At bootstrap time, we define a bunch of indices on system catalogs.
1178 * We postpone actually building the indices until just before we're
1179 * finished with initialization, however. This is because more classes
1180 * and indices may be defined, and we want to be sure that all of them
1181 * are present in the index.
1184 index_register(Oid heap,
1186 IndexInfo *indexInfo)
1189 MemoryContext oldcxt;
1192 * XXX mao 10/31/92 -- don't gc index reldescs, associated info at
1193 * bootstrap time. we'll declare the indices now, but want to create
1198 nogc = AllocSetContextCreate(NULL,
1200 ALLOCSET_DEFAULT_MINSIZE,
1201 ALLOCSET_DEFAULT_INITSIZE,
1202 ALLOCSET_DEFAULT_MAXSIZE);
1204 oldcxt = MemoryContextSwitchTo(nogc);
1206 newind = (IndexList *) palloc(sizeof(IndexList));
1207 newind->il_heap = heap;
1208 newind->il_ind = ind;
1209 newind->il_info = (IndexInfo *) palloc(sizeof(IndexInfo));
1211 memcpy(newind->il_info, indexInfo, sizeof(IndexInfo));
1212 /* expressions will likely be null, but may as well copy it */
1213 newind->il_info->ii_Expressions = (List *)
1214 copyObject(indexInfo->ii_Expressions);
1215 newind->il_info->ii_ExpressionsState = NIL;
1216 /* predicate will likely be null, but may as well copy it */
1217 newind->il_info->ii_Predicate = (List *)
1218 copyObject(indexInfo->ii_Predicate);
1219 newind->il_info->ii_PredicateState = NIL;
1221 newind->il_next = ILHead;
1224 MemoryContextSwitchTo(oldcxt);
1230 for (; ILHead != NULL; ILHead = ILHead->il_next)
1235 heap = heap_open(ILHead->il_heap, NoLock);
1236 ind = index_open(ILHead->il_ind);
1237 index_build(heap, ind, ILHead->il_info);
1240 * In normal processing mode, index_build would close the heap and
1241 * index, but in bootstrap mode it will not.
1244 /* XXX Probably we ought to close the heap and index here? */