Re: libpq binary transfer of the numeric data type -

From: Brijesh Shrivastav <Bshrivastav(at)esri(dot)com>
To: "'Tom Lane'" <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Brijesh Shrivastav <Bshrivastav(at)esri(dot)com>
Cc: pgsql-interfaces(at)postgresql(dot)org
Subject: Re: libpq binary transfer of the numeric data type -
Date: 2004-08-03 00:29:26
Message-ID: 491DC5F3D279CD4EB4B157DDD62237F404E27FEF@zipwire.esri.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-interfaces

This is the part I am not clear. It seems the numeric_recv function
read int16s as you descibed below but returns Numeric structure.
What do we get from PQgetvalue(), a Numeric object or a series of
int16s followed by array of numeric digits as sent in numeric_send()
routine?

numeric_recv(PG_FUNCTION_ARGS)
{
StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
NumericVar value;
Numeric res;
int len,
i;

init_var(&value);

len = (uint16) pq_getmsgint(buf, sizeof(uint16));
if (len < 0 || len > NUMERIC_MAX_PRECISION +
NUMERIC_MAX_RESULT_SCALE)
ereport(ERROR,

(errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
errmsg("invalid length in external
\"numeric\" value")));

alloc_var(&value, len);

value.weight = (int16) pq_getmsgint(buf, sizeof(int16));
value.sign = (uint16) pq_getmsgint(buf, sizeof(uint16));
if (!(value.sign == NUMERIC_POS ||
value.sign == NUMERIC_NEG ||
value.sign == NUMERIC_NAN))
ereport(ERROR,

(errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
errmsg("invalid sign in external
\"numeric\" value")));

value.dscale = (uint16) pq_getmsgint(buf, sizeof(uint16));
for (i = 0; i < len; i++)
{
NumericDigit d = pq_getmsgint(buf, sizeof(NumericDigit));

if (d < 0 || d >= NBASE)
ereport(ERROR,

(errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
errmsg("invalid digit in external
\"numeric\" value")));
value.digits[i] = d;
}

res = make_result(&value);
free_var(&value);

PG_RETURN_NUMERIC(res);
}

warm regards,
Brijesh
-----Original Message-----
From: Tom Lane [mailto:tgl(at)sss(dot)pgh(dot)pa(dot)us]
Sent: Monday, August 02, 2004 4:35 PM
To: Brijesh Shrivastav
Cc: pgsql-interfaces(at)postgresql(dot)org
Subject: Re: [INTERFACES] libpq binary transfer of the numeric data type
- II

Brijesh Shrivastav <Bshrivastav(at)esri(dot)com> writes:
> Has anyone had success with fetching numeric data in binary format?
> I tried to follow Tom's advice below and try to reverse engineer from
> recv/send function. It seemes the end structure that user will
> get is of Numeric type (see struct below)

No, it isn't. What you get is a series of int16 fields:

pq_sendint(&buf, x.ndigits, sizeof(int16));
pq_sendint(&buf, x.weight, sizeof(int16));
pq_sendint(&buf, x.sign, sizeof(int16));
pq_sendint(&buf, x.dscale, sizeof(int16));
for (i = 0; i < x.ndigits; i++)
pq_sendint(&buf, x.digits[i], sizeof(NumericDigit));

Note that the "digits" are base-10000 digits.

regards, tom lane

Browse pgsql-interfaces by date

  From Date Subject
Next Message Alex Ott 2004-08-03 05:48:50 working with big text fields
Previous Message Tom Lane 2004-08-02 23:35:20 Re: libpq binary transfer of the numeric data type - II