pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andres Freund <andres@anarazel.de>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: snapshot scalability: Introduce dense array of in-progress xids.
Date: Fri, 14 Aug 2020 22:40:13 +0000
Message-ID: <E1k6iMv-0001VP-Ml@gemulon.postgresql.org> (raw)

snapshot scalability: Introduce dense array of in-progress xids.

The new array contains the xids for all connected backends / in-use
PGPROC entries in a dense manner (in contrast to the PGPROC/PGXACT
arrays which can have unused entries interspersed).

This improves performance because GetSnapshotData() always needs to
scan the xids of all live procarray entries and now there's no need to
go through the procArray->pgprocnos indirection anymore.

As the set of running top-level xids changes rarely, compared to the
number of snapshots taken, this substantially increases the likelihood
of most data required for a snapshot being in l2 cache.  In
read-mostly workloads scanning the xids[] array will sufficient to
build a snapshot, as most backends will not have an xid assigned.

To keep the xid array dense ProcArrayRemove() needs to move entries
behind the to-be-removed proc's one further up in the array. Obviously
moving array entries cannot happen while a backend sets it
xid. I.e. locking needs to prevent that array entries are moved while
a backend modifies its xid.

To avoid locking ProcArrayLock in GetNewTransactionId() - a fairly hot
spot already - ProcArrayAdd() / ProcArrayRemove() now needs to hold
XidGenLock in addition to ProcArrayLock. Adding / Removing a procarray
entry is not a very frequent operation, even taking 2PC into account.

Due to the above, the dense array entries can only be read or modified
while holding ProcArrayLock and/or XidGenLock. This prevents a
concurrent ProcArrayRemove() from shifting the dense array while it is
accessed concurrently.

While the new dense array is very good when needing to look at all
xids it is less suitable when accessing a single backend's xid. In
particular it would be problematic to have to acquire a lock to access
a backend's own xid. Therefore a backend's xid is not just stored in
the dense array, but also in PGPROC. This also allows a backend to
only access the shared xid value when the backend had acquired an
xid.

The infrastructure added in this commit will be used for the remaining
PGXACT fields in subsequent commits. They are kept separate to make
review easier.

Author: Andres Freund <andres@anarazel.de>
Reviewed-By: Robert Haas <robertmhaas@gmail.com>
Reviewed-By: Thomas Munro <thomas.munro@gmail.com>
Reviewed-By: David Rowley <dgrowleyml@gmail.com>
Discussion: https://postgr.es/m/20200301083601.ews6hz5dduc3w2se@alap3.anarazel.de

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/941697c3c1ae5d6ee153065adb96e1e63ee11224

Modified Files
--------------
src/backend/access/heap/heapam_visibility.c |   8 +-
src/backend/access/transam/README           |  29 +--
src/backend/access/transam/clog.c           |   8 +-
src/backend/access/transam/twophase.c       |  31 ++--
src/backend/access/transam/varsup.c         |  20 +-
src/backend/commands/vacuum.c               |   2 +-
src/backend/storage/ipc/procarray.c         | 271 +++++++++++++++++++---------
src/backend/storage/ipc/sinvaladt.c         |   4 +-
src/backend/storage/lmgr/lock.c             |   3 +-
src/backend/storage/lmgr/proc.c             |  26 ++-
src/include/storage/proc.h                  |  79 +++++++-
11 files changed, 327 insertions(+), 154 deletions(-)



Message-ID: <E1k6iMv-0001VP-Ml@gemulon.postgresql.org>
Permalink:  ../E1k6iMv-0001VP-Ml@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1k6iMv-0001VP-Ml@gemulon.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-committers@postgresql.org
  Cc: andres@anarazel.de
  Subject: Re: pgsql: snapshot scalability: Introduce dense array of in-progress xids.
  In-Reply-To: <E1k6iMv-0001VP-Ml@gemulon.postgresql.org>
  References: <E1k6iMv-0001VP-Ml@gemulon.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox