Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
PostgreSQL 提供了將 SQL 執行的資料變更串流傳輸到外部資料庫的基礎結構。此功能可用於多種目的,包括了複寫解決方案和稽核需求。
變更會以邏輯複寫插槽的串流形式發送出去。
串流傳輸這些變更的格式由所使用的輸出模組決定。PostgreSQL 發行版中提供了一個範例模組。可以撰寫其他模組來擴展可用的格式,而毋須修改任何核心程式。每個輸出模組都可以存取 INSERT 所產生的每個新資料列以及 UPDATE 所建立的新資料列版本。UPDATE 和 DELETE 舊資料列版本的可用性取決於其所設定的副本識別(請參閱 REPLICA IDENTITY)。
可以使用串流複寫協定(請參閱第 52.4 節和第 48.3 節)或透過 SQL 呼叫函數(請參閱第 48.4 節)來使用資料變更的內容。也可以撰寫其他方法來處理複寫插槽的輸出,而毋須修改核心程式(請參閱第 48.7 節)。
Logical decoding is the process of extracting all persistent changes to a database's tables into a coherent, easy to understand format which can be interpreted without detailed knowledge of the database's internal state.
In PostgreSQL, logical decoding is implemented by decoding the contents of the write-ahead log, which describe changes on a storage level, into an application-specific form such as a stream of tuples or SQL statements.
In the context of logical replication, a slot represents a stream of changes that can be replayed to a client in the order they were made on the origin server. Each slot streams a sequence of changes from a single database.
PostgreSQL also has streaming replication slots (see Section 26.2.5), but they are used somewhat differently there.
A replication slot has an identifier that is unique across all databases in a PostgreSQL cluster. Slots persist independently of the connection using them and are crash-safe.
A logical slot will emit each change just once in normal operation. The current position of each slot is persisted only at checkpoint, so in the case of a crash the slot may return to an earlier LSN, which will then cause recent changes to be sent again when the server restarts. Logical decoding clients are responsible for avoiding ill effects from handling the same message more than once. Clients may wish to record the last LSN they saw when decoding and skip over any repeated data or (when using the replication protocol) request that decoding start from that LSN rather than letting the server determine the start point. The Replication Progress Tracking feature is designed for this purpose, refer to replication origins.
Multiple independent slots may exist for a single database. Each slot has its own state, allowing different consumers to receive changes from different points in the database change stream. For most applications, a separate slot will be required for each consumer.
A logical replication slot knows nothing about the state of the receiver(s). It's even possible to have multiple different receivers using the same slot at different times; they'll just get the changes following on from when the last receiver stopped consuming them. Only one receiver may consume changes from a slot at any given time.
Replication slots persist across crashes and know nothing about the state of their consumer(s). They will prevent removal of required resources even when there is no connection using them. This consumes storage because neither required WAL nor required rows from the system catalogs can be removed by VACUUM
as long as they are required by a replication slot. In extreme cases this could cause the database to shut down to prevent transaction ID wraparound (see Section 24.1.5). So if a slot is no longer required it should be dropped.
Output plugins transform the data from the write-ahead log's internal representation into the format the consumer of a replication slot desires.
When a new replication slot is created using the streaming replication interface (see CREATE_REPLICATION_SLOT), a snapshot is exported (see Section 9.27.5), which will show exactly the state of the database after which all changes will be included in the change stream. This can be used to create a new replica by using SET TRANSACTION SNAPSHOT
to read the state of the database at the moment the slot was created. This transaction can then be used to dump the database's state at that point in time, which afterwards can be updated using the slot's contents without losing any changes.
Creation of a snapshot is not always possible. In particular, it will fail when connected to a hot standby. Applications that do not require snapshot export may suppress it with the NOEXPORT_SNAPSHOT
option.
The commands
CREATE_REPLICATION_SLOT
slot_name
LOGICAL output_plugin
DROP_REPLICATION_SLOT
slot_name
[ WAIT
]
START_REPLICATION SLOT
slot_name
LOGICAL ...
are used to create, drop, and stream changes from a replication slot, respectively. These commands are only available over a replication connection; they cannot be used via SQL. See Section 52.4 for details on these commands.
The command pg_recvlogical can be used to control logical decoding over a streaming replication connection. (It uses these commands internally.)
See Section 9.27.6 for detailed documentation on the SQL-level API for interacting with logical decoding.
Synchronous replication (see Section 26.2.8) is only supported on replication slots used over the streaming replication interface. The function interface and additional, non-core interfaces do not support synchronous replication.
It is possible to add more output methods for logical decoding. For details, see src/backend/replication/logical/logicalfuncs.c
. Essentially, three functions need to be provided: one to read WAL, one to prepare writing output, and one to write the output (see Section 48.6.5).
An example output plugin can be found in the contrib/test_decoding
subdirectory of the PostgreSQL source tree.
An output plugin is loaded by dynamically loading a shared library with the output plugin's name as the library base name. The normal library search path is used to locate the library. To provide the required output plugin callbacks and to indicate that the library is actually an output plugin it needs to provide a function named _PG_output_plugin_init
. This function is passed a struct that needs to be filled with the callback function pointers for individual actions.
The begin_cb
, change_cb
and commit_cb
callbacks are required, while startup_cb
, filter_by_origin_cb
, truncate_cb
, and shutdown_cb
are optional. If truncate_cb
is not set but a TRUNCATE
is to be decoded, the action will be ignored.
To decode, format and output changes, output plugins can use most of the backend's normal infrastructure, including calling output functions. Read only access to relations is permitted as long as only relations are accessed that either have been created by initdb
in the pg_catalog
schema, or have been marked as user provided catalog tables using
Any actions leading to transaction ID assignment are prohibited. That, among others, includes writing to tables, performing DDL changes, and calling pg_current_xact_id()
.
Output plugin callbacks can pass data to the consumer in nearly arbitrary formats. For some use cases, like viewing the changes via SQL, returning data in a data type that can contain arbitrary data (e.g., bytea
) is cumbersome. If the output plugin only outputs textual data in the server's encoding, it can declare that by setting OutputPluginOptions.output_type
to OUTPUT_PLUGIN_TEXTUAL_OUTPUT
instead of OUTPUT_PLUGIN_BINARY_OUTPUT
in the startup callback. In that case, all the data has to be in the server's encoding so that a text
datum can contain it. This is checked in assertion-enabled builds.
An output plugin gets notified about changes that are happening via various callbacks it needs to provide.
Concurrent transactions are decoded in commit order, and only changes belonging to a specific transaction are decoded between the begin
and commit
callbacks. Transactions that were rolled back explicitly or implicitly never get decoded. Successful savepoints are folded into the transaction containing them in the order they were executed within that transaction.
Only transactions that have already safely been flushed to disk will be decoded. That can lead to a COMMIT
not immediately being decoded in a directly following pg_logical_slot_get_changes()
when synchronous_commit
is set to off
.
The optional startup_cb
callback is called whenever a replication slot is created or asked to stream changes, independent of the number of changes that are ready to be put out.
The is_init
parameter will be true when the replication slot is being created and false otherwise. options
points to a struct of options that output plugins can set:
output_type
has to either be set to OUTPUT_PLUGIN_TEXTUAL_OUTPUT
or OUTPUT_PLUGIN_BINARY_OUTPUT
. See also Section 48.6.3. If receive_rewrites
is true, the output plugin will also be called for changes made by heap rewrites during certain DDL operations. These are of interest to plugins that handle DDL replication, but they require special handling.
The startup callback should validate the options present in ctx->output_plugin_options
. If the output plugin needs to have a state, it can use ctx->output_plugin_private
to store it.
The optional shutdown_cb
callback is called whenever a formerly active replication slot is not used anymore and can be used to deallocate resources private to the output plugin. The slot isn't necessarily being dropped, streaming is just being stopped.
The required begin_cb
callback is called whenever a start of a committed transaction has been decoded. Aborted transactions and their contents never get decoded.
The txn
parameter contains meta information about the transaction, like the time stamp at which it has been committed and its XID.
The required commit_cb
callback is called whenever a transaction commit has been decoded. The change_cb
callbacks for all modified rows will have been called before this, if there have been any modified rows.
The required change_cb
callback is called for every individual row modification inside a transaction, may it be an INSERT
, UPDATE
, or DELETE
. Even if the original command modified several rows at once the callback will be called individually for each row.
The ctx
and txn
parameters have the same contents as for the begin_cb
and commit_cb
callbacks, but additionally the relation descriptor relation
points to the relation the row belongs to and a struct change
describing the row modification are passed in.
Only changes in user defined tables that are not unlogged (see UNLOGGED
) and not temporary (see TEMPORARY
or TEMP
) can be extracted using logical decoding.
The truncate_cb
callback is called for a TRUNCATE
command.
The parameters are analogous to the change_cb
callback. However, because TRUNCATE
actions on tables connected by foreign keys need to be executed together, this callback receives an array of relations instead of just a single one. See the description of the TRUNCATE statement for details.
The optional filter_by_origin_cb
callback is called to determine whether data that has been replayed from origin_id
is of interest to the output plugin.
The ctx
parameter has the same contents as for the other callbacks. No information but the origin is available. To signal that changes originating on the passed in node are irrelevant, return true, causing them to be filtered away; false otherwise. The other callbacks will not be called for transactions and changes that have been filtered away.
This is useful when implementing cascading or multidirectional replication solutions. Filtering by the origin allows to prevent replicating the same changes back and forth in such setups. While transactions and changes also carry information about the origin, filtering via this callback is noticeably more efficient.
The optional message_cb
callback is called whenever a logical decoding message has been decoded.
The txn
parameter contains meta information about the transaction, like the time stamp at which it has been committed and its XID. Note however that it can be NULL when the message is non-transactional and the XID was not assigned yet in the transaction which logged the message. The lsn
has WAL location of the message. The transactional
says if the message was sent as transactional or not. The prefix
is arbitrary null-terminated prefix which can be used for identifying interesting messages for the current plugin. And finally the message
parameter holds the actual message of message_size
size.
Extra care should be taken to ensure that the prefix the output plugin considers interesting is unique. Using name of the extension or the output plugin itself is often a good choice.
To actually produce output, output plugins can write data to the StringInfo
output buffer in ctx->out
when inside the begin_cb
, commit_cb
, or change_cb
callbacks. Before writing to the output buffer, OutputPluginPrepareWrite(ctx, last_write)
has to be called, and after finishing writing to the buffer, OutputPluginWrite(ctx, last_write)
has to be called to perform the write. The last_write
indicates whether a particular write was the callback's last write.
The following example shows how to output data to the consumer of an output plugin:
The following example demonstrates controlling logical decoding using the SQL interface.
Before you can use logical decoding, you must set wal_level to logical
and max_replication_slots to at least 1. Then, you should connect to the target database (in the example below, postgres
) as a superuser.
The following example shows how logical decoding is controlled over the streaming replication protocol, using the program pg_recvlogical included in the PostgreSQL distribution. This requires that client authentication is set up to allow replication connections (see Section 26.2.5.1) and that max_wal_senders
is set sufficiently high to allow an additional connection.
Logical decoding can be used to build synchronous replication solutions with the same user interface as synchronous replication for streaming replication. To do this, the streaming replication interface (see Section 48.3) must be used to stream out data. Clients have to send Standby status update (F)
(see Section 52.4) messages, just like streaming replication clients do.
A synchronous replica receiving changes via logical decoding will work in the scope of a single database. Since, in contrast to that, synchronous_standby_names
currently is server wide, this means this technique will not work properly if more than one database is actively used.
The pg_replication_slots
view and the pg_stat_replication
view provide information about the current state of replication slots and streaming replication connections respectively. These views apply to both physical and logical replication.