Skip to content

Instantly share code, notes, and snippets.

View rapimo's full-sized avatar

Manuel Kniep rapimo

View GitHub Profile
@rapimo
rapimo / gist:4094917
Created November 17, 2012 11:02
postgres kernel ressources
get https://github.com/gregs1104/pgtune
see http://www.postgresql.org/docs/8.4/static/kernel-resources.html for reasonable kernel conf
play around with kernel settings without restart
sudo sysctl -w kern.sysv.shmmax=4409466880 #4GB must be a multiplier of 4096
kern.sysv.shmmax: 4194304 -> 1073741824
shmall must be shmax / pagesize
get pagesize with
@rapimo
rapimo / aggregate.sql
Created October 26, 2012 09:01
postgres hstore aggregation
Select (SELECT hstore(array_agg(v), array_agg(c::text)) FROM (
SELECT v, COUNT(*) as c from unnest(array_agg(country)) v GROUP BY v) t
) countries FROM test;
-- => "de"=>"10669", "en"=>"21542", "fr"=>"21573", "it"=>"10783", "us"=>"21774"
@rapimo
rapimo / ar_null_object.rb
Created August 7, 2012 11:02
implementation for the NullObject Pattern in ActiveRecord
module ActiveRecord
module NullObject #:nodoc:
module InstanceMethods
# should quark like a nil
def nil?
true
end
def present?
false
@rapimo
rapimo / gist:3250341
Created August 3, 2012 18:44
count occurrences of array values in postgres using hstore
SELECT hstore(array_agg(v), array_agg(c::text)) FROM (
SELECT v, COUNT(*) as c ,1 as agg from unnest(ARRAY['foo','bar','baz','foo']) v GROUP BY v) t
GROUP BY agg
--> "bar"=>"1", "baz"=>"1", "foo"=>"2"
@rapimo
rapimo / gist:3183297
Created July 26, 2012 17:20
Find useless indexes on postres
SELECT idstat.relname AS table_name,
indexrelname AS index_name,
idstat.idx_scan AS times_used,
pg_size_pretty(pg_relation_size(idstat.relname::regclass)) AS table_size, pg_size_pretty(pg_relation_size(indexrelname::regclass)) AS index_size,
n_tup_upd + n_tup_ins + n_tup_del as num_writes,
indexdef AS definition
FROM pg_stat_user_indexes AS idstat JOIN pg_indexes ON indexrelname = indexname
JOIN pg_stat_user_tables AS tabstat ON idstat.relname = tabstat.relname
WHERE idstat.idx_scan < 200
AND indexdef !~* 'unique'
@rapimo
rapimo / gist:3182134
Created July 26, 2012 13:52
change ENCODING for template1
-- Postgres makes it difficult four you to shoot yourselves in the foot
-- So it is not allowed to delete template databases
UPDATE pg_database SET datistemplate = FALSE where datname = 'template1';
DROP DATABASE template1;
CREATE DATABASE template1 WITH template = template0 encoding = 'utf8';
UPDATE pg_database SET datistemplate = TRUE where datname = 'template1';
@rapimo
rapimo / array_add.rb
Created July 4, 2012 09:32
add two arrays
class Array
# [1,2,3].add [1,2,3] => [2, 4, 6]
def add (other)
self.zip(other).map{|a| a.inject(0){|s,n| s+=n || 0;s }}
end
end
@rapimo
rapimo / cracker.rb
Created May 8, 2012 07:19
crackme solution in ruby
require "benchmark"
class Cracker
class << self
## runs the code retuning sek runtime
def run(test)
Benchmark.realtime{`./crackme #{test}`}
end
@rapimo
rapimo / print_mail
Created March 12, 2012 14:27
print email addresses sent out from logfile
cat /var/log/mail.log | grep "status=sent" | awk '{print $7}' | awk '{ sub(/^to=</, ""); print }' | awk '{ sub(/>,$/, ""); print }'
@rapimo
rapimo / rails_start.scpt
Created February 21, 2012 17:18
iterm2 AppleScript for my Rails Project
-- ~/Library/Application\ Support/iTerm/Scripts/setup_rails.scpt
-- Thanks to http://www.worldgoneweb.com/2011/iterm2-advanced-features/#comment-20560
-- http://www.iterm2.com/#/section/documentation/scripting
tell application "iTerm"
activate
set myterm to (make new terminal)
set cd_to_my_project_path to ("cd ~/Projekte/my_awesome_project")
-- you can altenativly tell the first terminal
tell the first terminal
launch session "Panes"