Snapshot management, final - Mailing list pgsql-patches

From Alvaro Herrera
Subject Snapshot management, final
Date
Msg-id 20080422194942.GF6912@alvh.no-ip.org
Whole thread Raw
Responses Re: Snapshot management, final  (Simon Riggs <simon@2ndquadrant.com>)
List pgsql-patches
Here is the patch for snapshot management I currently have.

Below is a complete description of the changes in this patch.

The most important change is on the use of CopySnapshot and the games
with ActiveSnapshot.  On the new code, whenever a piece of code needs a
snapshot to persist, it is "registered" by using RegisterSnapshot.  As
soon as it is done with it, it must be unregistered with
UnregisterSnapshot.

For ActiveSnapshot, we deal with a stack structure.  Callers needing an
Active snap set call PushActiveSnapshot, and PopActiveSnapshot when they
no longer need it.

GetSnapshotData still creates initial versions of each snapshot in
static memory.  There's a new boolean on the SnapshotData struct (set to
false by GetSnapshotData), which indicates whether the snapshot has been
copied out of static memory.  When a snapshot is Registered or
PushedActive, this bit is checked: if false, then a copy of the snapshot
is made on the dedicated SnapshotContext, and the "copied" flag is
flipped.

SnapshotData also carries two new reference counts: one for the Register
list, another one for the ActiveSnapshot stack.  Whenever a snap is
Unregistered or Popped, both refcounts are checked.  If both are found
to be zero then memory can be released.

On Unregister and Pop, after deleting the snapshot, we also verify the
whole lists.  If they are empty, it means no more live snapshot remain
for this transaction.  In this case we can reset MyProc->xmin to
InvalidXid.  Thus, GetSnapshotData must now recalculate MyProc->xmin
each time it finds xmin to be Invalid, which can not only happen for the
serializable snapshot but at any time.

A serializable transaction Registers its snapshot as first thing, and
keeps it registered until transaction end.


Note: I had previously made noises about changing the semantics of
MyProc->xmin or introducing a new VacuumXmin.  I have refrained from
doing so in this patch.


Those are the high-level changes.  Some coding changes:

- SerializableSnapshot and LatestSnapshot as global symbols are no more.

- A couple of PG_TRY blocks have been removed.  Particularly, on
plancache.c this means the do_planning() routine is now pointless and
has been folded back into its sole caller.

- Three CopySnapshot call sites remain outside snapmgr.c: DoCopy() on
copy.c, ExplainOnePlan() on explain.c and _SPI_execute_plan() on spi.c.
They are there because they grab the current ActiveSnapshot, modify it,
and then use the resulting snapshot.  There is no corresponding
FreeSnapshot, because it's not needed.

- CommandCounterIncrement now calls into snapmgr.c to set the CID of the
static snapshots.

- CREATE INDEX CONCURRENTLY, VACUUM FULL, and CLUSTER must now
explicitely Pop the ActiveSnapshot set by PortalRunUtility before
calling CommitTransactionCommand.

--
Alvaro Herrera                                http://www.CommandPrompt.com/
The PostgreSQL Company - Command Prompt, Inc.

Attachment

pgsql-patches by date:

Previous
From: Teodor Sigaev
Date:
Subject: Re: Partial match in GIN (next vesrion)
Next
From: Simon Riggs
Date:
Subject: Re: Snapshot management, final