📄 bootstrap.c
字号:
/* ---------------- * InsertOneTuple * assumes that 'oid' will not be zero. * ---------------- */voidInsertOneTuple(Oid objectid){ HeapTuple tuple; TupleDesc tupDesc; int i; if (DebugMode) { printf("InsertOneTuple oid %u, %d attrs\n", objectid, numattr); fflush(stdout); } tupDesc = CreateTupleDesc(numattr, attrtypes); tuple = heap_formtuple(tupDesc, (Datum *) values, Blanks); pfree(tupDesc); /* just free's tupDesc, not the attrtypes */ if (objectid != (Oid) 0) tuple->t_data->t_oid = objectid; heap_insert(reldesc, tuple); pfree(tuple); if (DebugMode) { printf("End InsertOneTuple, objectid=%u\n", objectid); fflush(stdout); } /* * Reset blanks for next tuple */ for (i = 0; i < numattr; i++) Blanks[i] = ' ';}/* ---------------- * InsertOneValue * ---------------- */voidInsertOneValue(Oid objectid, char *value, int i){ int typeindex; char *prt; struct typmap **app; if (DebugMode) printf("Inserting value: '%s'\n", value); if (i < 0 || i >= MAXATTR) { printf("i out of range: %d\n", i); Assert(0); } if (Typ != (struct typmap **) NULL) { struct typmap *ap; if (DebugMode) puts("Typ != NULL"); app = Typ; while (*app && (*app)->am_oid != reldesc->rd_att->attrs[i]->atttypid) ++app; ap = *app; if (ap == NULL) { printf("Unable to find atttypid in Typ list! %u\n", reldesc->rd_att->attrs[i]->atttypid ); Assert(0); } values[i] = fmgr(ap->am_typ.typinput, value, ap->am_typ.typelem, -1); /* shouldn't have char() or varchar() * types during boostrapping but just to * be safe */ prt = fmgr(ap->am_typ.typoutput, values[i], ap->am_typ.typelem); if (!Quiet) printf("%s ", prt); pfree(prt); } else { typeindex = attrtypes[i]->atttypid - FIRST_TYPE_OID; if (DebugMode) printf("Typ == NULL, typeindex = %u idx = %d\n", typeindex, i); values[i] = fmgr(Procid[typeindex].inproc, value, Procid[typeindex].elem, -1); prt = fmgr(Procid[typeindex].outproc, values[i], Procid[typeindex].elem); if (!Quiet) printf("%s ", prt); pfree(prt); } if (DebugMode) { puts("End InsertValue"); fflush(stdout); }}/* ---------------- * InsertOneNull * ---------------- */voidInsertOneNull(int i){ if (DebugMode) printf("Inserting null\n"); if (i < 0 || i >= MAXATTR) elog(FATAL, "i out of range (too many attrs): %d\n", i); values[i] = (char *) NULL; Blanks[i] = 'n';}#define MORE_THAN_THE_NUMBER_OF_CATALOGS 256static boolBootstrapAlreadySeen(Oid id){ static Oid seenArray[MORE_THAN_THE_NUMBER_OF_CATALOGS]; static int nseen = 0; bool seenthis; int i; seenthis = false; for (i = 0; i < nseen; i++) { if (seenArray[i] == id) { seenthis = true; break; } } if (!seenthis) { seenArray[nseen] = id; nseen++; } return seenthis;}/* ---------------- * cleanup * ---------------- */static voidcleanup(){ static int beenhere = 0; if (!beenhere) beenhere = 1; else { elog(FATAL, "Memory manager fault: cleanup called twice.\n", stderr); proc_exit(1); } if (reldesc != (Relation) NULL) heap_close(reldesc); CommitTransactionCommand(); proc_exit(Warnings);}/* ---------------- * gettype * ---------------- */static Oidgettype(char *type){ int i; Relation rel; HeapScanDesc scan; HeapTuple tup; struct typmap **app; if (Typ != (struct typmap **) NULL) { for (app = Typ; *app != (struct typmap *) NULL; app++) { if (strncmp((*app)->am_typ.typname.data, type, NAMEDATALEN) == 0) { Ap = *app; return (*app)->am_oid; } } } else { for (i = 0; i <= n_types; i++) { if (strncmp(type, Procid[i].name, NAMEDATALEN) == 0) return i; } if (DebugMode) printf("bootstrap.c: External Type: %s\n", type); rel = heap_openr(TypeRelationName); scan = heap_beginscan(rel, 0, SnapshotNow, 0, (ScanKey) NULL); i = 0; while (HeapTupleIsValid(tup = heap_getnext(scan, 0))) ++i; heap_endscan(scan); app = Typ = ALLOC(struct typmap *, i + 1); while (i-- > 0) *app++ = ALLOC(struct typmap, 1); *app = (struct typmap *) NULL; scan = heap_beginscan(rel, 0, SnapshotNow, 0, (ScanKey) NULL); app = Typ; while (HeapTupleIsValid(tup = heap_getnext(scan, 0))) { (*app)->am_oid = tup->t_data->t_oid; memmove((char *) &(*app++)->am_typ, (char *) GETSTRUCT(tup), sizeof((*app)->am_typ)); } heap_endscan(scan); heap_close(rel); return gettype(type); } elog(ERROR, "Error: unknown type '%s'.\n", type); err_out(); /* not reached, here to make compiler happy */ return 0;}/* ---------------- * AllocateAttribute * ---------------- */static Form_pg_attribute /* XXX */AllocateAttribute(){ Form_pg_attribute attribute = (Form_pg_attribute) malloc(ATTRIBUTE_TUPLE_SIZE); if (!PointerIsValid(attribute)) elog(FATAL, "AllocateAttribute: malloc failed"); MemSet(attribute, 0, ATTRIBUTE_TUPLE_SIZE); return attribute;}/* ---------------- * MapArrayTypeName * XXX arrays of "basetype" are always "_basetype". * this is an evil hack inherited from rel. 3.1. * XXX array dimension is thrown away because we * don't support fixed-dimension arrays. again, * sickness from 3.1. * * the string passed in must have a '[' character in it * * the string returned is a pointer to static storage and should NOT * be freed by the CALLER. * ---------------- */char *MapArrayTypeName(char *s){ int i, j; static char newStr[NAMEDATALEN]; /* array type names < NAMEDATALEN * long */ if (s == NULL || s[0] == '\0') return s; j = 1; newStr[0] = '_'; for (i = 0; i < NAMEDATALEN - 1 && s[i] != '['; i++, j++) newStr[j] = s[i]; newStr[j] = '\0'; return newStr;}/* ---------------- * EnterString * returns the string table position of the identifier * passed to it. We add it to the table if we can't find it. * ---------------- */intEnterString(char *str){ hashnode *node; int len; len = strlen(str); node = FindStr(str, len, 0); if (node) return node->strnum; else { node = AddStr(str, len, 0); return node->strnum; }}/* ---------------- * LexIDStr * when given an idnum into the 'string-table' return the string * associated with the idnum * ---------------- */char *LexIDStr(int ident_num){ return strtable[ident_num];}/* ---------------- * CompHash * * Compute a hash function for a given string. We look at the first, * the last, and the middle character of a string to try to get spread * the strings out. The function is rather arbitrary, except that we * are mod'ing by a prime number. * ---------------- */static intCompHash(char *str, int len){ int result; result = (NUM * str[0] + NUMSQR * str[len - 1] + NUMCUBE * str[(len - 1) / 2]); return result % HASHTABLESIZE;}/* ---------------- * FindStr * * This routine looks for the specified string in the hash * table. It returns a pointer to the hash node found, * or NULL if the string is not in the table. * ---------------- */static hashnode *FindStr(char *str, int length, hashnode *mderef){ hashnode *node; node = hashtable[CompHash(str, length)]; while (node != NULL) { /* * We must differentiate between string constants that might have * the same value as a identifier and the identifier itself. */ if (!strcmp(str, strtable[node->strnum])) { return node; /* no need to check */ } else node = node->next; } /* Couldn't find it in the list */ return NULL;}/* ---------------- * AddStr * * This function adds the specified string, along with its associated * data, to the hash table and the string table. We return the node * so that the calling routine can find out the unique id that AddStr * has assigned to this string. * ---------------- */static hashnode *AddStr(char *str, int strlength, int mderef){ hashnode *temp, *trail, *newnode; int hashresult; int len; if (++strtable_end == STRTABLESIZE) { /* Error, string table overflow, so we Punt */ elog(FATAL, "There are too many string constants and identifiers for the compiler to handle."); } /* * Some of the utilites (eg, define type, create relation) assume that * the string they're passed is a NAMEDATALEN. We get array bound * read violations from purify if we don't allocate at least * NAMEDATALEN bytes for strings of this sort. Because we're lazy, we * allocate at least NAMEDATALEN bytes all the time. */ if ((len = strlength + 1) < NAMEDATALEN) len = NAMEDATALEN; strtable[strtable_end] = malloc((unsigned) len); strcpy(strtable[strtable_end], str); /* Now put a node in the hash table */ newnode = (hashnode *) malloc(sizeof(hashnode) * 1); newnode->strnum = strtable_end; newnode->next = NULL; /* Find out where it goes */ hashresult = CompHash(str, strlength); if (hashtable[hashresult] == NULL) hashtable[hashresult] = newnode; else { /* There is something in the list */ trail = hashtable[hashresult]; temp = trail->next; while (temp != NULL) { trail = temp; temp = temp->next; } trail->next = newnode; } return newnode;}/* * index_register() -- record an index that has been set up for building * later. * * At bootstrap time, we define a bunch of indices on system catalogs. * We postpone actually building the indices until just before we're * finished with initialization, however. This is because more classes * and indices may be defined, and we want to be sure that all of them * are present in the index. */voidindex_register(char *heap, char *ind, int natts, AttrNumber *attnos, uint16 nparams, Datum *params, FuncIndexInfo *finfo, PredInfo *predInfo){ Datum *v; IndexList *newind; int len; MemoryContext oldcxt; /* * XXX mao 10/31/92 -- don't gc index reldescs, associated info at * bootstrap time. we'll declare the indices now, but want to create * them later. */ if (nogc == (GlobalMemory) NULL) nogc = CreateGlobalMemory("BootstrapNoGC"); oldcxt = MemoryContextSwitchTo((MemoryContext) nogc); newind = (IndexList *) palloc(sizeof(IndexList)); newind->il_heap = pstrdup(heap); newind->il_ind = pstrdup(ind); newind->il_natts = natts; if (PointerIsValid(finfo)) len = FIgetnArgs(finfo) * sizeof(AttrNumber); else len = natts * sizeof(AttrNumber); newind->il_attnos = (AttrNumber *) palloc(len); memmove(newind->il_attnos, attnos, len); if ((newind->il_nparams = nparams) > 0) { v = newind->il_params = (Datum *) palloc(2 * nparams * sizeof(Datum)); nparams *= 2; while (nparams-- > 0) { *v = (Datum) palloc(strlen((char *) (*params)) + 1); strcpy((char *) *v++, (char *) *params++); } } else newind->il_params = (Datum *) NULL; if (finfo != (FuncIndexInfo *) NULL) { newind->il_finfo = (FuncIndexInfo *) palloc(sizeof(FuncIndexInfo)); memmove(newind->il_finfo, finfo, sizeof(FuncIndexInfo)); } else newind->il_finfo = (FuncIndexInfo *) NULL; if (predInfo != NULL) { newind->il_predInfo = (PredInfo *) palloc(sizeof(PredInfo)); newind->il_predInfo->pred = predInfo->pred; newind->il_predInfo->oldPred = predInfo->oldPred; } else newind->il_predInfo = NULL; newind->il_next = ILHead; ILHead = newind; MemoryContextSwitchTo(oldcxt);}voidbuild_indices(){ Relation heap; Relation ind; for (; ILHead != (IndexList *) NULL; ILHead = ILHead->il_next) { heap = heap_openr(ILHead->il_heap); ind = index_openr(ILHead->il_ind); index_build(heap, ind, ILHead->il_natts, ILHead->il_attnos, ILHead->il_nparams, ILHead->il_params, ILHead->il_finfo, ILHead->il_predInfo); /* * All of the rest of this routine is needed only because in * bootstrap processing we don't increment xact id's. The normal * DefineIndex code replaces a pg_class tuple with updated info * including the relhasindex flag (which we need to have updated). * Unfortunately, there are always two indices defined on each * catalog causing us to update the same pg_class tuple twice for * each catalog getting an index during bootstrap resulting in the * ghost tuple problem (see heap_replace). To get around this we * change the relhasindex field ourselves in this routine keeping * track of what catalogs we already changed so that we don't * modify those tuples twice. The normal mechanism for updating * pg_class is disabled during bootstrap. * * -mer */ heap = heap_openr(ILHead->il_heap); if (!BootstrapAlreadySeen(RelationGetRelid(heap))) UpdateStats(RelationGetRelid(heap), 0, true); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -