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-2006, 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.218 2006/06/27 22:16:43 momjian Exp $
13 *-------------------------------------------------------------------------
23 #define BOOTSTRAP_INCLUDE /* mask out stuff in tcop/tcopprot.h */
25 #include "access/genam.h"
26 #include "access/heapam.h"
27 #include "access/xlog.h"
28 #include "bootstrap/bootstrap.h"
29 #include "catalog/index.h"
30 #include "catalog/pg_type.h"
31 #include "executor/executor.h"
32 #include "libpq/pqsignal.h"
33 #include "miscadmin.h"
34 #include "nodes/makefuncs.h"
35 #include "postmaster/bgwriter.h"
36 #include "storage/freespace.h"
37 #include "storage/ipc.h"
38 #include "storage/pg_shmem.h"
39 #include "storage/proc.h"
40 #include "tcop/tcopprot.h"
41 #include "utils/builtins.h"
42 #include "utils/flatfiles.h"
43 #include "utils/fmgroids.h"
44 #include "utils/guc.h"
45 #include "utils/lsyscache.h"
46 #include "utils/memutils.h"
47 #include "utils/ps_status.h"
48 #include "utils/relcache.h"
54 #define ALLOC(t, c) ((t *) calloc((unsigned)(c), sizeof(t)))
56 static void bootstrap_signals(void);
57 static void ShutdownDummyProcess(int code, Datum arg);
58 static hashnode *AddStr(char *str, int strlength, int mderef);
59 static Form_pg_attribute AllocateAttribute(void);
60 static int CompHash(char *str, int len);
61 static hashnode *FindStr(char *str, int length, hashnode *mderef);
62 static Oid gettype(char *type);
63 static void cleanup(void);
70 Relation boot_reldesc; /* current relation descriptor */
73 * In the lexical analyzer, we need to get the reference number quickly from
74 * the string, and the string from the reference number. Thus we have
75 * as our data structure a hash table, where the hashing key taken from
76 * the particular string. The hash table is chained. One of the fields
77 * of the hash table node is an index into the array of character pointers.
78 * The unique index number that every string is assigned is simply the
79 * position of its string pointer in the array of string pointers.
82 #define STRTABLESIZE 10000
83 #define HASHTABLESIZE 503
85 /* Hash function numbers */
90 char *strtable[STRTABLESIZE];
91 hashnode *hashtable[HASHTABLESIZE];
93 static int strtable_end = -1; /* Tells us last occupied string space */
96 * Basic information associated with each type. This is used before
99 * XXX several of these input/output functions do catalog scans
100 * (e.g., F_REGPROCIN scans pg_proc). this obviously creates some
101 * order dependencies in the catalog creation process.
105 char name[NAMEDATALEN];
116 static const struct typinfo TypInfo[] = {
117 {"bool", BOOLOID, 0, 1, true, 'c', 'p',
118 F_BOOLIN, F_BOOLOUT},
119 {"bytea", BYTEAOID, 0, -1, false, 'i', 'x',
120 F_BYTEAIN, F_BYTEAOUT},
121 {"char", CHAROID, 0, 1, true, 'c', 'p',
122 F_CHARIN, F_CHAROUT},
123 {"name", NAMEOID, CHAROID, NAMEDATALEN, false, 'i', 'p',
124 F_NAMEIN, F_NAMEOUT},
125 {"int2", INT2OID, 0, 2, true, 's', 'p',
126 F_INT2IN, F_INT2OUT},
127 {"int4", INT4OID, 0, 4, true, 'i', 'p',
128 F_INT4IN, F_INT4OUT},
129 {"regproc", REGPROCOID, 0, 4, true, 'i', 'p',
130 F_REGPROCIN, F_REGPROCOUT},
131 {"regclass", REGCLASSOID, 0, 4, true, 'i', 'p',
132 F_REGCLASSIN, F_REGCLASSOUT},
133 {"regtype", REGTYPEOID, 0, 4, true, 'i', 'p',
134 F_REGTYPEIN, F_REGTYPEOUT},
135 {"text", TEXTOID, 0, -1, false, 'i', 'x',
136 F_TEXTIN, F_TEXTOUT},
137 {"oid", OIDOID, 0, 4, true, 'i', 'p',
139 {"tid", TIDOID, 0, 6, false, 's', 'p',
141 {"xid", XIDOID, 0, 4, true, 'i', 'p',
143 {"cid", CIDOID, 0, 4, true, 'i', 'p',
145 {"int2vector", INT2VECTOROID, INT2OID, -1, false, 'i', 'p',
146 F_INT2VECTORIN, F_INT2VECTOROUT},
147 {"oidvector", OIDVECTOROID, OIDOID, -1, false, 'i', 'p',
148 F_OIDVECTORIN, F_OIDVECTOROUT},
149 {"_int4", INT4ARRAYOID, INT4OID, -1, false, 'i', 'x',
150 F_ARRAY_IN, F_ARRAY_OUT},
151 {"_text", 1009, TEXTOID, -1, false, 'i', 'x',
152 F_ARRAY_IN, F_ARRAY_OUT},
153 {"_oid", 1028, OIDOID, -1, false, 'i', 'x',
154 F_ARRAY_IN, F_ARRAY_OUT},
155 {"_char", 1002, CHAROID, -1, false, 'i', 'x',
156 F_ARRAY_IN, F_ARRAY_OUT},
157 {"_aclitem", 1034, ACLITEMOID, -1, false, 'i', 'x',
158 F_ARRAY_IN, F_ARRAY_OUT}
161 static const int n_types = sizeof(TypInfo) / sizeof(struct typinfo);
166 FormData_pg_type am_typ;
169 static struct typmap **Typ = NULL;
170 static struct typmap *Ap = NULL;
172 static int Warnings = 0;
173 static char Blanks[MAXATTR];
175 Form_pg_attribute attrtypes[MAXATTR]; /* points to attribute info */
176 static Datum values[MAXATTR]; /* corresponding attribute values */
177 int numattr; /* number of attributes for cur. rel */
179 static MemoryContext nogc = NULL; /* special no-gc mem context */
182 * At bootstrap time, we first declare all the indices to be built, and
183 * then build them. The IndexList structure stores enough information
184 * to allow us to build the indices after they've been declared.
187 typedef struct _IndexList
192 struct _IndexList *il_next;
195 static IndexList *ILHead = NULL;
199 * The main entry point for running the backend in bootstrap mode
201 * The bootstrap mode is used to initialize the template database.
202 * The bootstrap backend doesn't speak SQL, but instead expects
203 * commands in a special bootstrap language.
205 * For historical reasons, BootstrapMain is also used as the control
206 * routine for non-backend subprocesses launched by the postmaster,
207 * such as startup and shutdown.
210 BootstrapMain(int argc, char *argv[])
212 char *progname = argv[0];
216 int xlogop = BS_XLOG_NOP;
217 char *userDoption = NULL;
222 MyProcPid = getpid();
225 * Fire up essential subsystems: error and memory management
227 * If we are running under the postmaster, this is done already.
229 if (!IsUnderPostmaster)
232 /* Compute paths, if we didn't inherit them from postmaster */
233 if (my_exec_path[0] == '\0')
235 if (find_my_exec(progname, my_exec_path) < 0)
236 elog(FATAL, "%s: could not locate my own executable path",
241 * process command arguments
244 /* Set defaults, to be overriden by explicit options below */
246 if (!IsUnderPostmaster)
247 InitializeGUCOptions();
249 /* Ignore the initial --boot argument, if present */
250 if (argc > 1 && strcmp(argv[1], "--boot") == 0)
256 while ((flag = getopt(argc, argv, "B:c:d:D:Fr:x:y:-:")) != -1)
261 SetConfigOption("shared_buffers", optarg, PGC_POSTMASTER, PGC_S_ARGV);
264 userDoption = optarg;
268 /* Turn on debugging for the bootstrap process. */
269 char *debugstr = palloc(strlen("debug") + strlen(optarg) + 1);
271 sprintf(debugstr, "debug%s", optarg);
272 SetConfigOption("log_min_messages", debugstr,
273 PGC_POSTMASTER, PGC_S_ARGV);
274 SetConfigOption("client_min_messages", debugstr,
275 PGC_POSTMASTER, PGC_S_ARGV);
280 SetConfigOption("fsync", "false", PGC_POSTMASTER, PGC_S_ARGV);
283 StrNCpy(OutputFileName, optarg, MAXPGPATH);
286 xlogop = atoi(optarg);
289 dbname = strdup(optarg);
297 ParseLongOption(optarg, &name, &value);
302 (errcode(ERRCODE_SYNTAX_ERROR),
303 errmsg("--%s requires a value",
307 (errcode(ERRCODE_SYNTAX_ERROR),
308 errmsg("-c %s requires a value",
312 SetConfigOption(name, value, PGC_POSTMASTER, PGC_S_ARGV);
319 write_stderr("Try \"%s --help\" for more information.\n",
326 if (!dbname && argc - optind == 1)
328 dbname = argv[optind];
331 if (!dbname || argc != optind)
333 write_stderr("%s: invalid command-line arguments\n", progname);
338 * Identify myself via ps
340 if (IsUnderPostmaster)
346 case BS_XLOG_STARTUP:
347 statmsg = "startup process";
349 case BS_XLOG_BGWRITER:
350 statmsg = "writer process";
353 statmsg = "??? process";
356 init_ps_display(statmsg, "", "", "");
359 /* Acquire configuration parameters, unless inherited from postmaster */
360 if (!IsUnderPostmaster)
362 if (!SelectConfigFiles(userDoption, progname))
364 /* If timezone is not set, determine what the OS uses */
365 pg_timezone_initialize();
368 /* Validate we have been given a reasonable-looking DataDir */
370 ValidatePgVersion(DataDir);
372 /* Change into DataDir (if under postmaster, should be done already) */
373 if (!IsUnderPostmaster)
376 /* If standalone, create lockfile for data directory */
377 if (!IsUnderPostmaster)
378 CreateDataDirLockFile(false);
380 SetProcessingMode(BootstrapProcessing);
381 IgnoreSystemIndexes = true;
386 * When we are a dummy process, we aren't going to do the full
387 * InitPostgres pushups, but there are a couple of things that need
388 * to get lit up even in a dummy process.
390 if (IsUnderPostmaster)
393 * Create a PGPROC so we can use LWLocks. In the EXEC_BACKEND case,
394 * this was already done by SubPostmasterMain().
400 /* finish setting up bufmgr.c */
401 InitBufferPoolBackend();
403 /* register a shutdown callback for LWLock cleanup */
404 on_shmem_exit(ShutdownDummyProcess, 0);
410 SetProcessingMode(NormalProcessing);
418 case BS_XLOG_BOOTSTRAP:
424 case BS_XLOG_STARTUP:
428 * These next two functions don't consider themselves critical,
429 * but we'd best PANIC anyway if they fail.
431 START_CRIT_SECTION();
433 BuildFlatFiles(false);
435 proc_exit(0); /* startup done */
437 case BS_XLOG_BGWRITER:
438 /* don't set signals, bgwriter has its own agenda */
440 BackgroundWriterMain();
441 proc_exit(1); /* should never return */
444 elog(PANIC, "unrecognized XLOG op: %d", xlogop);
449 * We must be getting invoked for bootstrap mode
451 Assert(!IsUnderPostmaster);
453 SetProcessingMode(BootstrapProcessing);
456 * Do backend-like initialization for bootstrap mode
459 (void) InitPostgres(dbname, NULL);
462 * In NOP mode, all we really want to do is create shared memory and
463 * semaphores (just to prove we can do it with the current GUC settings).
466 if (xlogop == BS_XLOG_NOP)
469 /* Initialize stuff for bootstrap-file processing */
470 for (i = 0; i < MAXATTR; i++)
475 for (i = 0; i < STRTABLESIZE; ++i)
477 for (i = 0; i < HASHTABLESIZE; ++i)
481 * Process bootstrap input.
485 /* Perform a checkpoint to ensure everything's down to disk */
486 SetProcessingMode(NormalProcessing);
487 CreateCheckPoint(true, true);
488 SetProcessingMode(BootstrapProcessing);
490 /* Clean up and exit */
491 StartTransactionCommand();
494 /* not reached, here to make compiler happy */
499 /* ----------------------------------------------------------------
501 * ----------------------------------------------------------------
505 * Set up signal handling for a bootstrap process
508 bootstrap_signals(void)
510 if (IsUnderPostmaster)
513 * Properly accept or ignore signals the postmaster might send us
515 pqsignal(SIGHUP, SIG_IGN);
516 pqsignal(SIGINT, SIG_IGN); /* ignore query-cancel */
517 pqsignal(SIGTERM, die);
518 pqsignal(SIGQUIT, quickdie);
519 pqsignal(SIGALRM, SIG_IGN);
520 pqsignal(SIGPIPE, SIG_IGN);
521 pqsignal(SIGUSR1, SIG_IGN);
522 pqsignal(SIGUSR2, SIG_IGN);
525 * Reset some signals that are accepted by postmaster but not here
527 pqsignal(SIGCHLD, SIG_DFL);
528 pqsignal(SIGTTIN, SIG_DFL);
529 pqsignal(SIGTTOU, SIG_DFL);
530 pqsignal(SIGCONT, SIG_DFL);
531 pqsignal(SIGWINCH, SIG_DFL);
534 * Unblock signals (they were blocked when the postmaster forked us)
536 PG_SETMASK(&UnBlockSig);
540 /* Set up appropriately for interactive use */
541 pqsignal(SIGHUP, die);
542 pqsignal(SIGINT, die);
543 pqsignal(SIGTERM, die);
544 pqsignal(SIGQUIT, die);
549 * Begin shutdown of a dummy process. This is approximately the equivalent
550 * of ShutdownPostgres() in postinit.c. We can't run transactions in a
551 * dummy process, so most of the work of AbortTransaction() is not needed,
552 * but we do need to make sure we've released any LWLocks we are holding.
553 * (This is only critical during an error exit.)
556 ShutdownDummyProcess(int code, Datum arg)
562 * error handling / abort routines
573 /* ----------------------------------------------------------------
574 * MANUAL BACKEND INTERACTIVE INTERFACE COMMANDS
575 * ----------------------------------------------------------------
583 boot_openrel(char *relname)
591 if (strlen(relname) >= NAMEDATALEN)
592 relname[NAMEDATALEN - 1] = '\0';
596 rel = heap_open(TypeRelationId, NoLock);
597 scan = heap_beginscan(rel, SnapshotNow, 0, NULL);
599 while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
602 app = Typ = ALLOC(struct typmap *, i + 1);
604 *app++ = ALLOC(struct typmap, 1);
606 scan = heap_beginscan(rel, SnapshotNow, 0, NULL);
608 while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
610 (*app)->am_oid = HeapTupleGetOid(tup);
611 memcpy((char *) &(*app)->am_typ,
612 (char *) GETSTRUCT(tup),
613 sizeof((*app)->am_typ));
617 heap_close(rel, NoLock);
620 if (boot_reldesc != NULL)
623 elog(DEBUG4, "open relation %s, attrsize %d",
624 relname, (int) ATTRIBUTE_TUPLE_SIZE);
626 boot_reldesc = heap_openrv(makeRangeVar(NULL, 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, RelationGetRelationName(boot_reldesc));
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",
671 RelationGetRelationName(boot_reldesc));
672 heap_close(boot_reldesc, NoLock);
682 * define a <field,type> pair
683 * if there are n fields in a relation to be created, this routine
684 * will be called n times
688 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 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 attrtypes[attnum]->attlen = TypInfo[typeoid].len;
725 attrtypes[attnum]->attbyval = TypInfo[typeoid].byval;
726 attrtypes[attnum]->attstorage = TypInfo[typeoid].storage;
727 attrtypes[attnum]->attalign = TypInfo[typeoid].align;
728 /* if an array type, assume 1-dimensional attribute */
729 if (TypInfo[typeoid].elem != InvalidOid &&
730 attrtypes[attnum]->attlen < 0)
731 attrtypes[attnum]->attndims = 1;
733 attrtypes[attnum]->attndims = 0;
736 attrtypes[attnum]->attstattarget = -1;
737 attrtypes[attnum]->attcacheoff = -1;
738 attrtypes[attnum]->atttypmod = -1;
739 attrtypes[attnum]->attislocal = true;
742 * Mark as "not null" if type is fixed-width and prior columns are too.
743 * This corresponds to case where column can be accessed directly via C
744 * struct declaration.
746 * oidvector and int2vector are also treated as not-nullable, even though
747 * they are no longer fixed-width.
749 #define MARKNOTNULL(att) \
750 ((att)->attlen > 0 || \
751 (att)->atttypid == OIDVECTOROID || \
752 (att)->atttypid == INT2VECTOROID)
754 if (MARKNOTNULL(attrtypes[attnum]))
758 for (i = 0; i < attnum; i++)
760 if (!MARKNOTNULL(attrtypes[i]))
764 attrtypes[attnum]->attnotnull = true;
772 * If objectid is not zero, it is a specific OID to assign to the tuple.
773 * Otherwise, an OID will be assigned (if necessary) by heap_insert.
777 InsertOneTuple(Oid objectid)
783 elog(DEBUG4, "inserting row oid %u, %d columns", objectid, numattr);
785 tupDesc = CreateTupleDesc(numattr,
786 RelationGetForm(boot_reldesc)->relhasoids,
788 tuple = heap_formtuple(tupDesc, values, Blanks);
789 if (objectid != (Oid) 0)
790 HeapTupleSetOid(tuple, objectid);
791 pfree(tupDesc); /* just free's tupDesc, not the attrtypes */
793 simple_heap_insert(boot_reldesc, tuple);
794 heap_freetuple(tuple);
795 elog(DEBUG4, "row inserted");
798 * Reset blanks for next tuple
800 for (i = 0; i < numattr; i++)
809 InsertOneValue(char *value, int i)
817 AssertArg(i >= 0 || i < MAXATTR);
819 elog(DEBUG4, "inserting column %d value \"%s\"", i, value);
826 elog(DEBUG5, "Typ != NULL");
827 typoid = boot_reldesc->rd_att->attrs[i]->atttypid;
829 while (*app && (*app)->am_oid != typoid)
833 elog(ERROR, "could not find atttypid %u in Typ list", typoid);
835 /* XXX this logic should match getTypeIOParam() */
836 if (OidIsValid(ap->am_typ.typelem))
837 typioparam = ap->am_typ.typelem;
841 typinput = ap->am_typ.typinput;
842 typoutput = ap->am_typ.typoutput;
848 /* XXX why is typoid determined differently in this path? */
849 typoid = attrtypes[i]->atttypid;
850 for (typeindex = 0; typeindex < n_types; typeindex++)
852 if (TypInfo[typeindex].oid == typoid)
855 if (typeindex >= n_types)
856 elog(ERROR, "type oid %u not found", typoid);
857 elog(DEBUG5, "Typ == NULL, typeindex = %u", typeindex);
859 /* XXX this logic should match getTypeIOParam() */
860 if (OidIsValid(TypInfo[typeindex].elem))
861 typioparam = TypInfo[typeindex].elem;
865 typinput = TypInfo[typeindex].inproc;
866 typoutput = TypInfo[typeindex].outproc;
869 values[i] = OidInputFunctionCall(typinput, value, typioparam, -1);
870 prt = OidOutputFunctionCall(typoutput, values[i]);
871 elog(DEBUG4, "inserted -> %s", prt);
882 elog(DEBUG4, "inserting column %d NULL", i);
883 Assert(i >= 0 || i < MAXATTR);
884 values[i] = PointerGetDatum(NULL);
895 static int beenhere = 0;
901 elog(FATAL, "cleanup called twice");
904 if (boot_reldesc != NULL)
906 CommitTransactionCommand();
907 proc_exit(Warnings ? 1 : 0);
913 * NB: this is really ugly; it will return an integer index into TypInfo[],
914 * and not an OID at all, until the first reference to a type not known in
915 * TypInfo[]. At that point it will read and cache pg_type in the Typ array,
916 * and subsequently return a real OID (and set the global pointer Ap to
917 * point at the found row in Typ). So caller must check whether Typ is
918 * still NULL to determine what the return value is!
932 for (app = Typ; *app != NULL; app++)
934 if (strncmp(NameStr((*app)->am_typ.typname), type, NAMEDATALEN) == 0)
937 return (*app)->am_oid;
943 for (i = 0; i < n_types; i++)
945 if (strncmp(type, TypInfo[i].name, NAMEDATALEN) == 0)
948 elog(DEBUG4, "external type: %s", type);
949 rel = heap_open(TypeRelationId, NoLock);
950 scan = heap_beginscan(rel, SnapshotNow, 0, NULL);
952 while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
955 app = Typ = ALLOC(struct typmap *, i + 1);
957 *app++ = ALLOC(struct typmap, 1);
959 scan = heap_beginscan(rel, SnapshotNow, 0, NULL);
961 while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
963 (*app)->am_oid = HeapTupleGetOid(tup);
964 memmove((char *) &(*app++)->am_typ,
965 (char *) GETSTRUCT(tup),
966 sizeof((*app)->am_typ));
969 heap_close(rel, NoLock);
970 return gettype(type);
972 elog(ERROR, "unrecognized type \"%s\"", type);
974 /* not reached, here to make compiler happy */
982 static Form_pg_attribute
983 AllocateAttribute(void)
985 Form_pg_attribute attribute = (Form_pg_attribute) malloc(ATTRIBUTE_TUPLE_SIZE);
987 if (!PointerIsValid(attribute))
988 elog(FATAL, "out of memory");
989 MemSet(attribute, 0, ATTRIBUTE_TUPLE_SIZE);
996 * XXX arrays of "basetype" are always "_basetype".
997 * this is an evil hack inherited from rel. 3.1.
998 * XXX array dimension is thrown away because we
999 * don't support fixed-dimension arrays. again,
1000 * sickness from 3.1.
1002 * the string passed in must have a '[' character in it
1004 * the string returned is a pointer to static storage and should NOT
1005 * be freed by the CALLER.
1009 MapArrayTypeName(char *s)
1013 static char newStr[NAMEDATALEN]; /* array type names < NAMEDATALEN long */
1015 if (s == NULL || s[0] == '\0')
1020 for (i = 0; i < NAMEDATALEN - 1 && s[i] != '['; i++, j++)
1030 * returns the string table position of the identifier
1031 * passed to it. We add it to the table if we can't find it.
1035 EnterString(char *str)
1042 node = FindStr(str, len, NULL);
1044 return node->strnum;
1047 node = AddStr(str, len, 0);
1048 return node->strnum;
1054 * when given an idnum into the 'string-table' return the string
1055 * associated with the idnum
1059 LexIDStr(int ident_num)
1061 return strtable[ident_num];
1068 * Compute a hash function for a given string. We look at the first,
1069 * the last, and the middle character of a string to try to get spread
1070 * the strings out. The function is rather arbitrary, except that we
1071 * are mod'ing by a prime number.
1075 CompHash(char *str, int len)
1079 result = (NUM * str[0] + NUMSQR * str[len - 1] + NUMCUBE * str[(len - 1) / 2]);
1081 return result % HASHTABLESIZE;
1088 * This routine looks for the specified string in the hash
1089 * table. It returns a pointer to the hash node found,
1090 * or NULL if the string is not in the table.
1094 FindStr(char *str, int length, hashnode *mderef)
1098 node = hashtable[CompHash(str, length)];
1099 while (node != NULL)
1102 * We must differentiate between string constants that might have the
1103 * same value as a identifier and the identifier itself.
1105 if (!strcmp(str, strtable[node->strnum]))
1107 return node; /* no need to check */
1112 /* Couldn't find it in the list */
1119 * This function adds the specified string, along with its associated
1120 * data, to the hash table and the string table. We return the node
1121 * so that the calling routine can find out the unique id that AddStr
1122 * has assigned to this string.
1126 AddStr(char *str, int strlength, int mderef)
1134 if (++strtable_end >= STRTABLESIZE)
1135 elog(FATAL, "bootstrap string table overflow");
1138 * Some of the utilites (eg, define type, create relation) assume that the
1139 * string they're passed is a NAMEDATALEN. We get array bound read
1140 * violations from purify if we don't allocate at least NAMEDATALEN bytes
1141 * for strings of this sort. Because we're lazy, we allocate at least
1142 * NAMEDATALEN bytes all the time.
1145 if ((len = strlength + 1) < NAMEDATALEN)
1148 strtable[strtable_end] = malloc((unsigned) len);
1149 strcpy(strtable[strtable_end], str);
1151 /* Now put a node in the hash table */
1153 newnode = (hashnode *) malloc(sizeof(hashnode) * 1);
1154 newnode->strnum = strtable_end;
1155 newnode->next = NULL;
1157 /* Find out where it goes */
1159 hashresult = CompHash(str, strlength);
1160 if (hashtable[hashresult] == NULL)
1161 hashtable[hashresult] = newnode;
1163 { /* There is something in the list */
1164 trail = hashtable[hashresult];
1166 while (temp != NULL)
1171 trail->next = newnode;
1179 * index_register() -- record an index that has been set up for building
1182 * At bootstrap time, we define a bunch of indexes on system catalogs.
1183 * We postpone actually building the indexes until just before we're
1184 * finished with initialization, however. This is because the indexes
1185 * themselves have catalog entries, and those have to be included in the
1186 * indexes on those catalogs. Doing it in two phases is the simplest
1187 * way of making sure the indexes have the right contents at the end.
1190 index_register(Oid heap,
1192 IndexInfo *indexInfo)
1195 MemoryContext oldcxt;
1198 * XXX mao 10/31/92 -- don't gc index reldescs, associated info at
1199 * bootstrap time. we'll declare the indexes now, but want to create them
1204 nogc = AllocSetContextCreate(NULL,
1206 ALLOCSET_DEFAULT_MINSIZE,
1207 ALLOCSET_DEFAULT_INITSIZE,
1208 ALLOCSET_DEFAULT_MAXSIZE);
1210 oldcxt = MemoryContextSwitchTo(nogc);
1212 newind = (IndexList *) palloc(sizeof(IndexList));
1213 newind->il_heap = heap;
1214 newind->il_ind = ind;
1215 newind->il_info = (IndexInfo *) palloc(sizeof(IndexInfo));
1217 memcpy(newind->il_info, indexInfo, sizeof(IndexInfo));
1218 /* expressions will likely be null, but may as well copy it */
1219 newind->il_info->ii_Expressions = (List *)
1220 copyObject(indexInfo->ii_Expressions);
1221 newind->il_info->ii_ExpressionsState = NIL;
1222 /* predicate will likely be null, but may as well copy it */
1223 newind->il_info->ii_Predicate = (List *)
1224 copyObject(indexInfo->ii_Predicate);
1225 newind->il_info->ii_PredicateState = NIL;
1227 newind->il_next = ILHead;
1230 MemoryContextSwitchTo(oldcxt);
1235 * build_indices -- fill in all the indexes registered earlier
1240 for (; ILHead != NULL; ILHead = ILHead->il_next)
1245 heap = heap_open(ILHead->il_heap, NoLock);
1246 ind = index_open(ILHead->il_ind);
1248 index_build(heap, ind, ILHead->il_info, false, false);
1251 heap_close(heap, NoLock);