标簽
PostgreSQL , replication , 流複制 , 調試 , 協定
https://github.com/digoal/blog/blob/master/201806/20180614_03.md#%E8%83%8C%E6%99%AF 背景
當用戶端連接配接到PG服務端時,startup包裡面可以帶入一些資訊。
src/interfaces/libpq/fe-connect.c
當postmaster解析startup包時,如果解析到replication的參數資訊,則會啟動wal sender程序與用戶端互動,互動采用stream replication protocol.
src/backend/postmaster/postmaster.c
/*
* Read a client's startup packet and do something according to it.
*
* Returns STATUS_OK or STATUS_ERROR, or might call ereport(FATAL) and
* not return at all.
*
* (Note that ereport(FATAL) stuff is sent to the client, so only use it
* if that's what you want. Return STATUS_ERROR if you don't want to
* send anything to the client, which would typically be appropriate
* if we detect a communications failure.)
*/
static int
ProcessStartupPacket(Port *port, bool SSLdone)
{
............
else if (strcmp(nameptr, "replication") == 0)
{
/*
* Due to backward compatibility concerns the replication
* parameter is a hybrid beast which allows the value to be
* either boolean or the string 'database'. The latter
* connects to a specific database which is e.g. required for
* logical decoding while.
*/
if (strcmp(valptr, "database") == 0)
{
am_walsender = true;
am_db_walsender = true;
}
else if (!parse_bool(valptr, &am_walsender))
ereport(FATAL,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("invalid value for parameter \"%s\": \"%s\"",
"replication",
valptr),
errhint("Valid values are: \"false\", 0, \"true\", 1, \"database\".")));
}
/*
* Normal walsender backends, e.g. for streaming replication, are not
* connected to a particular database. But walsenders used for logical
* replication need to connect to a specific database. We allow streaming
* replication commands to be issued even if connected to a database as it
* can make sense to first make a basebackup and then stream changes
* starting from that.
*/
if (am_walsender && !am_db_walsender)
port->database_name[0] = '\0';
https://github.com/digoal/blog/blob/master/201806/20180614_03.md#%E4%BD%BF%E7%94%A8replication%E5%8F%82%E6%95%B0%E8%BF%9E%E6%8E%A5%E6%95%B0%E6%8D%AE%E5%BA%93%E7%9A%84%E4%BE%8B%E5%AD%90 使用replication參數連接配接資料庫的例子
參數允許設定為false,true,0,1,database
如果設定為database,可以使用logical replication指令
digoal@iZbp13nu0s9j3x3op4zpd4Z-> psql "replication=2" postgres
psql: FATAL: invalid value for parameter "replication": "2"
HINT: Valid values are: "false", 0, "true", 1, "database".
當建立流複制連接配接後,就隻能使用協定識别的指令,詳見:
https://www.postgresql.org/docs/10/static/protocol-replication.html https://www.postgresql.org/docs/10/static/protocol-logical-replication.htmldigoal@iZbp13nu0s9j3x3op4zpd4Z-> psql "replication=1" postgres
psql (10.4)
Type "help" for help.
postgres=# select 1;
ERROR: cannot execute SQL commands in WAL sender for physical replication
postgres=# \set VERBOSITY verbose
postgres=# select 1;
ERROR: XX000: cannot execute SQL commands in WAL sender for physical replication
LOCATION: exec_replication_command, walsender.c:1560
注意流複制協定指令區分大小寫
postgres=# IDENTIFY_SYSTEM
postgres-# ;
systemid | timeline | xlogpos | dbname
---------------------+----------+-------------+--------
6561932752697330615 | 1 | 2B/FE093610 |
(1 row)
postgres=# show block_size;
ERROR: XX000: cannot execute SQL commands in WAL sender for physical replication
LOCATION: exec_replication_command, walsender.c:1560
postgres=# SHOW block_size;
block_size
------------
8192
(1 row)
https://github.com/digoal/blog/blob/master/201806/20180614_03.md#%E4%BD%BF%E7%94%A8%E6%B5%81%E5%A4%8D%E5%88%B6%E5%8D%8F%E8%AE%AE%E7%9A%84%E4%B8%80%E4%BA%9B%E5%AE%A2%E6%88%B7%E7%AB%AF 使用流複制協定的一些用戶端
1、接收實體WAL日志
https://www.postgresql.org/docs/10/static/app-pgreceivewal.html2、接收上遊decode好的邏輯日志
https://www.postgresql.org/docs/10/static/app-pgrecvlogical.html3、線上備份
https://www.postgresql.org/docs/10/static/app-pgbasebackup.htmlhttps://github.com/digoal/blog/blob/master/201806/20180614_03.md#%E5%8F%82%E8%80%83 參考
https://jdbc.postgresql.org/documentation/head/connect.html#connection-parametersreplication = String
Connection parameter passed in the startup message.
This parameter accepts two values; "true" and database.
Passing true tells the backend to go into walsender mode,
wherein a small set of replication commands can be issued instead of SQL statements.
Only the simple query protocol can be used in walsender mode.
Passing "database" as the value instructs walsender to connect to the database specified in the dbname parameter,
which will allow the connection to be used for logical replication from that database.