How to pack your ZODB databaseΒΆ

Author:Sebastian Ware

The ZODB grows with each write operation. In order to reduce the size of the data.fs file, you need to perform a “pack” operation.

Packing from a grok.View class can be done with the following method:

self.request.publication.db.pack()

Each update is stored by appending the new state of the updated objects to the end of the data.fs file. This exposes an object history, allowing ZODB to offer an object undo feature and also multi version concurrency control.

A disadvantage with this approach is that the database file grows with each write. This requires you to “pack” the database to reduce the size by removing old copies of objects.

You can access the current database from a view by:

self.request.publication.db

The database exposes many features that you might find interesting, such as cache parameters and undo services. However, the only method that we need to pack the database is conveniently called just that:

class Pack(grok.View):

    def render(self):
        self.request.publication.db.pack()

This is what the documentation says:

pack(self, t=None, days=0) method of ZODB.DB.DB instance
    Pack the storage, deleting unused object revisions.

    A pack is always performed relative to a particular time, by
    default the current time.  All object revisions that are not
    reachable as of the pack time are deleted from the storage.

    The cost of this operation varies by storage, but it is
    usually an expensive operation.

    There are two optional arguments that can be used to set the
    pack time: t, pack time in seconds since the epcoh, and days,
    the number of days to subtract from t or from the current
    time if t is not specified.