标签
PostgreSQL , replication , 流复制 , 调试 , 协议
背景
当客户端连接到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';
使用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.html
digoal@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)
使用流复制协议的一些客户端
1、接收物理WAL日志
https://www.postgresql.org/docs/10/static/app-pgreceivewal.html
2、接收上游decode好的逻辑日志
https://www.postgresql.org/docs/10/static/app-pgrecvlogical.html
3、在线备份
https://www.postgresql.org/docs/10/static/app-pgbasebackup.html
参考
https://www.postgresql.org/docs/10/static/protocol-replication.html
https://www.postgresql.org/docs/10/static/protocol-logical-replication.html
https://jdbc.postgresql.org/documentation/head/connect.html#connection-parameters
replication = 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.