Protect against SnapshotNow race conditions in pg_tablespace scans.
Protect against SnapshotNow race conditions in pg_tablespace scans.

Use of SnapshotNow is known to expose us to race conditions if the tuple(s)
being sought could be updated by concurrently-committing transactions.
CREATE DATABASE and DROP DATABASE are particularly exposed because they do
heavyweight filesystem operations during their scans of pg_tablespace,
so that the scans run for a very long time compared to most.  Furthermore,
the potential consequences of a missed or twice-visited row are nastier
than average:

* createdb() could fail with a bogus "file already exists" error, or
  silently fail to copy one or more tablespace's worth of files into the
  new database.

* remove_dbtablespaces() could miss one or more tablespaces, thus failing
  to free filesystem space for the dropped database.

* check_db_file_conflict() could likewise miss a tablespace, leading to an
  OID conflict that could result in data loss either immediately or in
  future operations.  (This seems of very low probability, though, since a
  duplicate database OID would be unlikely to start with.)

Hence, it seems worth fixing these three places to use MVCC snapshots, even
though this will someday be superseded by a generic solution to SnapshotNow
race conditions.

Back-patch to all active branches.

Stephen Frost and Tom Lane

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