Make XactLockTableWait work for transactions that are not yet self-locked
Make XactLockTableWait work for transactions that are not yet self-locked

XactLockTableWait assumed that its xid argument has already added itself
to the lock table.  That assumption led to another assumption that if
locking the xid has succeeded but the xid is reported as still in
progress, then the input xid must have been a subtransaction.

These assumptions hold true for the original uses of this code in
locking related to on-disk tuples, but they break down in logical
replication slot snapshot building -- in particular, when a standby
snapshot logged contains an xid that's already in ProcArray but not yet
in the lock table.  This leads to assertion failures that can be
reproduced all the way back to 9.4, when logical decoding was
introduced.

To fix, change SubTransGetParent to SubTransGetTopmostTransaction which
has a slightly different API: it returns the argument Xid if there is no
parent, and it goes all the way to the top instead of moving up the
levels one by one.  Also, to avoid busy-waiting, add a 1ms sleep to give
the other process time to register itself in the lock table.

For consistency, change ConditionalXactLockTableWait the same way.

Author: Petr Jelínek
Discussion: d5096ccc180a7a5f16829a4dffa04875142264b8@postgrespro.ru
Reported-by: Konstantin Knizhnik
Diagnosed-by: Stas Kelvich, Petr Jelínek
Reviewed-by: Andres Freund, Robert Haas

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