btree index (1)

Indexがあるテーブルにinsert文を実行すると、indexに登録するためにbtinsertが実行される。

#0  btinsert (fcinfo=0xbffeb8a0) at nbtree.c:225
#1  0x08289103 in FunctionCall6 (flinfo=0x83f10b0, arg1=1099692112,
    arg2=3221142352, arg3=3221142320, arg4=138354180, arg5=1099690592, arg6=1)
    at fmgr.c:1270
#2  0x08098a19 in index_insert (indexRelation=0x418bf850, values=0xbffebb50,
    isnull=0xbffebb30 "", heap_t_ctid=0x83f1e04, heapRelation=0x418bf260,
    check_uniqueness=1 '\001') at indexam.c:215
#3  0x0815ce3c in ExecInsertIndexTuples (slot=0x83f1a98, tupleid=0x83f1e04,
    estate=0x83f18f0, is_vacuum=0 '\0') at execUtils.c:938
#4  0x08153709 in ExecInsert (slot=0x83f1a98, tupleid=0x0, estate=0x83f18f0)
    at execMain.c:1445
#5  0x0815341d in ExecutePlan (estate=0x83f18f0, planstate=0x83f1b18,
    operation=CMD_INSERT, numberTuples=0, direction=ForwardScanDirection,
    dest=0x83cc528) at execMain.c:1267
#6  0x081520d8 in ExecutorRun (queryDesc=0x83f1510,
    direction=ForwardScanDirection, count=0) at execMain.c:231
#7  0x081f7a77 in ProcessQuery (parsetree=0x83cbfa0, plan=0x83cc478,
    params=0x0, dest=0x83cc528, completionTag=0xbffebf00 "") at pquery.c:174

bt_insert

(1)index_form_tupleでdatum/nulls配列からIndexTupleを作成する。
(2)_bt_formitemでIndexTupleからBTItemを作成する。
(3)_bt_doinsertでBTItemをb-tree indexに登録
(4)IndexTupleとBTItemのメモリを開放。

index_form_tuple

Index用のTupleDescと,values/isnull配列を使って,Index用のタプルを作成する。Index用のタプルの構造は,普通のテーブル用のタプルと似ている。

_bt_formitem

IndexTupleからBTItemを作成する。
(1)IndexTupleのサイズからBTItemに必要な大きさを計算する。
(2)pallocでBTItem用のメモリを確保
(3)memcpyでbtitem->bti_itupにIndexTupleをコピー

BTItemのメンバはIndexTupleしかないため,IndexTupleとBTItemのデータ構造はまったく同じ。_bt_formitemでメモリを確保してコピーする意味がない(キャストすればいい)が,将来の拡張のためにこの処理が残してあるらしい。

typedef struct BTItemData
{
	IndexTupleData bti_itup;
} BTItemData;

typedef BTItemData *BTItem;