|
| 1 | +## PostgreSQL 9.5 new feature - log replication commands |
| 2 | + |
| 3 | +### 作者 |
| 4 | +digoal |
| 5 | + |
| 6 | +### 日期 |
| 7 | +2015-04-09 |
| 8 | + |
| 9 | +### 标签 |
| 10 | +PostgreSQL , 逻辑复制 , command |
| 11 | + |
| 12 | +---- |
| 13 | + |
| 14 | +## 背景 |
| 15 | +PostgreSQL 9.5新增一个参数, 可以记录流复制协议中用到的命令到日志中. |
| 16 | + |
| 17 | +``` |
| 18 | +log_replication_commands = on |
| 19 | +``` |
| 20 | + |
| 21 | +协议解释详见 : |
| 22 | + |
| 23 | +http://www.postgresql.org/docs/devel/static/protocol-replication.html |
| 24 | + |
| 25 | +测试 |
| 26 | + |
| 27 | +``` |
| 28 | +pg_hba.conf |
| 29 | +# Allow replication connections from localhost, by a user with the |
| 30 | +# replication privilege. |
| 31 | +local replication postgres trust |
| 32 | +host replication postgres 127.0.0.1/32 trust |
| 33 | +host replication postgres ::1/128 trust |
| 34 | +postgresql.conf |
| 35 | +wal_level = logical |
| 36 | +max_wal_senders = 2 |
| 37 | +max_replication_slots = 2 |
| 38 | +log_replication_commands = on |
| 39 | + |
| 40 | +pg95@db-172-16-3-150-> pg_ctl restart -m fast |
| 41 | +``` |
| 42 | + |
| 43 | +使用一个流复制协议command测试 : |
| 44 | + |
| 45 | +``` |
| 46 | +pg95@db-172-16-3-150-> psql "dbname=postgres replication=database" -c "IDENTIFY_SYSTEM;" |
| 47 | + systemid | timeline | xlogpos | dbname |
| 48 | +---------------------+----------+-----------+---------- |
| 49 | + 6135538990815133068 | 1 | 0/1B186A0 | postgres |
| 50 | +(1 row) |
| 51 | +``` |
| 52 | + |
| 53 | +以上命令在数据库日志中可以看到. |
| 54 | + |
| 55 | +``` |
| 56 | +2015-04-09 10:49:59.481 CST,"postgres","postgres",21664,"[local]",5525e8d7.54a0,3,"idle",2015-04-09 10:49:59 CST,2/0,0,LOG,00000,"received replication command: IDENTIFY_SYSTEM;",,,,,,,,"exec_replication_command, walsender.c:1274","psql" |
| 57 | +``` |
| 58 | + |
| 59 | +用于区分连接流复制的代码: |
| 60 | + |
| 61 | +src/backend/replication/libpqwalreceiver/libpqwalreceiver.c |
| 62 | + |
| 63 | +``` |
| 64 | +/* |
| 65 | + * Establish the connection to the primary server for XLOG streaming |
| 66 | + */ |
| 67 | +static void |
| 68 | +libpqrcv_connect(char *conninfo) |
| 69 | +{ |
| 70 | + char conninfo_repl[MAXCONNINFO + 75]; |
| 71 | + |
| 72 | + /* |
| 73 | + * Connect using deliberately undocumented parameter: replication. The |
| 74 | + * database name is ignored by the server in replication mode, but specify |
| 75 | + * "replication" for .pgpass lookup. |
| 76 | + */ |
| 77 | + snprintf(conninfo_repl, sizeof(conninfo_repl), |
| 78 | + "%s dbname=replication replication=true fallback_application_name=walreceiver", |
| 79 | + conninfo); |
| 80 | + |
| 81 | + streamConn = PQconnectdb(conninfo_repl); |
| 82 | + if (PQstatus(streamConn) != CONNECTION_OK) |
| 83 | + ereport(ERROR, |
| 84 | + (errmsg("could not connect to the primary server: %s", |
| 85 | + PQerrorMessage(streamConn)))); |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +src/bin/pg_basebackup/streamutil.c |
| 90 | + |
| 91 | +``` |
| 92 | + * streamutil.c - utility functions for pg_basebackup and pg_receivelog |
| 93 | + * |
| 94 | + * Author: Magnus Hagander <[email protected]> |
| 95 | + * |
| 96 | + * Portions Copyright (c) 1996-2014, PostgreSQL Global Development Group |
| 97 | + * |
| 98 | + * IDENTIFICATION |
| 99 | + * src/bin/pg_basebackup/streamutil.c |
| 100 | + |
| 101 | +/* |
| 102 | + * Connect to the server. Returns a valid PGconn pointer if connected, |
| 103 | + * or NULL on non-permanent error. On permanent error, the function will |
| 104 | + * call exit(1) directly. |
| 105 | + */ |
| 106 | +PGconn * |
| 107 | +GetConnection(void) |
| 108 | +{ |
| 109 | +... |
| 110 | + keywords[i] = "dbname"; |
| 111 | + values[i] = dbname == NULL ? "replication" : dbname; |
| 112 | + i++; |
| 113 | + keywords[i] = "replication"; |
| 114 | + values[i] = dbname == NULL ? "true" : "database"; |
| 115 | + i++; |
| 116 | + keywords[i] = "fallback_application_name"; |
| 117 | + values[i] = progname; |
| 118 | + i++; |
| 119 | +... |
| 120 | +``` |
| 121 | + |
| 122 | +只需要提供replication=true的连接串即可。 |
| 123 | + |
| 124 | +``` |
| 125 | +psql "replication=true" postgres |
| 126 | +psql (9.5.0) |
| 127 | +Type "help" for help. |
| 128 | +postgres=# IDENTIFY_SYSTEM; |
| 129 | + systemid | timeline | xlogpos | dbname |
| 130 | +---------------------+----------+-------------+-------- |
| 131 | + 6243682321523937214 | 1 | 1F/FB2D7B48 | |
| 132 | +(1 row) |
| 133 | +``` |
| 134 | + |
| 135 | + |
| 136 | +<a rel="nofollow" href="http://info.flagcounter.com/h9V1" ><img src="http://s03.flagcounter.com/count/h9V1/bg_FFFFFF/txt_000000/border_CCCCCC/columns_2/maxflags_12/viewers_0/labels_0/pageviews_0/flags_0/" alt="Flag Counter" border="0" ></a> |
| 137 | + |
0 commit comments