Drain the COPY on cancellation
Drain the COPY on cancellation

The COPY process isn't finished until PQgetCopyData() returns -1 so
we must consume all rows sent until we get -1. A single call should
in most cases do the trick but there are no guarantees so loop around
consumption until end-of-COPY is signalled. In case of error, break
out and continue operation to keep current flow, investigating the
error is probably warranted but handling cases that might fall out is
a bigger question that this isolated patch.

Also use PQfreemem() for the buffer per the manual. This is the same
as just free() on Linux/UNIX (while critically different on Windows)
but we might as well follow the set API to reduce confusion.

Original report by Coverity

kaknikhil/gpdb,lisakowen/gpdb,adam8157/gpdb,rvs/gpdb,greenplum-db/gpdb,ashwinstar/gpdb,jmcatamney/gpdb,greenplum-db/gpdb,cjcjameson/gpdb,greenplum-db/gpdb,greenplum-db/gpdb,Chibin/gpdb,xinzweb/gpdb,yuanzhao/gpdb,rvs/gpdb,lintzc/gpdb,Quikling/gpdb,50wu/gpdb,rvs/gpdb,xinzweb/gpdb,kaknikhil/gpdb,lintzc/gpdb,xinzweb/gpdb,cjcjameson/gpdb,50wu/gpdb,Chibin/gpdb,janebeckman/gpdb,janebeckman/gpdb,kaknikhil/gpdb,greenplum-db/gpdb,lintzc/gpdb,50wu/gpdb,greenplum-db/gpdb,kaknikhil/gpdb,Chibin/gpdb,Chibin/gpdb,Quikling/gpdb,yuanzhao/gpdb,cjcjameson/gpdb,janebeckman/gpdb,Quikling/gpdb,lisakowen/gpdb,cjcjameson/gpdb,xinzweb/gpdb,yuanzhao/gpdb,50wu/gpdb,ashwinstar/gpdb,rvs/gpdb,yuanzhao/gpdb,yuanzhao/gpdb,edespino/gpdb,ashwinstar/gpdb,edespino/gpdb,lintzc/gpdb,rvs/gpdb,Quikling/gpdb,janebeckman/gpdb,Quikling/gpdb,jmcatamney/gpdb,janebeckman/gpdb,janebeckman/gpdb,jmcatamney/gpdb,rvs/gpdb,adam8157/gpdb,ashwinstar/gpdb,edespino/gpdb,Chibin/gpdb,ashwinstar/gpdb,xinzweb/gpdb,kaknikhil/gpdb,cjcjameson/gpdb,50wu/gpdb,xinzweb/gpdb,yuanzhao/gpdb,kaknikhil/gpdb,edespino/gpdb,lisakowen/gpdb,ashwinstar/gpdb,lisakowen/gpdb,lintzc/gpdb,edespino/gpdb,rvs/gpdb,cjcjameson/gpdb,ashwinstar/gpdb,yuanzhao/gpdb,50wu/gpdb,edespino/gpdb,lisakowen/gpdb,kaknikhil/gpdb,greenplum-db/gpdb,cjcjameson/gpdb,jmcatamney/gpdb,adam8157/gpdb,xinzweb/gpdb,adam8157/gpdb,jmcatamney/gpdb,janebeckman/gpdb,Quikling/gpdb,edespino/gpdb,lisakowen/gpdb,Quikling/gpdb,edespino/gpdb,cjcjameson/gpdb,Quikling/gpdb,lintzc/gpdb,rvs/gpdb,cjcjameson/gpdb,kaknikhil/gpdb,50wu/gpdb,lisakowen/gpdb,jmcatamney/gpdb,xinzweb/gpdb,yuanzhao/gpdb,edespino/gpdb,jmcatamney/gpdb,adam8157/gpdb,rvs/gpdb,yuanzhao/gpdb,yuanzhao/gpdb,lintzc/gpdb,Chibin/gpdb,kaknikhil/gpdb,adam8157/gpdb,lisakowen/gpdb,lintzc/gpdb,ashwinstar/gpdb,lintzc/gpdb,adam8157/gpdb,janebeckman/gpdb,Chibin/gpdb,Chibin/gpdb,janebeckman/gpdb,50wu/gpdb,kaknikhil/gpdb,rvs/gpdb,jmcatamney/gpdb,edespino/gpdb,Quikling/gpdb,adam8157/gpdb,Quikling/gpdb,Chibin/gpdb,janebeckman/gpdb,cjcjameson/gpdb,greenplum-db/gpdb,Chibin/gpdb