由于篇幅限制,我将提供一个核心函数的简要描述,该函数展示了如何在PostgreSQL中使用堆存储管理器(heapam)进行基本的元组插入操作。
/*
* heap_insert() -- insert a tuple
*
* The new tuple is stamped with the current transaction XID and the
* specified command ID.
*
* After the tuple is inserted, an XLOG INSERT record is logged, unless
* the new tuple is being inserted in a rerun transaction. The XLOG record
* includes the new tuple's XID and CID, as well as info about the relation
* and a log image of the new tuple.
*
* It is assumed that the caller has made provision for any necessary
* permission checks.
*/
HeapTuple
heap_insert(Relation relation, HeapTuple tup, CommandId cid,
int options, BulkInsertState bistate)
{
TransactionId xid = GetCurrentTransactionId();
HeapTuple heapTuple;
Buffer buffer;
Buffer vmbuffer = InvalidBuffer;
Page page;
OffsetNumber offnum;
ItemId itemId;
bool need_tuple_routine = false;
/*
* Fill in tuple header fields.
*/
heapTuple = heap_prepare_insert(relation, tup, xid, cid, options);
/*
* Find buffer to insert this tuple into. If the insert is requested to
* go into the HOT chain, we'll try to put it there. Otherwise, we
* always put new tuples at the end of the disk file.
*/
buffer = RelationGetBufferForTuple(relation, heapTuple,
InvalidBuffer, options,
bistate, &vmbuffer, &offnum);
/*
* Now, do the actual insertion.
*/
START_CRIT_SECTION();
RelationPutHeapTuple(relation, buffer, offnum, heapTuple,
true /* new tuple */ ,
options);
if (vmbuffer != InvalidBuffer)
{
/*
* There is no need to register the buffer for the visibility map
* here, because we haven't inserted the tuple yet. The visibility
* map bit will be updated when the transaction commits, as usual.
*/
ReleaseBuffer(vmbuffer);