[py-lmdb] Re: query what databases are in an environment

  • From: David Wilson <dw@xxxxxxxx>
  • To: py-lmdb@xxxxxxxxxxxxx
  • Date: Mon, 13 Apr 2015 13:10:18 +0000

Hi there,

The usual advice is that in order to avoid unexpected key conflicts, if
you are using sub-databases, the main database should not contain any
keys except sub-database descriptors (which are internally created by
mdb_dbi_open()/Environment.open_db()), at which point discovering the
names of all sub-databases is simply:

    def get_dbi_names():
        with env.begin() as txn, txn.cursor() as cursor():
            return list(cursor.iternext())

    # Since we don't ever write regular data keys to the main DBI, we
    # need a sub-DBI for keys we would like to write there.
    main_db = env.open_db('main')

    with env.begin(db=main_db) as txn:
        txn.put('foo', 'bar')   # etc.

This is a slightly rough patch in LMDB's API, but following convention
makes it easy to work around. Ideally there would be no keyspace
conflicts, or alternatively put() would fail on the main DBI if any
sub-database had been created.


On Sun, Apr 12, 2015 at 09:36:43PM -0400, KP wrote:
> Is there a way to query the main database to find the available databases that
> are in the environment?
> I saw that in the docs that the main database had that, but couldn't quite 
> find
> an example of how. I see that the main has a key with my other database
> name...but how to distinguish that key (or value) is due to the existence of a
> named database?

Other related posts: