在业务量大的时候 碰到需要添加索引,需要用到 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
检索出死锁进程的ID
SELECT * FROM pg_stat_activity WHERE datname='死锁的数据库ID ';
将进程杀掉
SELECT pg_cancel_backend('死锁那条数据的procpid值 ');
pg_locks是否有这个对象的锁
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');
or
select pg_terminate_backend('进程ID')
explain 优化
使用explain语句分析语句的执行计划,查看索引使用情况
explain analyze select * from test_table where name='mendd'
EXPLAIN引用的数据是:
1). 预计的启动开销(在输出扫描开始之前消耗的时间,比如在一个排序节点里做排续的时间)。
2). 预计的总开销。
3). 预计的该规划节点输出的行数。
4). 预计的该规划节点的行平均宽度(单位:字节)
transaction 优化并发
START TRANSACTION:此命令表示开始一个新的事务块.
BEGIN:初始化一个事务块。在BEGIN命令后的语句都将在一个事务里面执行,知道遇见COMMIT或ROLLBACK。它和START TRANSACTION是一样的。
COMMIT:提交事务。
ROLLBACK:事务失败时执行回滚操作。
SET TRANSACTION:设置当前事务的特性。对后面的事务没有影响。
http://my.oschina.net/Kenyon/blog/85395 实用日常维护SQL
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
bundle
:/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$
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;
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"
INSERT INTO order_records_2014_2 select * from order_records where indexable_timestamp(updated_at, 'YYYY-MM'::character varying) between '2014-07' and '2014-12';
delete from order_records where indexable_timestamp(updated_at, 'YYYY-MM'::character varying) between '2014-07' and '2014-12';