Re: [PROPOSAL] Use SnapshotAny in get_actual_variable_range

Dmitriy Sarafannikov <dsarafannikov@yandex.ru>

From: Dmitriy Sarafannikov <dsarafannikov@yandex.ru>
To: Amit Kapila <amit.kapila16@gmail.com>
Cc: Tom Lane <tgl@sss.pgh.pa.us>, Andres Freund <andres@anarazel.de>, Robert Haas <robertmhaas@gmail.com>, "pgsql-hackers@postgresql.org" <pgsql-hackers@postgresql.org>, Borodin Vladimir <root@simply.name>, Хомик Кирилл <khomikki@yandex-team.ru>
Date: 2017-05-02T08:59:30Z
Lists: pgsql-hackers
> If that is the case, then how would using SnapshotAny solve this
> problem.  We get the value from index first and then check its
> visibility in heap, so if time is spent in _bt_checkkeys, why would
> using a different kind of Snapshot solve the problem?

1st scanning on the index with SnapshotAny will accept this rows and
will not mark this entries as killed. 

Theremore, killed entries are ignored on standby. And scanning with
SnapshotAny will always accept first row from index.
     /*
      * During recovery we ignore killed tuples and don't bother to kill them
      * either. We do this because the xmin on the primary node could easily be
      * later than the xmin on the standby node, so that what the primary
      * thinks is killed is supposed to be visible on standby. So for correct
      * MVCC for queries during recovery we must ignore these hints and check
      * all tuples. Do *not* set ignore_killed_tuples to true when running in a
      * transaction that was started during recovery. xactStartedInRecovery
      * should not be altered by index AMs.
      */
     scan->kill_prior_tuple = false;
     scan->xactStartedInRecovery = TransactionStartedDuringRecovery();
     scan->ignore_killed_tuples = !scan->xactStartedInRecovery;

We execute this read-only queries on standby.

Commits

  1. Improve performance of get_actual_variable_range with recently-dead tuples.

  2. Use SnapshotDirty rather than an active snapshot to probe index endpoints.