1
0
mirror of https://github.com/postgres/postgres.git synced 2025-06-29 10:41:53 +03:00

Add new block-by-block strategy for CREATE DATABASE.

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.

Discussion: http://postgr.es/m/CA+TgmoYtcdxBjLh31DLxUXHxFVMPGzrU5_T=CYCvRyFHywSBUQ@mail.gmail.com
This commit is contained in:
Robert Haas
2022-03-29 11:31:43 -04:00
parent bf902c1393
commit 9c08aea6a3
28 changed files with 1081 additions and 157 deletions

View File

@ -1502,6 +1502,10 @@ postgres 27093 0.0 0.0 30096 2752 ? Ss 11:34 0:00 postgres: ser
<entry><literal>TwophaseFileWrite</literal></entry>
<entry>Waiting for a write of a two phase state file.</entry>
</row>
<row>
<entry><literal>VersionFileWrite</literal></entry>
<entry>Waiting for the version file to be written while creating a database.</entry>
</row>
<row>
<entry><literal>WALBootstrapSync</literal></entry>
<entry>Waiting for WAL to reach durable storage during

View File

@ -25,6 +25,7 @@ CREATE DATABASE <replaceable class="parameter">name</replaceable>
[ [ WITH ] [ OWNER [=] <replaceable class="parameter">user_name</replaceable> ]
[ TEMPLATE [=] <replaceable class="parameter">template</replaceable> ]
[ ENCODING [=] <replaceable class="parameter">encoding</replaceable> ]
[ STRATEGY [=] <replaceable class="parameter">strategy</replaceable> ] ]
[ LOCALE [=] <replaceable class="parameter">locale</replaceable> ]
[ LC_COLLATE [=] <replaceable class="parameter">lc_collate</replaceable> ]
[ LC_CTYPE [=] <replaceable class="parameter">lc_ctype</replaceable> ]
@ -118,6 +119,27 @@ CREATE DATABASE <replaceable class="parameter">name</replaceable>
</para>
</listitem>
</varlistentry>
<varlistentry id="create-database-strategy" xreflabel="CREATE DATABASE STRATEGY">
<term><replaceable class="parameter">strategy</replaceable></term>
<listitem>
<para>
Strategy to be used in creating the new database. If
the <literal>WAL_LOG</literal> strategy is used, the database will be
copied block by block and each block will be separately written
to the write-ahead log. This is the most efficient strategy in
cases where the template database is small, and therefore it is the
default. The older <literal>FILE_COPY</literal> strategy is also
available. This strategy writes a small record to the write-ahead log
for each tablespace used by the target database. Each such record
represents copying an entire directory to a new location at the
filesystem level. While this does reduce the write-ahed
log volume substantially, especially if the template database is large,
it also forces the system to perform a checkpoint both before and
after the creation of the new database. In some situations, this may
have a noticeable negative impact on overall system performance.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><replaceable class="parameter">locale</replaceable></term>
<listitem>

View File

@ -177,6 +177,17 @@ PostgreSQL documentation
</listitem>
</varlistentry>
<varlistentry>
<term><option>-S <replaceable class="parameter">template</replaceable></option></term>
<term><option>--strategy=<replaceable class="parameter">strategy</replaceable></option></term>
<listitem>
<para>
Specifies the database creation strategy. See
<xref linkend="create-database-strategy" /> for more details.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><option>-T <replaceable class="parameter">template</replaceable></option></term>
<term><option>--template=<replaceable class="parameter">template</replaceable></option></term>