pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG?  select count(*) from table   don't get value via PQgetvalue() function in libpq(C)
2+ messages / 2 participants
[nested] [flat]

* BUG?  select count(*) from table   don't get value via PQgetvalue() function in libpq(C)
@ 2017-02-18 16:04 Gao Yanxiao <553216793@qq.com>
  2017-02-18 17:58 ` Re: BUG? select count(*) from table don't get value via PQgetvalue() function in libpq(C) Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 2+ messages in thread

From: Gao Yanxiao @ 2017-02-18 16:04 UTC (permalink / raw)
  To: pgsql-bugs

Hi, postgresql:

       First, my englist is not good. So I try to my best. Hope you can understand me.

 

       I programing a program use linux via C language. And I used libpq connect to postgresql.

       Then I want calculate rows in a table. So I write PQexecparams() perfrom sql statement: select count(*) from tablename. 

Like this:

    PGresult *res;

    PQtrace(pgconn, stdout);

    res = PQexecParams(pgconn, "select count(*) from chat_connstate", 0, NULL, NULL, NULL, NULL, 1);

 

Then I check res variable use 

PQresultStatus(res) == PGRES_TUPLES_OK

       that the status is successed.

 

So, I use PQgetvalue() function get the value, like this:

       Int result = *((int*)PQgetvalue(res, 0, 0));

       Or 

       Long result = *((long*)PQgetvalue(res, 0, 0);

But, the result variable value is zero, the I use PQfname(res, 0) and PQfnumber(res, “count”) check status that output is corrected.

So, I change my code to:

       Int result = *((int*)PQgetvalue(res, 0, PQfnumber(res, “count”);

But, the result value  still is zero.

 

I login postgresql use psql, then perfrom select count(*) from tablename. The output is current, like:

       chatdb=# select count(*) from chat_connstate;

       count 

-------

          1

(1 行记录)

  

Then I perfrom “select * from tablename” in PQexecparams, and get the values. The values is currect this time.

 

So, I test 

       Select sum(*) from tablename;

       Int result = *((int*)PQgetvalue(res, 0, 0);   //the result value is zero

       

       Select avg(*) from tablename;

       Double result = *((double*)PQgetvalue(res, 0, 0); // the result value is not zero, but still is not equal to psql output.

 

Finally, I have no ideas for new test way. Please help me.

Thanks.

 

 

output infomations use psql and PQtrace() function in gdb:  (The system and postgresql environment in the bottom.)

 

chatdb=# select * from chat_connstate;

id |                       ip                       | port | connfd | account |                  token                   |          expires           |        create_date         

----+------------------------------------------------+------+--------+---------+------------------------------------------+----------------------------+----------------------------

  1 | 127.0.0.1                                      |    1 |      7 |         | 34a57ff275715c87235bf880f4c642d2539372fc | 2017-02-18 22:33:55.381506 | 2017-02-18 22:33:55.381506

(1 行记录)

 

1. Select count(*) from chat_connstate

 

(gdb) 

41       chat_psql_init_connstate(1);

(gdb) s

chat_psql_init_connstate (clear=1) at chat_psql.c:38

38       PQtrace(pgconn, stdout);

(gdb) n

39       res = PQexecParams(pgconn, "select count(*) from chat_connstate",

(gdb)

To backend> Msg P

To backend> ""

To backend> "select count(*) from chat_connstate"

To backend (2#)> 0

To backend> Msg complete, length 44

To backend> Msg B

To backend> ""

To backend> ""

To backend (2#)> 0

To backend (2#)> 0

To backend (2#)> 1

To backend (2#)> 1

To backend> Msg complete, length 15

To backend> Msg D

To backend> P

To backend> ""

To backend> Msg complete, length 7

To backend> Msg E

To backend> ""

To backend (4#)> 0

To backend> Msg complete, length 10

To backend> Msg S

To backend> Msg complete, length 5

^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: BUG? select count(*) from table don't get value via PQgetvalue() function in libpq(C)
  2017-02-18 16:04 BUG?  select count(*) from table   don't get value via PQgetvalue() function in libpq(C) Gao Yanxiao <553216793@qq.com>
@ 2017-02-18 17:58 ` Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 2+ messages in thread

From: Tom Lane @ 2017-02-18 17:58 UTC (permalink / raw)
  To: Gao Yanxiao <553216793@qq.com>; +Cc: pgsql-bugs

"Gao Yanxiao" <553216793@qq.com> writes:
>     res = PQexecParams(pgconn, "select count(*) from chat_connstate", 0, NULL, NULL, NULL, NULL, 1);

So you asked for binary-format results ...

> So, I use PQgetvalue() function get the value, like this:
>        Int result = *((int*)PQgetvalue(res, 0, 0));
>        Or 
>        Long result = *((long*)PQgetvalue(res, 0, 0);

The first of those is certainly not going to work, and depending on what
platform you're on the second won't either, because it's only extracting
the first 32 bits of the 64-bit result of count().

The other problem you've got here is that the result is coming off the
wire in big-endian byte order, and you're not doing anything to convert
that into native byte order (which more than likely is little-endian).
The reason you're seeing zeroes is that the high 32 bits of the result
are zeroes.

Personally, I would not bother with binary format unless I anticipated
processing enormous volumes of data; it's just too error-prone.
Better to use text and apply strtol() or whatever.

If you really must do it in binary format, here's the way pq_getmsgint64
reads a 64-bit big-endian value:

	int64		result;
	uint32		h32;
	uint32		l32;

	pq_copymsgbytes(msg, (char *) &h32, 4);
	pq_copymsgbytes(msg, (char *) &l32, 4);
	h32 = ntohl(h32);
	l32 = ntohl(l32);

	result = h32;
	result <<= 32;
	result |= l32;

which is pretty tedious, but there's no standard 64-bit version
of ntohl/htonl, so we have to swap each half separately.

			regards, tom lane


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2017-02-18 17:58 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2017-02-18 16:04 BUG?  select count(*) from table   don't get value via PQgetvalue() function in libpq(C) Gao Yanxiao <553216793@qq.com>
2017-02-18 17:58 ` Re: BUG? select count(*) from table don't get value via PQgetvalue() function in libpq(C) Tom Lane <tgl@sss.pgh.pa.us>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox