📄 heaptuple.c
字号:
{ off = att_align(off, att[i]->attalign); if (usecache) att[i]->attcacheoff = off; } off = att_addlength(off, att[i]->attlen, tp + off); if (usecache && att[i]->attlen <= 0) usecache = false; } off = att_align(off, att[attnum]->attalign); return fetchatt(att[attnum], tp + off); }}/* ---------------- * heap_getsysattr * * Fetch the value of a system attribute for a tuple. * * This is a support routine for the heap_getattr macro. The macro * has already determined that the attnum refers to a system attribute. * ---------------- */Datumheap_getsysattr(HeapTuple tup, int attnum, bool *isnull){ Datum result; Assert(tup); /* Currently, no sys attribute ever reads as NULL. */ if (isnull) *isnull = false; switch (attnum) { case SelfItemPointerAttributeNumber: /* pass-by-reference datatype */ result = PointerGetDatum(&(tup->t_self)); break; case ObjectIdAttributeNumber: result = ObjectIdGetDatum(HeapTupleGetOid(tup)); break; case MinTransactionIdAttributeNumber: result = TransactionIdGetDatum(HeapTupleHeaderGetXmin(tup->t_data)); break; case MinCommandIdAttributeNumber: result = CommandIdGetDatum(HeapTupleHeaderGetCmin(tup->t_data)); break; case MaxTransactionIdAttributeNumber: result = TransactionIdGetDatum(HeapTupleHeaderGetXmax(tup->t_data)); break; case MaxCommandIdAttributeNumber: result = CommandIdGetDatum(HeapTupleHeaderGetCmax(tup->t_data)); break; case TableOidAttributeNumber: result = ObjectIdGetDatum(tup->t_tableOid); break; default: elog(ERROR, "invalid attnum: %d", attnum); result = 0; /* keep compiler quiet */ break; } return result;}/* ---------------- * heap_copytuple * * returns a copy of an entire tuple * * The HeapTuple struct, tuple header, and tuple data are all allocated * as a single palloc() block. * ---------------- */HeapTupleheap_copytuple(HeapTuple tuple){ HeapTuple newTuple; if (!HeapTupleIsValid(tuple) || tuple->t_data == NULL) return NULL; newTuple = (HeapTuple) palloc(HEAPTUPLESIZE + tuple->t_len); newTuple->t_len = tuple->t_len; newTuple->t_self = tuple->t_self; newTuple->t_tableOid = tuple->t_tableOid; newTuple->t_datamcxt = CurrentMemoryContext; newTuple->t_data = (HeapTupleHeader) ((char *) newTuple + HEAPTUPLESIZE); memcpy((char *) newTuple->t_data, (char *) tuple->t_data, tuple->t_len); return newTuple;}/* ---------------- * heap_copytuple_with_tuple * * copy a tuple into a caller-supplied HeapTuple management struct * ---------------- */voidheap_copytuple_with_tuple(HeapTuple src, HeapTuple dest){ if (!HeapTupleIsValid(src) || src->t_data == NULL) { dest->t_data = NULL; return; } dest->t_len = src->t_len; dest->t_self = src->t_self; dest->t_tableOid = src->t_tableOid; dest->t_datamcxt = CurrentMemoryContext; dest->t_data = (HeapTupleHeader) palloc(src->t_len); memcpy((char *) dest->t_data, (char *) src->t_data, src->t_len);}#ifdef NOT_USED/* ---------------- * heap_deformtuple * * the inverse of heap_formtuple (see below) * ---------------- */voidheap_deformtuple(HeapTuple tuple, TupleDesc tdesc, Datum *values, char *nulls){ int i; int natts; Assert(HeapTupleIsValid(tuple)); natts = tuple->t_natts; for (i = 0; i < natts; i++) { bool isnull; values[i] = heap_getattr(tuple, i + 1, tdesc, &isnull); if (isnull) nulls[i] = 'n'; else nulls[i] = ' '; }}#endif/* ---------------- * heap_formtuple * * constructs a tuple from the given *value and *null arrays * * old comments * Handles alignment by aligning 2 byte attributes on short boundries * and 3 or 4 byte attributes on long word boundries on a vax; and * aligning non-byte attributes on short boundries on a sun. Does * not properly align fixed length arrays of 1 or 2 byte types (yet). * * Null attributes are indicated by a 'n' in the appropriate byte * of the *null. Non-null attributes are indicated by a ' ' (space). * * Fix me. (Figure that must keep context if debug--allow give oid.) * Assumes in order. * ---------------- */HeapTupleheap_formtuple(TupleDesc tupleDescriptor, Datum *value, char *nulls){ HeapTuple tuple; /* return tuple */ HeapTupleHeader td; /* tuple data */ unsigned long len; int hoff; bool hasnull = false; int i; int numberOfAttributes = tupleDescriptor->natts; if (numberOfAttributes > MaxTupleAttributeNumber) ereport(ERROR, (errcode(ERRCODE_TOO_MANY_COLUMNS), errmsg("number of columns (%d) exceeds limit (%d)", numberOfAttributes, MaxTupleAttributeNumber))); for (i = 0; i < numberOfAttributes; i++) { if (nulls[i] != ' ') { hasnull = true; break; } } len = offsetof(HeapTupleHeaderData, t_bits); if (hasnull) len += BITMAPLEN(numberOfAttributes); if (tupleDescriptor->tdhasoid) len += sizeof(Oid); hoff = len = MAXALIGN(len); /* align user data safely */ len += ComputeDataSize(tupleDescriptor, value, nulls); tuple = (HeapTuple) palloc(HEAPTUPLESIZE + len); tuple->t_datamcxt = CurrentMemoryContext; td = tuple->t_data = (HeapTupleHeader) ((char *) tuple + HEAPTUPLESIZE); MemSet((char *) td, 0, len); tuple->t_len = len; ItemPointerSetInvalid(&(tuple->t_self)); tuple->t_tableOid = InvalidOid; td->t_natts = numberOfAttributes; td->t_hoff = hoff; if (tupleDescriptor->tdhasoid) /* else leave infomask = 0 */ td->t_infomask = HEAP_HASOID; DataFill((char *) td + hoff, tupleDescriptor, value, nulls, &td->t_infomask, (hasnull ? td->t_bits : NULL)); return tuple;}/* ---------------- * heap_modifytuple * * forms a new tuple from an old tuple and a set of replacement values. * returns a new palloc'ed tuple. * ---------------- */HeapTupleheap_modifytuple(HeapTuple tuple, Relation relation, Datum *replValue, char *replNull, char *repl){ int attoff; int numberOfAttributes; Datum *value; char *nulls; bool isNull; HeapTuple newTuple; /* * sanity checks */ Assert(HeapTupleIsValid(tuple)); Assert(RelationIsValid(relation)); Assert(PointerIsValid(replValue)); Assert(PointerIsValid(replNull)); Assert(PointerIsValid(repl)); numberOfAttributes = RelationGetForm(relation)->relnatts; /* * allocate and fill *value and *nulls arrays from either the tuple or * the repl information, as appropriate. */ value = (Datum *) palloc(numberOfAttributes * sizeof(Datum)); nulls = (char *) palloc(numberOfAttributes * sizeof(char)); for (attoff = 0; attoff < numberOfAttributes; attoff++) { if (repl[attoff] == ' ') { value[attoff] = heap_getattr(tuple, AttrOffsetGetAttrNumber(attoff), RelationGetDescr(relation), &isNull); nulls[attoff] = (isNull) ? 'n' : ' '; } else if (repl[attoff] == 'r') { value[attoff] = replValue[attoff]; nulls[attoff] = replNull[attoff]; } else elog(ERROR, "unrecognized replace flag: %d", (int) repl[attoff]); } /* * create a new tuple from the *values and *nulls arrays */ newTuple = heap_formtuple(RelationGetDescr(relation), value, nulls); pfree(value); pfree(nulls); /* * copy the identification info of the old tuple: t_ctid, t_self, and * OID (if any) */ newTuple->t_data->t_ctid = tuple->t_data->t_ctid; newTuple->t_self = tuple->t_self; newTuple->t_tableOid = tuple->t_tableOid; if (relation->rd_rel->relhasoids) HeapTupleSetOid(newTuple, HeapTupleGetOid(tuple)); return newTuple;}/* ---------------- * heap_freetuple * ---------------- */voidheap_freetuple(HeapTuple htup){ if (htup->t_data != NULL) if (htup->t_datamcxt != NULL && (char *) (htup->t_data) != ((char *) htup + HEAPTUPLESIZE)) pfree(htup->t_data); pfree(htup);}/* ---------------- * heap_addheader * * This routine forms a HeapTuple by copying the given structure (tuple * data) and adding a generic header. Note that the tuple data is * presumed to contain no null fields and no varlena fields. * * This routine is really only useful for certain system tables that are * known to be fixed-width and null-free. It is used in some places for * pg_class, but that is a gross hack (it only works because relacl can * be omitted from the tuple entirely in those places). * ---------------- */HeapTupleheap_addheader(int natts, /* max domain index */ bool withoid, /* reserve space for oid */ Size structlen, /* its length */ void *structure) /* pointer to the struct */{ HeapTuple tuple; HeapTupleHeader td; Size len; int hoff; AssertArg(natts > 0); /* header needs no null bitmap */ hoff = offsetof(HeapTupleHeaderData, t_bits); if (withoid) hoff += sizeof(Oid); hoff = MAXALIGN(hoff); len = hoff + structlen; tuple = (HeapTuple) palloc(HEAPTUPLESIZE + len); tuple->t_len = len; ItemPointerSetInvalid(&(tuple->t_self)); tuple->t_tableOid = InvalidOid; tuple->t_datamcxt = CurrentMemoryContext; tuple->t_data = td = (HeapTupleHeader) ((char *) tuple + HEAPTUPLESIZE); MemSet((char *) td, 0, hoff); td->t_natts = natts; td->t_hoff = hoff; if (withoid) /* else leave infomask = 0 */ td->t_infomask = HEAP_HASOID; memcpy((char *) td + hoff, structure, structlen); return tuple;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -