Writing and Reading bytea

From: Amit Gupta <amit(dot)pc(dot)gupta(at)gmail(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Cc: Robert Haas <robertmhaas(at)gmail(dot)com>, Emmanuel Cecchet <manu(at)frogthinker(dot)org>
Subject: Writing and Reading bytea
Date: 2009-02-12 15:50:28
Message-ID: 8d79a95c0902120750s402f6ce1j1661d8513c1a6b5f@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

We need insert rows to a catalog table that would store partitions info:

CATALOG(pg_partition,2336) BKI_WITHOUT_OIDS
{
Oid partrelid; /* partition table Oid */
Oid parentrelid; /* Parent table Oid */
int2 parttype; /* Type of partition, list, hash, range */
int2 partkey; /* partition key */
Oid keytype; /* type of partition key */
int2 keyorder; /* order of the key in multi-key partitions */
bytea minval;
bytea maxval; /* min and max for range partition */
bytea listval;
int2 hashval; /* hash value */
} FormData_pg_partition;

The following code is used to write bytea:
...
min_ba = (bytea *) palloc(len+VARHDRSZ);
memcpy(VARDATA(min_ba), &b_min, len);
SET_VARSIZE(min_ba, len+VARHDRSZ);
values[Anum_pg_partition_minval -1]= (Datum)min_ba ;
...
Relation r = heap_open(PartitionRelationId, RowExclusiveLock);
TupleDesc tupDesc = r->rd_att;
HeapTuple tup = heap_form_tuple(tupDesc, values, nulls);
simple_heap_insert(r, tup);
CatalogUpdateIndexes(r, tup);
heap_close(r, RowExclusiveLock);

We can see the correct bytes in the pg_partition table after exectuing
the above code. However, retrieving the bytea datatypes seems
problematic.
The following code didn't work:
....
pg_partrel = heap_open(PartitionRelationId, AccessShareLock);
pg_partscan = systable_beginscan(pg_partrel, PartitionParentIndexId, true,
SnapshotNow, 1, &skey);
while (HeapTupleIsValid(pg_parttup= systable_getnext(pg_partscan)))
{
Form_pg_partition pg_part = (Form_pg_partition) GETSTRUCT(pg_parttup);
Datum attr = heap_getattr(tuple, pg_part->partkey, rel->rd_att, &isnull)
Datum min_part_attr = (Datum) (&pg_part->minval);
Datum max_part_attr = (Datum) (&pg_part->maxval);
......

}

max_part_attr is not poining to right mem location. After doing some
investgation, we found since minval extends to 10 bytes (instead of 5
bytes occupied by struct varlena), max_part_attr value is not correct.
We also tried doing a hack:
max_ part_attr = (Datum)
(((void*)(&pg_part->minval))+VARSIZE_ANY(&pg_part->minval));

but still we are facing problems.

Any pointers in this respect will be helpful.

Thanks,
Amit
Persistent Systems

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Heikki Linnakangas 2009-02-12 16:01:49 Re: Writing and Reading bytea
Previous Message Tom Lane 2009-02-12 15:44:59 Re: some questions about SELECT FOR UPDATE/SHARE