about heap_insert() function
Hi,
Let me ask you some questions about heap_insert().
I'm trying to insert tuples into a existing table by using intenal
functions
bypassing the query executor.
For example, let's assume that the table name is "MyTable" having a Box
type attribute.
And I want to insert a box '((1,1),(2,2))' into the table.
The following is an internal function call sequence that I think I
should
follow.
Does it make sense?
{
Relation relation;
Oid oid;
HeapTuple tup;
Box box;
r = heap_open("MyTable", RowExclusiveLock);
box.high.x = 2;
box.high.y = 2;
box.low.x = 1;
box.low.y = 1;
..........................
oid = insert_heap(r, tup);
heap_close(r, NoLock)
}
Now, what I don't know is how to set values to HeapTuple tup.
The following data structure shows the fields that I need to fill in.
I found some fields are set by heap_insert() function, and I marked
with "&". But I have still some fields needing to be set. Especilly,
I have no idea on what I need to set for a box, the actual data which is
going to be stored on PostgreSQL's database.
Does anybody know a good example concerning my question?typedef struct
HeapTupleHeaderData
{
Oid t_oid; /* & OID of this tuple -- 4
bytes */
CommandId t_cmin; /* & insert CID stamp -- 4 bytes
each */
CommandId t_cmax; /* delete CommandId stamp */
TransactionId t_xmin; /* & insert XID stamp -- 4 bytes
each */
TransactionId t_xmax; /* & delete XID stamp */
ItemPointerData t_ctid; /* current TID of this or
newer tuple */
int16 t_natts; /* number of attributes */
uint16 t_infomask; /* & various infos */
uint8 t_hoff; /* sizeof() tuple header */
bits8 t_bits[MinHeapTupleBitmapSize / 8];
} HeapTupleHeaderData;
typedef struct HeapTupleData
{
uint32 t_len; /* length of *t_data */
ItemPointerData t_self; /* SelfItemPointer */
Oid t_tableOid; /* & table the tuple came from
*/
MemoryContext t_datamcxt; /* memory context of
allocation */
HeapTupleHeader t_data; /* -> tuple header and data */
} HeapTupleData;
Cheers.
Seung-Hyun Jeong <jeongs@cs.man.ac.uk> writes:
I'm trying to insert tuples into a existing table by using intenal
functions bypassing the query executor.
Now, what I don't know is how to set values to HeapTuple tup.
The following data structure shows the fields that I need to fill in.
You shouldn't be touching any of that directly. You want to use
heap_formtuple to build the tuple. All you need for that is a tuple
descriptor (which you get from the open relation), and two arrays of
Datum values and null flags.
regards, tom lane