mirror of
https://github.com/postgres/postgres.git
synced 2025-12-21 05:21:08 +03:00
Perform apply of large transactions by parallel workers.
Currently, for large transactions, the publisher sends the data in multiple streams (changes divided into chunks depending upon logical_decoding_work_mem), and then on the subscriber-side, the apply worker writes the changes into temporary files and once it receives the commit, it reads from those files and applies the entire transaction. To improve the performance of such transactions, we can instead allow them to be applied via parallel workers. In this approach, we assign a new parallel apply worker (if available) as soon as the xact's first stream is received and the leader apply worker will send changes to this new worker via shared memory. The parallel apply worker will directly apply the change instead of writing it to temporary files. However, if the leader apply worker times out while attempting to send a message to the parallel apply worker, it will switch to "partial serialize" mode - in this mode, the leader serializes all remaining changes to a file and notifies the parallel apply workers to read and apply them at the end of the transaction. We use a non-blocking way to send the messages from the leader apply worker to the parallel apply to avoid deadlocks. We keep this parallel apply assigned till the transaction commit is received and also wait for the worker to finish at commit. This preserves commit ordering and avoid writing to and reading from files in most cases. We still need to spill if there is no worker available. This patch also extends the SUBSCRIPTION 'streaming' parameter so that the user can control whether to apply the streaming transaction in a parallel apply worker or spill the change to disk. The user can set the streaming parameter to 'on/off', or 'parallel'. The parameter value 'parallel' means the streaming will be applied via a parallel apply worker, if available. The parameter value 'on' means the streaming transaction will be spilled to disk. The default value is 'off' (same as current behaviour). In addition, the patch extends the logical replication STREAM_ABORT message so that abort_lsn and abort_time can also be sent which can be used to update the replication origin in parallel apply worker when the streaming transaction is aborted. Because this message extension is needed to support parallel streaming, parallel streaming is not supported for publications on servers < PG16. Author: Hou Zhijie, Wang wei, Amit Kapila with design inputs from Sawada Masahiko Reviewed-by: Sawada Masahiko, Peter Smith, Dilip Kumar, Shi yu, Kuroda Hayato, Shveta Mallik Discussion: https://postgr.es/m/CAA4eK1+wyN6zpaHUkCLorEWNx75MG0xhMwcFhvjqm2KURZEAGw@mail.gmail.com
This commit is contained in:
@@ -1501,6 +1501,16 @@ CONTEXT: processing remote data for replication origin "pg_16395" during "INSER
|
||||
might not violate any constraint. This can easily make the subscriber
|
||||
inconsistent.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
When the streaming mode is <literal>parallel</literal>, the finish LSN of
|
||||
failed transactions may not be logged. In that case, it may be necessary to
|
||||
change the streaming mode to <literal>on</literal> or <literal>off</literal> and
|
||||
cause the same conflicts again so the finish LSN of the failed transaction will
|
||||
be written to the server log. For the usage of finish LSN, please refer to <link
|
||||
linkend="sql-altersubscription"><command>ALTER SUBSCRIPTION ...
|
||||
SKIP</command></link>.
|
||||
</para>
|
||||
</sect1>
|
||||
|
||||
<sect1 id="logical-replication-restrictions">
|
||||
@@ -1809,8 +1819,9 @@ CONTEXT: processing remote data for replication origin "pg_16395" during "INSER
|
||||
|
||||
<para>
|
||||
<link linkend="guc-max-logical-replication-workers"><varname>max_logical_replication_workers</varname></link>
|
||||
must be set to at least the number of subscriptions (for apply workers), plus
|
||||
some reserve for the table synchronization workers.
|
||||
must be set to at least the number of subscriptions (for leader apply
|
||||
workers), plus some reserve for the table synchronization workers and
|
||||
parallel apply workers.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
@@ -1827,6 +1838,13 @@ CONTEXT: processing remote data for replication origin "pg_16395" during "INSER
|
||||
subscription initialization or when new tables are added.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
<link linkend="guc-max-parallel-apply-workers-per-subscription"><varname>max_parallel_apply_workers_per_subscription</varname></link>
|
||||
controls the amount of parallelism for streaming of in-progress
|
||||
transactions with subscription parameter
|
||||
<literal>streaming = parallel</literal>.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
Logical replication workers are also affected by
|
||||
<link linkend="guc-wal-receiver-timeout"><varname>wal_receiver_timeout</varname></link>,
|
||||
|
||||
Reference in New Issue
Block a user