Snapshot management, final

From: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
To: Pg Patches <pgsql-patches(at)postgresql(dot)org>
Subject: Snapshot management, final
Date: 2008-04-22 19:49:43
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: 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

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

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.

explicitely Pop the ActiveSnapshot set by PortalRunUtility before
calling CommitTransactionCommand.

Alvaro Herrera
The PostgreSQL Company - Command Prompt, Inc.

Attachment Content-Type Size
snapshot-6.patch.bz2 application/octet-stream 17.8 KB


Browse pgsql-patches by date

  From Date Subject
Next Message Simon Riggs 2008-04-22 21:45:12 Re: Snapshot management, final
Previous Message Teodor Sigaev 2008-04-22 19:01:58 Re: Partial match in GIN (next vesrion)