1
0
mirror of https://github.com/postgres/postgres.git synced 2025-06-25 01:02:05 +03:00

Skip WAL for new relfilenodes, under wal_level=minimal.

Until now, only selected bulk operations (e.g. COPY) did this.  If a
given relfilenode received both a WAL-skipping COPY and a WAL-logged
operation (e.g. INSERT), recovery could lose tuples from the COPY.  See
src/backend/access/transam/README section "Skipping WAL for New
RelFileNode" for the new coding rules.  Maintainers of table access
methods should examine that section.

To maintain data durability, just before commit, we choose between an
fsync of the relfilenode and copying its contents to WAL.  A new GUC,
wal_skip_threshold, guides that choice.  If this change slows a workload
that creates small, permanent relfilenodes under wal_level=minimal, try
adjusting wal_skip_threshold.  Users setting a timeout on COMMIT may
need to adjust that timeout, and log_min_duration_statement analysis
will reflect time consumption moving to COMMIT from commands like COPY.

Internally, this requires a reliable determination of whether
RollbackAndReleaseCurrentSubTransaction() would unlink a relation's
current relfilenode.  Introduce rd_firstRelfilenodeSubid.  Amend the
specification of rd_createSubid such that the field is zero when a new
rel has an old rd_node.  Make relcache.c retain entries for certain
dropped relations until end of transaction.

Back-patch to 9.5 (all supported versions).  This introduces a new WAL
record type, XLOG_GIST_ASSIGN_LSN, without bumping XLOG_PAGE_MAGIC.  As
always, update standby systems before master systems.  This changes
sizeof(RelationData) and sizeof(IndexStmt), breaking binary
compatibility for affected extensions.  (The most recent commit to
affect the same class of extensions was
089e4d405d0f3b94c74a2c6a54357a84a681754b.)

Kyotaro Horiguchi, reviewed (in earlier, similar versions) by Robert
Haas.  Heikki Linnakangas and Michael Paquier implemented earlier
designs that materially clarified the problem.  Reviewed, in earlier
designs, by Andrew Dunstan, Andres Freund, Alvaro Herrera, Tom Lane,
Fujii Masao, and Simon Riggs.  Reported by Martijn van Oosterhout.

Discussion: https://postgr.es/m/20150702220524.GA9392@svana.org
This commit is contained in:
Noah Misch
2020-03-21 09:38:26 -07:00
parent 14d2bb4941
commit a653bd8aa7
46 changed files with 1416 additions and 323 deletions

View File

@ -2171,16 +2171,19 @@ include_dir 'conf.d'
levels. This parameter can only be set at server start.
</para>
<para>
In <literal>minimal</> level, WAL-logging of some bulk
operations can be safely skipped, which can make those
operations much faster (see <xref linkend="populate-pitr">).
Operations in which this optimization can be applied include:
In <literal>minimal</literal> level, no information is logged for
permanent relations for the remainder of a transaction that creates or
rewrites them. This can make operations much faster (see
<xref linkend="populate-pitr">). Operations that initiate this
optimization include:
<simplelist>
<member><command>CREATE TABLE AS</></member>
<member><command>CREATE INDEX</></member>
<member><command>CLUSTER</></member>
<member><command>COPY</> into tables that were created or truncated in the same
transaction</member>
<member><command>ALTER ... SET TABLESPACE</command></member>
<member><command>CLUSTER</command></member>
<member><command>CREATE TABLE</command></member>
<member><command>REFRESH MATERIALIZED VIEW</command>
(without <option>CONCURRENTLY</option>)</member>
<member><command>REINDEX</command></member>
<member><command>TRUNCATE</command></member>
</simplelist>
But minimal WAL does not contain enough information to reconstruct the
data from a base backup and the WAL logs, so <literal>replica</> or
@ -2569,6 +2572,26 @@ include_dir 'conf.d'
</listitem>
</varlistentry>
<varlistentry id="guc-wal-skip-threshold" xreflabel="wal_skip_threshold">
<term><varname>wal_skip_threshold</varname> (<type>integer</type>)
<indexterm>
<primary><varname>wal_skip_threshold</varname> configuration parameter</primary>
</indexterm>
</term>
<listitem>
<para>
When <varname>wal_level</varname> is <literal>minimal</literal> and a
transaction commits after creating or rewriting a permanent relation,
this setting determines how to persist the new data. If the data is
smaller than this setting, write it to the WAL log; otherwise, use an
fsync of affected files. Depending on the properties of your storage,
raising or lowering this value might help if such commits are slowing
concurrent transactions. The default is two megabytes
(<literal>2MB</literal>).
</para>
</listitem>
</varlistentry>
<varlistentry id="guc-commit-delay" xreflabel="commit_delay">
<term><varname>commit_delay</varname> (<type>integer</type>)
<indexterm>

View File

@ -1394,42 +1394,13 @@ SELECT * FROM x, y, a, b, c WHERE something AND somethingelse;
</para>
<para>
Aside from avoiding the time for the archiver or WAL sender to
process the WAL data,
doing this will actually make certain commands faster, because they
are designed not to write WAL at all if <varname>wal_level</varname>
is <literal>minimal</>. (They can guarantee crash safety more cheaply
by doing an <function>fsync</> at the end than by writing WAL.)
This applies to the following commands:
<itemizedlist>
<listitem>
<para>
<command>CREATE TABLE AS SELECT</command>
</para>
</listitem>
<listitem>
<para>
<command>CREATE INDEX</command> (and variants such as
<command>ALTER TABLE ADD PRIMARY KEY</command>)
</para>
</listitem>
<listitem>
<para>
<command>ALTER TABLE SET TABLESPACE</command>
</para>
</listitem>
<listitem>
<para>
<command>CLUSTER</command>
</para>
</listitem>
<listitem>
<para>
<command>COPY FROM</command>, when the target table has been
created or truncated earlier in the same transaction
</para>
</listitem>
</itemizedlist>
Aside from avoiding the time for the archiver or WAL sender to process the
WAL data, doing this will actually make certain commands faster, because
they do not to write WAL at all if <varname>wal_level</varname>
is <literal>minimal</literal> and the current subtransaction (or top-level
transaction) created or truncated the table or index they change. (They
can guarantee crash safety more cheaply by doing
an <function>fsync</function> at the end than by writing WAL.)
</para>
</sect2>