pgpool是一个很不错的中间件,具体安装配置可参考PostGreSQL pgpool-II集群配置,不过如果你没有很深入的去了解,那么会有踩不完的坑。今天我们聊聊遇到的pgpool中关于failover的坑。failover这个功能初衷是好的.当主库发生问题时,自动切换到备库,简直不要太爽。不过某些时候我们不想让它切过去,这就有点难为情了。

kill 头锁进程

某些情况,数据库发生大面积锁,导致业务不能继续,那么你要找到导致这么一堆锁发生的原因,也就是头锁,然后干掉它。这里有一个sql 可以查看当前数据库锁的情况,并根据锁的先后顺序做了排序,排在首位的就是头锁。为了简单,我们可以创建成视图v_locks_monitor,然后直接用select * from v_locks_monitor 进行查看。
创建视图

  1. create view v_locks_monitor as
  2. with
  3. t_wait as
  4. (
  5. select a.mode,a.locktype,a.database,a.relation,a.page,a.tuple,a.classid,a.granted,
  6. a.objid,a.objsubid,a.pid,a.virtualtransaction,a.virtualxid,a.transactionid,a.fastpath,
  7. b.state,b.query,b.xact_start,b.query_start,b.usename,b.datname,b.client_addr,b.client_port,b.application_name
  8. from pg_locks a,pg_stat_activity b where a.pid=b.pid and not a.granted
  9. ),
  10. t_run as
  11. (
  12. select a.mode,a.locktype,a.database,a.relation,a.page,a.tuple,a.classid,a.granted,
  13. a.objid,a.objsubid,a.pid,a.virtualtransaction,a.virtualxid,a.transactionid,a.fastpath,
  14. b.state,b.query,b.xact_start,b.query_start,b.usename,b.datname,b.client_addr,b.client_port,b.application_name
  15. from pg_locks a,pg_stat_activity b where a.pid=b.pid and a.granted
  16. ),
  17. t_overlap as
  18. (
  19. select r.* from t_wait w join t_run r on
  20. (
  21. r.locktype is not distinct from w.locktype and
  22. r.database is not distinct from w.database and
  23. r.relation is not distinct from w.relation and
  24. r.page is not distinct from w.page and
  25. r.tuple is not distinct from w.tuple and
  26. r.virtualxid is not distinct from w.virtualxid and
  27. r.transactionid is not distinct from w.transactionid and
  28. r.classid is not distinct from w.classid and
  29. r.objid is not distinct from w.objid and
  30. r.objsubid is not distinct from w.objsubid and
  31. r.pid <> w.pid
  32. )
  33. ),
  34. t_unionall as
  35. (
  36. select r.* from t_overlap r
  37. union all
  38. select w.* from t_wait w
  39. )
  40. select locktype,datname,relation::regclass,page,tuple,virtualxid,transactionid::text,classid::regclass,objid,objsubid,
  41. string_agg(
  42. 'Pid: '||case when pid is null then 'NULL' else pid::text end||chr(10)||
  43. 'Lock_Granted: '||case when granted is null then 'NULL' else granted::text end||' , Mode: '||case when mode is null then 'NULL' else mode::text end||' , FastPath: '||case when fastpath is null then 'NULL' else fastpath::text end||' , VirtualTransaction: '||case when virtualtransaction is null then 'NULL' else virtualtransaction::text end||' , Session_State: '||case when state is null then 'NULL' else state::text end||chr(10)||
  44. 'Username: '||case when usename is null then 'NULL' else usename::text end||' , Database: '||case when datname is null then 'NULL' else datname::text end||' , Client_Addr: '||case when client_addr is null then 'NULL' else client_addr::text end||' , Client_Port: '||case when client_port is null then 'NULL' else client_port::text end||' , Application_Name: '||case when application_name is null then 'NULL' else application_name::text end||chr(10)||
  45. 'Xact_Start: '||case when xact_start is null then 'NULL' else xact_start::text end||' , Query_Start: '||case when query_start is null then 'NULL' else query_start::text end||' , Xact_Elapse: '||case when (now()-xact_start) is null then 'NULL' else (now()-xact_start)::text end||' , Query_Elapse: '||case when (now()-query_start) is null then 'NULL' else (now()-query_start)::text end||chr(10)||
  46. 'SQL (Current SQL in Transaction): '||chr(10)||
  47. case when query is null then 'NULL' else query::text end,
  48. chr(10)||'--------'||chr(10)
  49. order by
  50. ( case mode
  51. when 'INVALID' then 0
  52. when 'AccessShareLock' then 1
  53. when 'RowShareLock' then 2
  54. when 'RowExclusiveLock' then 3
  55. when 'ShareUpdateExclusiveLock' then 4
  56. when 'ShareLock' then 5
  57. when 'ShareRowExclusiveLock' then 6
  58. when 'ExclusiveLock' then 7
  59. when 'AccessExclusiveLock' then 8
  60. else 0
  61. end ) desc,
  62. (case when granted then 0 else 1 end)
  63. ) as lock_conflict
  64. from t_unionall
  65. group by
  66. locktype,datname,relation,page,tuple,virtualxid,transactionid::text,classid,objid,objsubid ;

此处不举例,我们重点是pgpool。

坑爹的参数failover_on_backend_eror

重点来了,我们kill了一个头锁,突然发现,链接被重置了,再看,主库变备库,备库变主库了。。。
我们来看下pgpool官网关于Failover and Failback 这段的描述。开头定义了什么是Failover: pgpool发现后端连接的pg节点不可用后自动剔除掉该节点。然后调用自动以的脚本去做一些事情,一般是自动切换
注意我们红框标注的这个地方,提到了坑爹参数 failover_on_backend_eror 。该参数如果是off 状态,那么当连接到pg的连接发生错误或者巴拉巴拉的什么情况,pgpool 仅仅是断开这个session的连接。那么就不会发生我们kill掉一个进程从而导致failover的情况
坑爹的Failover (pgpool) - 图1

问题来了,看这个参数的解释,这里没有提默认是on还是off。但是实际配置上,我们一般不去了解所有的参数的意义,都是遵循大部分的默认配置。当我们kill掉一个连接,发生了自动切换后,我们知道了,参数默认是on。
坑爹的Failover (pgpool) - 图2

最佳实践

如果你不想因为kill 一个头锁的进程而导致pgpool自动切,那么一定要注意显性的配置该参数为off。
关于防止一般问题导致自动切换,可以多注意下health check的几个参数,降低pgpool对pg的敏感度。根据名字也知道是什么意思,详细可参考 https://www.pgpool.net/docs/latest/en/html/runtime-config-health-check.html.

health_check_max_retries = 3
health_check_retry_delay = 3