Database Reference
In-Depth Information
is element pass-by-value
element alignment id
The type
OID
for
int4 (=23)
is already conveniently defined as
INT4OID
, the oth-
ers you just have to look up.
The easiest way to get the values for
type
,
size
,
passbyvalue
, and
alignment
is to query these from the database.
c_samples=# select oid, typlen, typbyval,
typalign from pg_type
c_samples-# where typname = 'int4';
-[ RECORD 1 ]
oid | 23
typlen | 4
typbyval | t
typalign | i
After the call to
deconstruct_array(...)
the rest is easy—just iterate over the
value and null arrays and accumulate the sum:
for(i=0;i<count;i++) {
// first check and ignore null elements
if ( nulls[i] )
continue;
// accumulate and remember there were
non-null values
sum +=
DatumGetInt32(
datums[i]);
not_null = true;
}
The only PostgreSQL-specific thing here is the use of the
DatumGetInt32(<datum>)
macro for converting the
Datum
to
integer
. The
DatumGetInt32(<datum>)
macro performs no checking of its argument to verify
that it is indeed an integer (this is C remember, so no type info is available in data
itself), but using the
DatumGet*()
macro helps us to make the compiler happy.