Datum caql_insert_to_in_memory_pg_attribute(PG_FUNCTION_ARGS) { Oid attrelid = PG_GETARG_OID(0); char *attname = text_to_cstring(PG_GETARG_TEXT_P(1)); AttrNumber attno = PG_GETARG_INT16(2); cqContext *pcqCtx = caql_beginscan( NULL, cql("INSERT INTO pg_attribute", NULL)); FormData_pg_attribute attributeD; HeapTuple attributeTuple = heap_addheader(Natts_pg_attribute, false, ATTRIBUTE_TUPLE_SIZE, (void *) &attributeD); Form_pg_attribute attribute = (Form_pg_attribute) GETSTRUCT(attributeTuple); attribute->attrelid = attrelid; namestrcpy(&(attribute->attname), attname); attribute->attnum = attno; caql_insert_inmem(pcqCtx, attributeTuple); caql_endscan(pcqCtx); StringInfoData buf; initStringInfo(&buf); appendStringInfo(&buf, "inserted tuple to pg_attribute (attrelid %d, attname %s, attno %d)", attribute->attrelid, NameStr(attribute->attname), attribute->attnum); PG_RETURN_TEXT_P(cstring_to_text(buf.data)); }
/* ---------------------------------------------------------------- * UpdateRelationRelation * ---------------------------------------------------------------- */ static void UpdateRelationRelation(Relation indexRelation) { Relation pg_class; HeapTuple tuple; pg_class = heap_openr(RelationRelationName, RowExclusiveLock); /* XXX Natts_pg_class_fixed is a hack - see pg_class.h */ tuple = heap_addheader(Natts_pg_class_fixed, true, CLASS_TUPLE_SIZE, (void *) indexRelation->rd_rel); /* * the new tuple must have the oid already chosen for the index. sure * would be embarrassing to do this sort of thing in polite company. */ HeapTupleSetOid(tuple, RelationGetRelid(indexRelation)); simple_heap_insert(pg_class, tuple); /* update the system catalog indexes */ CatalogUpdateIndexes(pg_class, tuple); heap_freetuple(tuple); heap_close(pg_class, RowExclusiveLock); }
/* ---------------------------------------------------------------- * AppendAttributeTuples * ---------------------------------------------------------------- */ static void AppendAttributeTuples(Relation indexRelation, int numatts) { Relation pg_attribute; CatalogIndexState indstate; TupleDesc indexTupDesc; HeapTuple new_tuple; int i; /* * open the attribute relation and its indexes */ pg_attribute = heap_openr(AttributeRelationName, RowExclusiveLock); indstate = CatalogOpenIndexes(pg_attribute); /* * insert data from new index's tupdesc into pg_attribute */ indexTupDesc = RelationGetDescr(indexRelation); for (i = 0; i < numatts; i++) { /* * There used to be very grotty code here to set these fields, but * I think it's unnecessary. They should be set already. */ Assert(indexTupDesc->attrs[i]->attnum == i + 1); Assert(indexTupDesc->attrs[i]->attcacheoff == -1); new_tuple = heap_addheader(Natts_pg_attribute, false, ATTRIBUTE_TUPLE_SIZE, (void *) indexTupDesc->attrs[i]); simple_heap_insert(pg_attribute, new_tuple); CatalogIndexInsert(indstate, new_tuple); heap_freetuple(new_tuple); } CatalogCloseIndexes(indstate); heap_close(pg_attribute, RowExclusiveLock); }