Skip to content

Instantly share code, notes, and snippets.

@kjunichi
Last active October 13, 2017 03:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kjunichi/5478825 to your computer and use it in GitHub Desktop.
Save kjunichi/5478825 to your computer and use it in GitHub Desktop.
プライベートではPostgreSQL一筋のわりには全然使いこなせていないw

関連記事

言語別準備

Perl

cpan install DBD::Pg

Ruby

gem install pg

OCaml

opam install postgresql-ocaml

DBD::Pg

取得件数を調べるには

$sth->execute
$sth->rows

と出来るが、DBD::Pgに関しては、SELECTのEXECUTEの返却値に件数が入っている。(俺の英語力での理解) (ほかのDBDモジュールではそもそもrowsで取得件数が入らないこともありそうな気配。。)

rows                                                                                                     
                                                                                                                
         $rv = $sth->rows;                                                                                      
                                                                                                                
       Returns the number of rows returned by the last query. In contrast to many other DBD modules, the        
       number of rows is available immediately after calling "$sth->execute". Note that the "execute" method    
       itself returns the number of rows itself, which means that this method is rarely needed.    

後から変える系

alter table black_domain alter column itimestamp set default current_timestamp;

調べる系

ディスク使用量

select pg_database_size(’databasename’);
select relname,relpages,to_char(reltuples ,'999,999,999,999') from pg_class order by relpages desc;

実行中のSQLを調べる

select
 procpid,
 start,
 now()-start as lap,
 current_query 
from                                                     
 (
  select
   backendid,
   pg_stat_get_backend_pid(S.backendid) as procpid,                                                         
   pg_stat_get_backend_activity_start(S.backendid) as start,                                                                  
   pg_stat_get_backend_activity(S.backendid) as current_query                                                                 
  from
   (
    select
     pg_stat_get_backend_idset() as backendid
   ) as S 
 ) as S                                                         
where
 current_query <> '<IDLE>' 
order by
 lap desc;

関連Gist

アクセス解析タグ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment