@@ -377,3 +377,60 @@ exist anymore) you can trigger a :ref:`view cleanup <api/db/view_cleanup>`::
377377.. code-block :: javascript
378378
379379 {" ok" : true }
380+
381+ Generational Compaction
382+ =======================
383+
384+ Normally, compaction works by copying all live data (the document bodies and
385+ attachments for the latest versions of all documents) into a new file with the
386+ ``.compact `` extension, and then replacing the normal database file with this
387+ new one. That is, if the database resides in a file named ``mydb.couch ``, then
388+ compaction copies its live data into ``mydb.couch.compact ``, and then renames
389+ ``mydb.couch.compact `` to ``mydb.couch ``.
390+
391+ If you have a lot of documents that do not change for long periods of time, then
392+ compaction will have to copy a lot of the same data every time it runs. For
393+ large data sets, especially those where individual documents or attachments are
394+ very large, this can be wasteful and it leads to compaction taking a long time
395+ to run.
396+
397+ To improve compaction times for such data sets, CouchDB offers a mode called
398+ `generational compaction `. When this is enabled, the database is stored in
399+ multiple files known as `generations `, named ``mydb.1.couch ``, ``mydb.2.couch ``
400+ and so on, rather than in a single file. Rather than building a whole new copy
401+ of the ``mydb.couch `` file, generational compaction `promotes ` live data from
402+ its current generation to the next one up.
403+
404+ All new document and attachment data is originally stored in `generation 0 `, and
405+ when it is compacted, its live data is promoted to `generation 1 `. The next time
406+ generation 0 is compacted, CouchDB will not have to re-copy all the data that
407+ was already promoted to generation 1.
408+
409+ Generational storage and compaction can be enabled either by setting the ``gen ``
410+ parameter when the database is first created:
411+
412+ .. code-block :: bash
413+
414+ curl -X PUT http://admin:pass@localhost:5984/dbname? gen=G
415+
416+ Or, by setting the ``max_generation `` property on an existing database:
417+
418+ .. codeb-block :: bash
419+
420+ curl -X PUT http://admin:pass@localhost:5984/dbname/_max_generation -d G
421+
422+ The value ``G `` sets the database's ``max_generation `` number; a value of ``0 ``
423+ selects the normal single-file storage model. A value of ``1 `` produces a single
424+ extra generational file, ``2 `` produces two extra files, and so on.
425+
426+ When compacting a generational database, you may specify a generation to compact
427+ via the ``gen `` parameter:
428+
429+ .. code-block :: bash
430+
431+ curl -X POST http://admin:pass@localhost:5984/mydb/_compact? gen=1
432+
433+ Compaction targets generation 0 by default. CouchDB will only ever compact a
434+ single generation of a database at the time, and Smoosh monitors the extra
435+ generation files for when they exceed a configured slack/ratio threshold, just
436+ like it does when using single-file storage.
0 commit comments