Teach libpq to detect integer overflow in the row count of a PGresult.
Teach libpq to detect integer overflow in the row count of a PGresult.

Adding more than 1 billion rows to a PGresult would overflow its ntups and
tupArrSize fields, leading to client crashes.  It'd be desirable to use
wider fields on 64-bit machines, but because all of libpq's external APIs
use plain "int" for row counters, that's going to be hard to accomplish
without an ABI break.  Given the lack of complaints so far, and the general
pain that would be involved in using such huge PGresults, let's settle for
just preventing the overflow and reporting a useful error message if it
does happen.  Also, for a couple more lines of code we can increase the
threshold of trouble from INT_MAX/2 to INT_MAX rows.

To do that, refactor pqAddTuple() to allow returning an error message that
replaces the default assumption that it failed because of out-of-memory.

Along the way, fix PQsetvalue() so that it reports all failures via
pqInternalNotice().  It already did so in the case of bad field number,
but neglected to report anything for other error causes.

Because of the potential for crashes, this seems like a back-patchable
bug fix, despite the lack of field reports.

Michael Paquier, per a complaint from Igor Korot.

Discussion: ae7198adc88cc6db907f171e9419ddfd663af26c@mail.gmail.com

lisakowen/gpdb,jmcatamney/gpdb,xinzweb/gpdb,50wu/gpdb,lisakowen/gpdb,ashwinstar/gpdb,adam8157/gpdb,xinzweb/gpdb,jmcatamney/gpdb,ashwinstar/gpdb,ashwinstar/gpdb,jmcatamney/gpdb,adam8157/gpdb,adam8157/gpdb,jmcatamney/gpdb,lisakowen/gpdb,ashwinstar/gpdb,lisakowen/gpdb,lisakowen/gpdb,greenplum-db/gpdb,greenplum-db/gpdb,ashwinstar/gpdb,50wu/gpdb,xinzweb/gpdb,xinzweb/gpdb,xinzweb/gpdb,lisakowen/gpdb,50wu/gpdb,greenplum-db/gpdb,adam8157/gpdb,50wu/gpdb,jmcatamney/gpdb,greenplum-db/gpdb,jmcatamney/gpdb,jmcatamney/gpdb,xinzweb/gpdb,adam8157/gpdb,adam8157/gpdb,adam8157/gpdb,xinzweb/gpdb,greenplum-db/gpdb,50wu/gpdb,lisakowen/gpdb,ashwinstar/gpdb,adam8157/gpdb,jmcatamney/gpdb,greenplum-db/gpdb,greenplum-db/gpdb,xinzweb/gpdb,50wu/gpdb,ashwinstar/gpdb,50wu/gpdb,lisakowen/gpdb,greenplum-db/gpdb,ashwinstar/gpdb,50wu/gpdb