现在的位置: 首页 > 综合 > 正文

Postgres 技巧

2018年09月07日 ⁄ 综合 ⁄ 共 5280字 ⁄ 字号 评论关闭
文章目录

在业务量大的时候 碰到需要添加索引,需要用到 CONCURRENTLY,不然你的库很容易会坏的

如 

create index CONCURRENTLY idx_order_records_bank_statist_uid on order_records(statist_uid)

http://www.postgresql.org/docs/9.1/static/sql-createindex.html

在大的业务量下,如果你要去执行一些费时耗数据库的任务 有时会发现 某一个任务 还在暗地里执行着 需要我们 手动的 关闭 该任务

如果你是 kill 对应的pid ,那么你玩了,你很有可能会出现 数据库的问题

保险的做法是 用 PG_CANCEL_BACKEND

查看死锁

SELECT * FROM pg_stat_activity WHERE datname='死锁的数据库ID ';

检索出来的字段中,【wating 】字段,数据为t的那条,就是死锁的进程。找到对应的【procpid 】列的值

select oid,relname from pg_class where relname='table name';

select locktype,pid,relation,mode,granted,* from pg_locks where relation= '上面查询出来的oid';

select pg_cancel_backend('进程ID');

pg_terminate_backend 也可以

通过 如下查找对应的PID

select pid, trim(starttime) as start, 
duration, trim(user_name) as user,
substring (query,1,40) as querytxt
from stv_recents
where status = 'Running';

查看当前连接数据

SELECT count(*) FROM pg_stat_activity;


查看最大连接数

show max_connections;

超级用户预留连接数


show superuser_reserved_connections ;

查看

select pg_cancel_backend(802);

通过如下 杀掉 任务

pg_cancel_backend( pid )

http://docs.aws.amazon.com/redshift/latest/dg/PG_CANCEL_BACKEND.html

pg gem 安装出错

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    /usr/local/rvm/rubies/ruby-2.1.1/bin/ruby extconf.rb 
checking for pg_config... no
No pg_config... trying anyway. If building fails, please try again with
 --with-pg-config=/path/to/pg_config
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
	--with-opt-dir
	--without-opt-dir
	--with-opt-include
	--without-opt-include=${opt-dir}/include
	--with-opt-lib
	--without-opt-lib=${opt-dir}/lib
	--with-make-prog
	--without-make-prog
	--srcdir=.
	--curdir
	--ruby=/usr/local/rvm/rubies/ruby-2.1.1/bin/ruby
	--with-pg
	--without-pg
	--with-pg-dir
	--without-pg-dir
	--with-pg-include
	--without-pg-include=${pg-dir}/include
	--with-pg-lib
	--without-pg-lib=${pg-dir}/lib
	--with-pg-config
	--without-pg-config
	--with-pg_config
	--without-pg_config

extconf failed, exit code 1

Gem files will remain installed in /usr/local/rvm/gems/ruby-2.1.1/gems/pg-0.14.1 for inspection.
Results logged to /usr/local/rvm/gems/ruby-2.1.1/extensions/x86_64-linux/2.1.0/pg-0.14.1/gem_make.out
An error occurred while installing pg (0.14.1), and Bundler cannot continue.
Make sure that `gem install pg -v '0.14.1'` succeeds before bundling.
root#fgtocashe (2.1.1) (master) $ exit

设置: 

export PATH=$PATH:/usr/pgsql-9.2/bin/

安装:

yum install libpqxx-devel

OK, 

bundle

 ERROR: connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?

:/usr/local$createuser -d -a -P postgres
Enter password for new role: 
Enter it again: 
createuser: could not connect to database postgres: could not connect to server: No such file or directory
	Is the server running locally and accepting
	connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?

:/usr/local$createuser -d -a -P postgres
Enter password for new role: 
Enter it again: 
:/usr/local$

postgis

http://windstill.me/blog/2014/10/15/rails-with-postgis/

http://blog.sina.com.cn/s/blog_61b9ba1d0100rw1t.html

http://francs3.blog.163.com/blog/static/4057672720132295337690/

数据导出

copy(select mobile_num,to_char(created_at,'yyyy-MM-DD HH:mm:ss'),province,'huafei',amount,pay_status from order_records_2010_1) TO STDOUT CSV;

copy(select mobile_num,to_char(created_at,'yyyy-MM-DD HH:mm:ss'),province,'huafei',amount,pay_status from order_records_2010_2) TO STDOUT CSV;

copy(select mobile_num,to_char(created_at,'yyyy-MM-DD HH:mm:ss'),province,'huafei',amount,pay_status from order_records_2011_1) TO STDOUT CSV;

copy(select mobile_num,to_char(created_at,'yyyy-MM-DD HH:mm:ss'),province,'huafei',amount,pay_status from order_records_2011_2) TO STDOUT CSV;

copy(select mobile_num,to_char(created_at,'yyyy-MM-DD HH:mm:ss'),province,'huafei',amount,pay_status from order_records_2012_1) TO STDOUT CSV;

copy(select mobile_num,to_char(created_at,'yyyy-MM-DD HH:mm:ss'),province,'huafei',amount,pay_status from order_records_2012_2) TO STDOUT CSV;

copy(select mobile_num,to_char(created_at,'yyyy-MM-DD HH:mm:ss'),province,'huafei',amount,pay_status from order_records_2013_1) TO STDOUT CSV;

copy(select mobile_num,to_char(created_at,'yyyy-MM-DD HH:mm:ss'),province,'huafei',amount,pay_status from order_records_2013_2) TO STDOUT CSV;

copy(select mobile_num,to_char(created_at,'yyyy-MM-DD HH:mm:ss'),province,'huafei',amount,pay_status from order_records_2014_1) TO STDOUT CSV;

copy(select mobile_num,to_char(created_at,'yyyy-MM-DD HH:mm:ss'),province,'huafei',amount,pay_status from order_records_2014_2) TO STDOUT CSV;

copy(select mobile_num,to_char(created_at,'yyyy-MM-DD HH:mm:ss'),province,'huafei',amount,pay_status from order_records) TO STDOUT CSV;

快速导出数据 小笔记下次不需手动拼sql 了

psql -h host -U huafei -p 5432  -d huafei3_production   -A -F , -c "select 
order_records.order_no as order_no,
order_records.mobile_num as charge_no,
order_records.pay_mobile_num as pay_no,
order_records.province as province,
order_records.city as city,
order_records.merchant as carrier,
order_records.amount as order_amount,
coalesce(pay_code_logs.amount,0) as coupon_amount,
coalesce(pay_code_logs.pay_code_no,null)  as coupon_no,
order_records.statist_ver as app_ver 
from 
order_records
left join pay_code_logs on pay_code_logs.order_record_id = order_records.id
where
order_records.created_at between '2015-03-01 00:00:00' and '2015-03-31 23:59:59'
and order_records.pay_status = 1"  > cmb201503.csv

psql huafei_production -U pgsql -h host -A -F , -c "select mobile_num,to_char(created_at,'yyyy-MM-DD HH:mm:ss'),province,'huafei',amount,pay_status from order_records_2010_1"

抱歉!评论已关闭.