Because this strategy logs changes on a block-by-block basis, it avoids the need to checkpoint before and after the operation. However, because it logs each changed block individually, it might generate a lot of extra write-ahead logging if the template database is large. Therefore, the older strategy remains available via a new STRATEGY parameter to CREATE DATABASE, and a corresponding --strategy option to createdb.
Somewhat controversially, this patch assembles the list of relations to be copied to the new database by reading the pg_class relation of the template database. Cross-database access like this isn't normally possible, but it can be made to work here because there can't be any connections to the database being copied, nor can it contain any in-doubt transactions. Even so, we have to use lower-level interfaces than normal, since the table scan and relcache interfaces will not work for a database to which we're not connected. The advantage of this approach is that we do not need to rely on the filesystem to determine what ought to be copied, but instead on PostgreSQL's own knowledge of the database structure. This avoids, for example, copying stray files that happen to be located in the source database directory.
Dilip Kumar, with a fairly large number of cosmetic changes by me. Reviewed and tested by Ashutosh Sharma, Andres Freund, John Naylor, Greg Nancarrow, Neha Sharma. Additional feedback from Bruce Momjian, Heikki Linnakangas, Julien Rouhaud, Adam Brusselback, Kyotaro Horiguchi, Tomas Vondra, Andrew Dunstan, Álvaro Herrera, and others.
9c08aea6a3 Add new block-by-block strategy for CREATE DATABASE.
contrib/bloom/blinsert.c | 2 +-
doc/src/sgml/monitoring.sgml | 4 +
doc/src/sgml/ref/create_database.sgml | 22 +
doc/src/sgml/ref/createdb.sgml | 11 +
src/backend/access/heap/heapam_handler.c | 6 +-
src/backend/access/nbtree/nbtree.c | 2 +-
src/backend/access/rmgrdesc/dbasedesc.c | 20 +-
src/backend/access/transam/xlogutils.c | 6 +-
src/backend/catalog/heap.c | 2 +-
src/backend/catalog/storage.c | 34 +-
src/backend/commands/dbcommands.c | 769 ++++++++++++++++++++++++++-----
src/backend/commands/tablecmds.c | 2 +-
src/backend/storage/buffer/bufmgr.c | 172 ++++++-
src/backend/storage/lmgr/lmgr.c | 28 ++
src/backend/utils/activity/wait_event.c | 3 +
src/backend/utils/cache/relcache.c | 2 +-
src/backend/utils/cache/relmapper.c | 64 +++
src/bin/pg_rewind/parsexlog.c | 9 +-
src/bin/psql/tab-complete.c | 4 +-
src/bin/scripts/createdb.c | 10 +-
src/bin/scripts/t/020_createdb.pl | 20 +
src/include/catalog/storage.h | 4 +-
src/include/commands/dbcommands_xlog.h | 25 +-
src/include/storage/bufmgr.h | 6 +-
src/include/storage/lmgr.h | 1 +
src/include/utils/relmapper.h | 4 +-
src/include/utils/wait_event.h | 1 +
src/tools/pgindent/typedefs.list | 5 +-
28 files changed, 1081 insertions(+), 157 deletions(-)