Fix torn-page, unlogged xid and further risks from heap_update().
Fix torn-page, unlogged xid and further risks from heap_update().

When heap_update needs to look for a page for the new tuple version,
because the current one doesn't have sufficient free space, or when
columns have to be processed by the tuple toaster, it has to release the
lock on the old page during that. Otherwise there'd be lock ordering and
lock nesting issues.

To avoid concurrent sessions from trying to update / delete / lock the
tuple while the page's content lock is released, the tuple's xmax is set
to the current session's xid.

That unfortunately was done without any WAL logging, thereby violating
the rule that no XIDs may appear on disk, without an according WAL
record.  If the database were to crash / fail over when the page level
lock is released, and some activity lead to the page being written out
to disk, the xid could end up being reused; potentially leading to the
row becoming invisible.

There might be additional risks by not having t_ctid point at the tuple
itself, without having set the appropriate lock infomask fields.

To fix, compute the appropriate xmax/infomask combination for locking
the tuple, and perform WAL logging using the existing XLOG_HEAP_LOCK
record. That allows the fix to be backpatched.

This issue has existed for a long time. There appears to have been
partial attempts at preventing dangers, but these never have fully been
implemented, and were removed a long time ago, in
11919160 (cf. HEAP_XMAX_UNLOGGED).

In master / 9.6, there's an additional issue, namely that the
visibilitymap's freeze bit isn't reset at that point yet. Since that's a
new issue, introduced only in a892234f830, that'll be fixed in a
separate commit.

Author: Masahiko Sawada and Andres Freund
Reported-By: Different aspects by Thomas Munro, Noah Misch, and others
Discussion: 1f612f060d4ca28b4a84800931355cbc66bca719@mail.gmail.com
Backpatch: 9.1/all supported versions

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