Rearrange dblink's dblink_build_sql_insert() and related routines to open and lock the target relation just once per SQL function call.  The original coding obtained and released lock several times per call.  Aside from saving a not-insignificant number of cycles, this eliminates possible race conditions if someone tries to modify the relation's schema concurrently.  Also centralize locking and permission-checking logic.
Rearrange dblink's dblink_build_sql_insert() and related routines to open and
lock the target relation just once per SQL function call.  The original coding
obtained and released lock several times per call.  Aside from saving a
not-insignificant number of cycles, this eliminates possible race conditions
if someone tries to modify the relation's schema concurrently.  Also
centralize locking and permission-checking logic.

Problem noted while investigating a trouble report from Robert Voinea --- his
problem is still to be fixed, though.

edespino/gpdb,royc1/gpdb,0x0FFF/gpdb,rvs/gpdb,xuegang/gpdb,chrishajas/gpdb,Quikling/gpdb,rvs/gpdb,rvs/gpdb,greenplum-db/gpdb,xuegang/gpdb,zaksoup/gpdb,Chibin/gpdb,0x0FFF/gpdb,0x0FFF/gpdb,lisakowen/gpdb,cjcjameson/gpdb,yuanzhao/gpdb,Chibin/gpdb,cjcjameson/gpdb,xuegang/gpdb,xinzweb/gpdb,cjcjameson/gpdb,kaknikhil/gpdb,rvs/gpdb,ashwinstar/gpdb,adam8157/gpdb,Quikling/gpdb,50wu/gpdb,ashwinstar/gpdb,0x0FFF/gpdb,ashwinstar/gpdb,janebeckman/gpdb,lintzc/gpdb,CraigHarris/gpdb,Quikling/gpdb,yuanzhao/gpdb,lintzc/gpdb,jmcatamney/gpdb,xinzweb/gpdb,royc1/gpdb,chrishajas/gpdb,cjcjameson/gpdb,50wu/gpdb,kaknikhil/gpdb,kaknikhil/gpdb,xuegang/gpdb,lisakowen/gpdb,xuegang/gpdb,kaknikhil/gpdb,ahachete/gpdb,yuanzhao/gpdb,jmcatamney/gpdb,adam8157/gpdb,chrishajas/gpdb,adam8157/gpdb,kaknikhil/gpdb,Quikling/gpdb,ahachete/gpdb,zaksoup/gpdb,yuanzhao/gpdb,0x0FFF/gpdb,Chibin/gpdb,kaknikhil/gpdb,yuanzhao/gpdb,xinzweb/gpdb,edespino/gpdb,Quikling/gpdb,janebeckman/gpdb,ashwinstar/gpdb,lintzc/gpdb,xinzweb/gpdb,cjcjameson/gpdb,royc1/gpdb,lintzc/gpdb,rvs/gpdb,greenplum-db/gpdb,50wu/gpdb,lisakowen/gpdb,lisakowen/gpdb,edespino/gpdb,xuegang/gpdb,Chibin/gpdb,lintzc/gpdb,kaknikhil/gpdb,janebeckman/gpdb,xuegang/gpdb,ashwinstar/gpdb,ahachete/gpdb,lintzc/gpdb,CraigHarris/gpdb,xuegang/gpdb,greenplum-db/gpdb,greenplum-db/gpdb,lintzc/gpdb,royc1/gpdb,rvs/gpdb,0x0FFF/gpdb,edespino/gpdb,chrishajas/gpdb,jmcatamney/gpdb,adam8157/gpdb,royc1/gpdb,jmcatamney/gpdb,rvs/gpdb,yuanzhao/gpdb,janebeckman/gpdb,lintzc/gpdb,CraigHarris/gpdb,ahachete/gpdb,rvs/gpdb,janebeckman/gpdb,Quikling/gpdb,Quikling/gpdb,zaksoup/gpdb,xinzweb/gpdb,jmcatamney/gpdb,edespino/gpdb,Chibin/gpdb,ashwinstar/gpdb,jmcatamney/gpdb,CraigHarris/gpdb,lisakowen/gpdb,Chibin/gpdb,adam8157/gpdb,adam8157/gpdb,Chibin/gpdb,edespino/gpdb,xinzweb/gpdb,chrishajas/gpdb,xinzweb/gpdb,cjcjameson/gpdb,yuanzhao/gpdb,Chibin/gpdb,50wu/gpdb,ashwinstar/gpdb,jmcatamney/gpdb,royc1/gpdb,royc1/gpdb,greenplum-db/gpdb,ahachete/gpdb,chrishajas/gpdb,edespino/gpdb,edespino/gpdb,kaknikhil/gpdb,lisakowen/gpdb,50wu/gpdb,rvs/gpdb,kaknikhil/gpdb,cjcjameson/gpdb,CraigHarris/gpdb,zaksoup/gpdb,zaksoup/gpdb,Quikling/gpdb,ashwinstar/gpdb,CraigHarris/gpdb,jmcatamney/gpdb,Quikling/gpdb,Chibin/gpdb,50wu/gpdb,janebeckman/gpdb,janebeckman/gpdb,0x0FFF/gpdb,ahachete/gpdb,yuanzhao/gpdb,Chibin/gpdb,lintzc/gpdb,CraigHarris/gpdb,adam8157/gpdb,ahachete/gpdb,yuanzhao/gpdb,0x0FFF/gpdb,kaknikhil/gpdb,greenplum-db/gpdb,adam8157/gpdb,cjcjameson/gpdb,zaksoup/gpdb,janebeckman/gpdb,CraigHarris/gpdb,zaksoup/gpdb,lisakowen/gpdb,50wu/gpdb,ahachete/gpdb,rvs/gpdb,chrishajas/gpdb,Quikling/gpdb,cjcjameson/gpdb,edespino/gpdb,yuanzhao/gpdb,greenplum-db/gpdb,50wu/gpdb,royc1/gpdb,janebeckman/gpdb,CraigHarris/gpdb,greenplum-db/gpdb,chrishajas/gpdb,lisakowen/gpdb,edespino/gpdb,xinzweb/gpdb,xuegang/gpdb,zaksoup/gpdb,cjcjameson/gpdb,janebeckman/gpdb