postgres 常规操作杂记
分布式:
1.扩容不方便(数据重分布)
2.分布键变更很麻烦
3.分布键选择(架构设计)谨慎
4.跨库join性能差
5.分布式事务性能差
6.sql限制多,功能确实多
7.应用改造成本巨大
8.全局一致性时间点恢复几乎不可能实现
一、PGSQL 常规操作
citus 分库分表:
https://yq.aliyun.com/articles/647368?spm=a2c4e.11153940.0.0.428c3fb76WPkVX
http://mysql.taobao.org/monthly/2018/01/08/
备库:
max_standby_archive_delay =-1
max_standby_streaming_delay=-1
表示备库查询完成在进行recover恢复,即恢复不影响查询
分区表的建立:
-- Table: "GPO".count_perion_days_lottery_201912
-- DROP TABLE "GPO".count_perion_days_lottery_201912;
CREATE TABLE "GPO".count_perion_days_lottery_201912
(
CONSTRAINT count_perion_days_lottery_201912_pkey PRIMARY KEY (id),
CONSTRAINT count_perion_days_lottery_201912_no_uq UNIQUE (account_id, create_time, lottery_id),
CONSTRAINT count_perion_days_lottery_201912_create_time_check CHECK (create_time >= '2019-12-01 00:00:00'::timestamp without time zone AND create_time <= '2019-12-31 23:59:59.999999'::timestamp without time zone)
)
INHERITS ("GPO".count_perion_days_lottery)
WITH (
OIDS = FALSE
)
TABLESPACE pg_default;
ALTER TABLE "GPO".count_perion_days_lottery_201912
OWNER to "GPO";
GRANT ALL ON TABLE "GPO".count_perion_days_lottery_201912 TO "GPO";
GRANT UPDATE, DELETE, INSERT, SELECT ON TABLE "GPO".count_perion_days_lottery_201912 TO "GPO_agent";
GRANT UPDATE, INSERT, SELECT, DELETE ON TABLE "GPO".count_perion_days_lottery_201912 TO "GPO_member";
GRANT SELECT ON TABLE "GPO".count_perion_days_lottery_201912 TO "MylIZ8UUIFO7KZBh1hXEnCPHqugzAm";
GRANT ALL ON TABLE "GPO".count_perion_days_lottery_201912 TO "PcHTa4478A";
GRANT SELECT ON TABLE "GPO".count_perion_days_lottery_201912 TO bwtback;
GRANT SELECT ON TABLE "GPO".count_perion_days_lottery_201912 TO dev_query;
GRANT SELECT ON TABLE "GPO".count_perion_days_lottery_201912 TO "uqnO9KKg5VPlFGezn90jgcM5uQXqZr";
-- Index: count_perion_days_lottery_201912_ctime_lottid_accid
-- DROP INDEX "GPO".count_perion_days_lottery_201912_ctime_lottid_accid;
CREATE INDEX count_perion_days_lottery_201912_ctime_lottid_accid
ON "GPO".count_perion_days_lottery_201912 USING btree
(create_time, lottery_id, account_id)
TABLESPACE pg_default;
-- Index: count_perion_days_lottery_201912_lottid_ctime_accid
-- DROP INDEX "GPO".count_perion_days_lottery_201912_lottid_ctime_accid;
CREATE INDEX count_perion_days_lottery_201912_lottid_ctime_accid
ON "GPO".count_perion_days_lottery_201912 USING btree
(lottery_id, create_time, account_id)
TABLESPACE pg_default;
pg_rewind增量同步数据:
pg_rewind --target-pgdata=/pgsql/pg_data/ --source-server='host=192.168.10.51 port=5432 user=postgres password=postgres dbname=postgres' -P
查询的几个重要的数据字典:
查看网站:https://gp-docs-cn.github.io/docs/ref_guide/system_catalogs/pg_attribute.html
其中:
pg_class:可以查看oid
pg_attribute:可以查看熟悉名称
information_schema.COLUMNS:
pg_namespace:这个nspname可以通过pg_class (relnamespace = n.oid)查看schema名称
查看慢sql:
语句级一般通过开启pg_stat_statements
shared_preload_libraries = 'pg_stat_statements' # (change requires restart)
pg_stat_statements.track = all
GPO=# CREATE EXTENSION pg_stat_statements ;
查看postgres数据库中已安装的模块:
\dx
select * from pg_extension ;
select * from pg_available_extensions;
初始化一个新的库(安装好了数据库软件):
/usr/local/pgsql_100/bin/initdb -D /home/pg_data/pg_data_100 -E UTF-8 --locale=C -U postgres -W
CASE
WHEN condition_1 THEN result_1
WHEN condition_2 THEN result_2
[WHEN ...]
[ELSE result_n]
END
PostgreSQL中默认schema设置:
search_path='"GPO"'
search_path = '"GPO","$user", public'
/usr/local/pgsql/bin/psql -Upostgres -h127.0.0.1 -p54202 GPO -c "set search_path="GPO";
select count(1) from information_schema.columns where table_name='mny_draw_record' and table_schema='GPO' group by table_name;"
--自关联删除重复记录:
delete from mny_com_record a using (select order_no,max(com_record_id) as id from mny_com_record group by order_no) b
where a.order_no=b.order_no and a.com_record_id < b.id
或者:
delete from read as a where id < select max(id) from reader read as b where a.id=b.id and a.name=b.name)
GPO=# SHOW search_path;
search_path
-------------
"GPO"
(1 row)
GPO=#
GPO=# select current_user;
current_user
--------------
postgres
(1 row)
2.具体的顺序,也可以通过如下的命令来修改顺序。这样的修改只能在当前连接中有效。
SET search_path TO 'schema_name';
3.如果需要长久生效可以为用户创建一个变量:
ALTER ROLE etl SET search_path=trade;
官方建议是这样的:在管理员创建一个具体数据库后,应该为所有可以连接到该数据库的用户分别创建一个与用户名相同的模式,然后,将search_path设置为"$user",
即默认的模式是与用户名相同的模式。一般是再数据库的 文件中添加到文件postgresql.conf:search_path='"GPO"'
查看表权限:
select * from information_schema.table_privileges where grantee='OT_member';
--99.对新表授权_其它用户.sql
grant select,update,delete,insert on all tables in schema "GPO" to "OT_member";
grant select,update,delete,insert on all tables in schema "GPO" to "OT_agent";
grant usage,select,update on all sequences in schema "GPO" to "GPO_member";
grant usage,select,update on all sequences in schema "GPO" to "GPO_agent";
grant usage,select,update on all sequences in schema "GPO" to "PcHTa4478A";
grant usage,select,update on all sequences in schema "GPO" to "GPO_member";
grant usage,select,update on all sequences in schema "GPO" to "MylIZ8UUIFO7KZBh1hXEnCPHqugzAm";
grant usage,select,update on all sequences in schema "GPO" to "querysysuser";
grant usage,select,update on all sequences in schema "GPO" to "GPO_agent";
grant select on all tables in schema "GPO" to "MylIZ8UUIFO7KZBh1hXEnCPHqugzAm";
create database "GPO_NEW" OWNER "GPO" encoding='UTF8';
权限:
--1、查看某用户的表权限
select * from information_schema.table_privileges where grantee='user_name';
--2、查看usage权限表
select * from information_schema.usage_privileges where grantee='user_name';
--3、查看存储过程函数相关权限表
select * from information_schema.routine_privileges where grantee='user_name';
--4、建用户授权
create user user_name;
alter user user_name with password '';
alter user user_name with CONNECTION LIMIT ?20;#连接数限制
--创建只读用户
--1.创建一个用户名为readonly密码为ropass的用户
CREATE USER readonly WITH ENCRYPTED PASSWORD 'ropass';
--2.用户只读事务
alter user readonly set default_transaction_read_only=on;
?
--3.把所有库的语言的USAGE权限给到readonly
GRANT USAGE ON SCHEMA public to readonly;
--4.授予select权限(这句要进入具体数据库操作在哪个db环境执行就授予那个db的权)
grant select on all tables in schema public to readonly;
USAGE
对于过程语言,允许使用指定过程语言创建该语言的函数。这是适用于过程语言的唯一权限。
对于模式,允许访问包含在指定模式中的对象(假设该对象的所有权要求同样也设置了)。 最终这些就允许了权限接受者"查询"模式中的对象。没有这个权限仍然可以看见这些对象的名字 (比如通过查询系统视图)。同样,撤销该权限之后,现有的后端可能有在查找之前就执行了的语句, 因此这不是一个很安全的限制对象访问的方法。
对于序列,该权限允许使用currval和nextval函数。
对于类型和域,该权限允许在创建表、函数和其他模式对象时使用类型或域。 (请注意,它不控制类型的一般"使用",例如显示在查询中的该类型的值。 它只防止依赖于该类型的对象被创建。该权限的主要目的是控制哪个用户在类型上创建依赖, 该依赖会防止用户稍后改变类型。)
对于外部数据封装器,该权限使权限接受者能够使用那个外部数据封装器创建新的服务器。
对于服务器,该权限使权限接受者能够使用该服务器创建外部表,并且也能创建、 修改或删除他自己的用户的与该服务器相关的用户映射。
otcp
需要执行的语句:
CREATE USER "GPO" WITH PASSWORD '^regex$';
CREATE SCHEMA "GPO" AUTHORIZATION "GPO";
create database "GPO_NEW" OWNER "GPO" encoding='UTF8';
CREATE DATABASE "GPO" OWNER "GPO" encoding='UTF8';
GRANT ALL PRIVILEGES ON DATABASE "GPO" TO "GPO";
GRANT ALL ON SCHEMA "GPO" TO "GPO";
--GRANT ALL ON SCHEMA "GPO" TO "PcHTa4478A";
GRANT ALL ON SCHEMA "GPO" TO PUBLIC;
约束:
alter table api_import_total add constraint api_import_total_unique unique (account1,platform,betting_date);
alter table ge_dashboard_total add constraint ge_dashboard_total_unique unique (account1,collect_date);
修改array:
update tps_game_category set category_name = '快3' where category_level = 4 and code = 'K3';
update tps_game_mobile set category_names = Array['彩票','系统彩票','官方玩法','快3'] where category_first_type = 1;
查看分区表(postgres10,9没有这个试图)
select * from pg_partitioned_table ;
子表解除继承以及加入继承关系:
一般分区父表比较大的时候,操作子表,可以进行子表的接触解除继承关系后加入继承关系,比如,对子表的DML(删除操作)
alter table c1 no inherit p1;
alter table c1 drop constraint p1_c1_check;
alter table c1 inherit p1;
'test ! -f /home/pg_archive/%f && cp %p /home/pg_archive/%f'
popstgrersql 清理归档:(只能清理到备份的归档之前的)
pg_archivecleanup -d /pgsql/pg_data/pg_xlog 000000050000000A00000026
查看OID 号:
select relname,oid from pg_class where relname='test_array1';
PostgreSQL - 用psql 运行SQL文件
方式一:连接db后执行SQL文件
首先通过psql连接到对应的db:
psql -d db1 -U userA
接着输入密码,进入数据库后,输入:
\i /pathA/xxx.sql
这里有个问题,如果你把SQL文件的路径里的路径分隔符写成了\,会报错说Permission denied。
这里的文件路径必须使用Linux平台下的路径分隔符/,否则会报错。
方式二:直接通过psql命令执行SQL文件
这种方式无需先登录数据库,直接用一个命令就可以了:
psql -d db1 -U userA -f /pathA/xxx.sql
接着输入密码即可执行SQL文件到对应的db里。
外部表data_wrapper查询:
select * from pg_foreign_data_wrapper;
外部表server查询:
select * from pg_foreign_server;
外部表查询:
select * from pg_foreign_table;
case when 用法:
CASE
WHEN condition_1 THEN result_1
WHEN condition_2 THEN result_2
[WHEN ...]
[ELSE result_n]
END
在这种通用形式中,每个条件都是一个返回布尔值的表达式,true或者false。
如果条件的计算结果为true,则CASE表达式返回与条件对应的结果,而所有其他CASE分支根本不处理。
如果所有条件都计算为false,则CASE表达式将返回ELSE零件中的结果。如果省略该ELSE子句,则CASE表达式将返回null。
请注意,所有结果表达式必须具有可转换为单个数据类型的数据类型,例如字符串,数字和时间。
select id,case when name='a' then 'yes' when name='b' then 'no' else 'b' end from a where id >=20 and id <=30;
psql sql 时间类型:
select now()::timestamp without time zone;
now
----------------------------
2019-07-19 19:26:39.047424
select current_date,current_time;
current_date | current_time
--------------+--------------------
2019-07-19 | 19:27:33.102901+08
另一个非常重要的函数:
select extract(year from now());
date_part
-----------
2019
postgresql sql 界面执行备份:
\copy (select * from wi_discounts_activity) to /home/postgres/wi_discounts_activity_bak_0704;
或者 \copy wi_discounts_activity to /home/postgres/wi_discounts_activity_bak_0704;
使用SELECT查询
如果要将结果集复制到CSV文件,COPY命令的格式为:
COPY (<select-query-here>) TO <file-path>;
或者,一个更现实的例子:
COPY (SELECT * FROM people WHERE age > 21) TO '~/Desktop/adults.csv';
\set VERBOSITY verbose 设置详细输出,跟踪报错代码。
修改table 列字段类型:
alter ty_betting_details alter detail_id type bigint;
create table test(id int, info text, crt_time timestamp);
insert into test select generate_series(1,10000), md5(random()::text),clock_timestamp();
在线建索引不影响性能的:concurrently
CREATE INDEX concurrently index_at_account_create_time_account1 ON "GPO".at_account USING btree (create_time, account1);
修改表的列:
alter table t alter b type varchar(7);
查看物化视图:
select * from pg_matviews ;
给用户授权使用外部表服务:
grant usage on foreign server s1 to digoal;
##查询活动的会话:
select pid,usename,query_start,query,client_addr from pg_stat_activity where pid<>pg_backend_pid() and state='active';
## 查询等待事件
select pid,usename,datname,query,client_addr,wait_event_type,wait_event from pg_stat_activity where pid <>pg_backend_pid() and wait_event is not null;
查看当前数据库支持的类型:
select * from pg_am;
查看
select relkind,relname from pg_class where relnamespace = (select oid from pg_namespace where nspname='pg_catalog') and
relkind='r' order by 1,2; --(r =ordinary table, i = index, S = sequence, v = view, m = materialized
view, c = composite type, t = TOAST table, f = foreign table)
查看表对象的iod:(iod类似Oracle rownum)
selelct relname,oid from pg_class;
查看当前可用游标:
select * from pg_cursors;
数据库中使用create rule创建的规则信息:
select * from pg_rules;
查看当前正在写的XLOG:
select pg_current_xlog_location();
查看表对应的文件路径:
select * from pg_relation_filepath('test_1');
pg_relation_filepath
----------------------
base/32813/32849
pg_seclabels -- 安全标签信息
pg_settings -- 当前数据库集群的参数设置信息
pg_shadow -- 数据库用户信息
pg_stat_activity -- 会话活动信息
pg_stat_all_indexes -- 查询用户权限范围内的所有索引的统计信息
pg_stat_all_tables -- 查询用户权限范围内的所有表的统计信息
pg_stat_bgwriter -- bgwriter进程的统计信息
pg_stat_database -- 数据库级别的统计信息
pg_stat_database_conflicts -- 数据库
pg_stat_replication -- 流复制相关的统计信息
pg_stat_sys_indexes -- 系统表相关的索引统计信息
pg_stat_sys_tables -- 系统表统计信息
pg_stat_user_functions -- 用户函数统计信息
pg_stat_user_indexes -- 用户表的索引相关的统计信息
pg_stat_user_tables -- 用户表统计信息
查看语法帮助命令:
\h grant --例如查看grant 语法
\dnS 查看schema
GPO=# \dnS
List of schemas
Name | Owner
--------------------+----------
GPO | postgres
information_schema | postgres
pg_catalog | postgres
pg_temp_1 | postgres
pg_toast | postgres
pg_toast_temp_1 | postgres
public | postgres
(7 rows)
查看role密码:
select rolname,rolsuper,rolpassword from pg_authid;
创建用户:
CREATE USER dbuser WITH PASSWORD '*****';
GRANT ALL PRIVILEGES ON DATABASE exampledb TO dbuser;
now转换成time without time zone:
select now()::timestamp without time zone
select now(),now()::timestamp(0)+interval '1 day';
select date '2019-07-09' +interval '1 day';
?column?
---------------------
2019-07-10 00:00:00
select current_date,current_time;
date | timetz
------------+--------------------
2019-07-06 | 14:18:25.173986+08
(1 row)
select extract(year from now());
date_part
-----------
2019
postgresql 提供三种方式转化:通过格式化函数,cast函数,::操作符。
select cast(varchar '123' as int4);
int4
------
123
(1 row)
select cast(id as text) from test_serial;
id
----
1
2
3
4
5
6
(6 rows)
select 1::int4,3/2::numeric;
int4 | ?column?
------+--------------------
1 | 1.5000000000000000
(1 row)
GPO=# select * from test_bigserial
GPO-# ;
id | flag
----+------
1 | a
2 | b
3 | c
4 | d
5 | e
6 | f
(6 rows)
GPO=# select string_agg(flag,',') from test_bigserial;
string_agg
-------------
a,b,c,d,e,f
修改序列:
select max(draw_record_id) from mny_draw_record;
select nextval('mny_draw_record_id_seq');
select nextval('t_a_seq'::regclass);
ALTER SEQUENCE name [ INCREMENT [ BY ] increment ]
[ MINVALUE minvalue | NO MINVALUE ] [ MAXVALUE maxvalue | NO MAXVALUE ]
[ START [ WITH ] start ]
[ RESTART [ [ WITH ] restart ] ]
[ CACHE cache ] [ [ NO ] CYCLE ]
[ OWNED BY { table.column | NONE } ]
ALTER SEQUENCE name OWNER TO new_owner
ALTER SEQUENCE name RENAME TO new_name
ALTER SEQUENCE name SET SCHEMA new_schema
修改序列起始值:
alter sequence mny_draw_record_id_seq restart with 7
select version(); --查看版本
select pg_postmaster_start_time(); --查看数据库启动时间
select pg_conf_load_time(); --查看配置文件的时间
show timezone; --查看数据库时区
select user;
select current_user;
select session_user;
select current_catalog,current_database();
select inet_client_addr(),inet_client_port(); --查看session客户端登录IP,端口
inet_client_addr | inet_client_port
------------------+------------------
192.168.211.152 | 36891
select inet_server_addr(),inet_server_port(); --查看数据库服务器登录IP,端口
inet_server_addr | inet_server_port
------------------+------------------
192.168.211.152 | 5432
select pg_backend_pid(); 查看当前session的后台服务器进程的PID
show shared_buffers; --查看共享buffer
select current_setting('shared_buffers'); --查看当前shared_buffers
select pg_xlogfile_name(pg_current_xlog_location()); --查看当前正在写的WAL文件
pg_xlogfile_name
--------------------------
000000010000000900000098
(1 row)
select pg_is_in_backup(),pg_backup_start_time(); --查看实例是否正在做基础备份
select pg_is_in_recovery(); --查看实例时hot standby 状态还是正常数据库状态
select pg_database_size('test'),pg_size_pretty(pg_database_size('test')); --查看test数据库大小(显示会慢)
select pg_size_pretty(pg_relation_size('test1')); --查看连接数据库test1表的大小(不包括索引)
select pg_size_pretty(pg_total_relation_size('test1')); --查看连接数据库test1表的大小(包括索引)
select pg_size_pretty(pg_indexes_size('test1')); --查看表test1上索引的大小
select pg_size_pretty(pg_tablespace_size('test')); 查看test表空间的使用大小
select pg_relation_filepath('test1'); --查看test1表对应的数据文件
select * from pg_settings where name like '%wal%'; --查看参数
select pg_database.datname, pg_size_pretty (pg_database_size(pg_database.datname)) AS size from pg_database; --查看所有的数据库的大小
select * from pg_tablespace; --查看数据库所有表空间
select datname,numbackends,blks_read,blks_hit,xact_commit,xact_rollback,deadlocks,stats_reset from pg_stat_database; --查看当前连接数
select datname,blks_hit::float/(blks_read+blks_hit)::float as cache_hit_ratio from pg_stat_database where datname='GPO' --查看缓存命中率
select xact_commit::float/(xact_commit+xact_rollback) as sucessfull_xact_raito from pg_stat_database where datname='GPO' --查看事务提交率
select pg_stat_reset(); 建议修改完优化之后执行,方便对比优化和调整前后的和各项指标。
查看索引扫描次数:
select
relname, indexrelname, idx_scan, idx_tup_read, idx_tup_fetch from
pg_stat_user_indexes order by idx_scan asc, idx_tup_read asc, idx_tup_fetch asc;
查看某个表索引扫描次数:
select
relname, indexrelname, idx_scan, idx_tup_read, idx_tup_fetch from
pg_stat_user_indexes where
relname = table_name order by idx_scan asc, idx_tup_read asc, idx_tup_fetch asc;
查看schema下索引扫描总比例:
select round(sum(idx_scan)/(sum(idx_scan) + sum(seq_scan)),4) as index_scan_ratio from pg_stat_all_tables where schemaname='GPO';
查看schema下表索引扫描比例:
select relname,idx_scan::float/(idx_scan+seq_scan+1) as idx_scan_ratio from pg_stat_all_tables where schemaname='GPO' order by 2 asc;
查看平均执行SQL时间:
select calls,total_time/calls as avg_time from pg_stat_statements order by 2 desc limit 10;
select calls,total_time/calls as avg_time,left(query,80) from pg_stat_statements order by 2 desc limit 10;
sql 调整完后,执行select pg_stat_statements_reset(); 充值pg_stat_statements;的统计信息
SELECT pg_stat_statements_reset();
spcname | spcowner | spcacl | spcoptions
------------+----------+---------------+------------
pg_default | 10 | |
pg_global | 10 | |
tbs_test | 16384 | |
test | 16384 | {test=C/test} |
select * from pg_user; --查看PG用户。
select oid,datname from pg_database; 查看数据库的oid
select oid,datname from pg_database;
oid | datname
-------+-----------
13275 | postgres
1 | template1
13274 | template0
16385 | test
select txid_current(); --查看当前事务ID
txid_current
--------------
1803
(4 rows)
查看数据库所有表:
select * from pg_tables;
SHOW data_directory; --查看默认数据文件路径
二、系统维护常用命令
修改配置文件postgresql.conf后,生效的两种方式:
1.pg_ctl reload
2.select pg_reload_conf();
能生效的不需要重启服务器。
切换日志文件
select pg_rotate_logfile();
PostgreSQL手动切换WAL日志的命令:
在PG10之前:
select pg_switch_xlog();
在PG10之后:
select pg_switch_wal();
3.取消正在执行的长时间的SQL方法
pg_cancel_backend(pid):取消正在执行的SQL
pg_terminate_backend(pid):终止一个后台服务进程,同时释放此后台服务进程的资源。
select pid ,datname,usename,query_start,query from pg_stat_activity; --查看运行的SQL
select pg_cancel_backend(pid); 如果取消不了,用这一句取消:select pg_terminate_backend(28184);
WAL日志维护
1. 参数max_wal_size/min_wal_size
9.5以前: (2 + checkpoint_completion_target) * checkpoint_segments + 1
9.5:PostgreSQL 9.5 将废弃checkpoint_segments 参数, 并引入max_wal_size 和 min_wal_size 参数,
通过max_wal_size和checkpoint_completion_target 参数来控制产生多少个XLOG后触发检查点,
通过min_wal_size和max_wal_size参数来控制哪些XLOG可以循环使用。
2. 参数wal_keep_segments
在流复制的环境中。使用流复制建好备库,如果备库由于某些原因接收日志较慢。导致备库还未接收到。就被覆盖了。导致主备无法同步。这个需要重建备库。
避免这种情况提供了该参数。每个日志文件大小16M。如果参数设置64. 占用大概64×16=1GB的空间。根据实际环境设置。
3. pg_resetxlog
在前面参数设置合理的话。是用不到pg_resetxlog命令。
使用案例参考: https://my.oschina.net/Kenyon/blog/101432
[postgres@postgres128 ~]$ pg_resetxlog -?
pg_resetxlog resets the PostgreSQL transaction log.
三.常规操作
创建表空间(创建完后,会在目录下生成)
create database test encoding='UTF8';
create tablespace test owner test location '/pgsql/pg_data/test/'; --注意,如果是级联复制,备库一定要先创建好表空间的目录,不然备库会宕机,影响查询业务
create table test_tbs (id int, info text, crt_time timestamp) tablespace fast_tb;
GRANT CREATE ON TABLESPACE tsp01 TO rax;
grant all privileges on database test to test;
ls -lh
total 4.0K
lrwxrwxrwx 1 postgres postgres 24 Jun 26 09:29 16387 -> /pgsql/pg_data/pg_tblspc
drwx------ 2 postgres postgres 4.0K Jun 26 09:29 PG_9.6_201608131
ALTER USER role_specification [ WITH ] option [ ... ]
where option can be:
SUPERUSER | NOSUPERUSER
| CREATEDB | NOCREATEDB
| CREATEROLE | NOCREATEROLE
| INHERIT | NOINHERIT
| LOGIN | NOLOGIN
| REPLICATION | NOREPLICATION
| BYPASSRLS | NOBYPASSRLS
| CONNECTION LIMIT connlimit
| [ ENCRYPTED ] PASSWORD 'password' | PASSWORD NULL
| VALID UNTIL 'timestamp'
ALTER USER name RENAME TO new_name
ALTER USER { role_specification | ALL } [ IN DATABASE database_name ] SET configuration_parameter { TO | = } { value | DEFAULT }
ALTER USER { role_specification | ALL } [ IN DATABASE database_name ] SET configuration_parameter FROM CURRENT
ALTER USER { role_specification | ALL } [ IN DATABASE database_name ] RESET configuration_parameter
ALTER USER { role_specification | ALL } [ IN DATABASE database_name ] RESET ALL
where role_specification can be:
role_name
| CURRENT_USER
| SESSION_USER
三,replication slots 的使用
3.1 主库设置以下,并需要重启
max_replication_slots = 4 # max number of replication slots
wal_level = hot_standby
3.2重启主库
pg_ctl -D /usr/local/postgresql/data restart
3.3在主库上创建slot
postgres=# select * from pg_create_physical_replication_slot('pg55_5434');
slot_name | xlog_position
-----------+---------------
slot49 |
(1 row)
3.4 在从库上加入设置 recovery.conf
primary_slot_name = 'slot49'
并重启从库
附:从库的recovery.conf 文件设置primary_slot_name 参数,指向主库创建的'slot49'
3.5在主库上查看是否设置成功
postgres=# select * from pg_replication_slots ;
slot_name | plugin | slot_type | datoid | database | active | active_pid | xmin | catalog_xmin | restart_lsn | confirmed_flush_lsn
-----------+--------+-----------+--------+----------+--------+------------+------+--------------+-------------+---------------------
slot49 | | physical | | | t | 4965 | | | 0/3F000290 |
(1 row)
附:active表示是否使用,如果想关闭这个replication,那么建议删除刚创建的slots
3.6删除slot
在slot处于活跃状态的时候是不能被删除的,首先需要在从库中将配置参数primary_slot_name 去掉,并重启,使其活跃状态失效。
aubu=# select pg_drop_replication_slot('slot49');
pg_drop_replication_slot
--------------------------
(1 row)
select * from pg_replication_slots ;
slot_name | plugin | slot_type | datoid | database | active | active_pid | xmin | catalog_xmin | restart_lsn | confirmed_flush_lsn
-----------+--------+-----------+--------+----------+--------+------------+------+--------------+-------------+---------------------
(0 rows)
至此,整个过程配置完成
vi recovery.conf
primary_slot_name = 'pg155_5434'
standby_mode = 'on'
recovery_target_timeline = 'latest'
primary_conninfo = 'user=postgres password=qwe123 host=10.14.39.155 port=5434 application_name=pg_5434'
hot_standby_feedback(布尔值)默认值为off:
指定热备用服务器是否将向主服务器或上游备用服务器发送有关当前在备用服务器上执行的查询的反馈。
此参数可用于消除由清理记录引起的查询取消,但可能导致某些工作负载的主数据库膨胀。每个wal_receiver_status_interval不会比一次更频繁地发送反馈消息。
默认值为off。此参数只能在postgresql.conf文件或服务器命令行中设置。
如果正在使用级联复制,则反馈将在上游传递,直到最终到达主数据库。除了通过上游之外,备用服务器不会使用他们收到的反馈。
此设置不会覆盖主服务器上old_snapshot_threshold的行为; 备用数据库上超过主要年龄阈值的快照可能会变为无效,从而导致备用数据库上的事务被取消。
这是因为old_snapshot_threshold旨在提供对死行可能导致膨胀的时间的绝对限制,否则由于备用数据库的配置而会被违反。
max_standby_archive_delay = 900s
max_standby_streaming_delay = 900s