Skip to content

Instantly share code, notes, and snippets.

View rapimo's full-sized avatar

Manuel Kniep rapimo

View GitHub Profile
@rapimo
rapimo / test.sql
Last active January 10, 2019 15:26
postgres case vs. hstore vs. istore
-- sample data
set max_parallel_workers_per_gather=1;
CREATE TABLE data AS
SELECT n, chr(n) as g, i FROM generate_series(97,122) n, generate_series(1,1e6) i ORDER BY i;
SELECT CASE g
WHEN 'a' THEN 'one'
WHEN 'b' THEN 'one'
WHEN 'c' THEN 'one'
@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"
CREATE EXTENSION istore;
CREATE TABLE data AS
SELECT (i%30)::int as event_id, (random()*100 + 10)::int as events
FROM generate_series(1,1e6) i;
SELECT
SUM(events) FILTER (WHERE event_id = 0) as "0_events",
SUM(events) FILTER (WHERE event_id = 1) as "1_events",
SUM(events) FILTER (WHERE event_id = 2) as "2_events",
SUM(events) FILTER (WHERE event_id = 3) as "3_events",
@rapimo
rapimo / istore_case.sql
Created January 22, 2016 15:54
istore beats case
\timing
CREATE EXTENSION istore;
CREATE TABLE foo AS SELECT i%10 as id, i FROM generate_series(1,1e7::int) i;
SELECT CASE
WHEN id = 1 THEN 10 WHEN id = 2 THEN 20 WHEN id = 3 THEN 30 WHEN id = 4 THEN 40 WHEN id = 5 THEN 50
WHEN id = 6 THEN 60 WHEN id = 7 THEN 70 WHEN id = 8 THEN 80 WHEN id = 9 THEN 90
ELSE 0 END,
@rapimo
rapimo / apt.postgresql.org.sh
Last active January 2, 2016 09:49
scripts to test postgres extension in travis cicombined from https://gist.github.com/petere/5893799 and https://gist.github.com/petere/6023944/
#!/bin/sh
# script to add apt.postgresql.org to sources.list
# from command like
CODENAME="$1"
# lsb_release is the best interface, but not always available
if [ -z "$CODENAME" ]; then
CODENAME=$(lsb_release -cs 2>/dev/null)
fi
@rapimo
rapimo / gist:7263779
Created November 1, 2013 10:49
postgres order of columns matters
CREATE TABLE small (
a smallint,
b smallint,
c integer,
d integer,
e date
)
CREATE TABLE big (
c integer,
@rapimo
rapimo / gist:5635977
Created May 23, 2013 13:15
creating a ramdisk to use for postgres
# create ramdisk of 500MB (500 x 2048)
diskutil erasevolume HFS+ "ramdisk" `hdiutil attach -nomount ram://1024000`
# create pg tablespace to use ramdisk
psql -c "CREATE TABLESPACE ramdisk LOCATION '/Volumes/ramdisk/'" postgres
# delete ramdisk
hdiutil eject /Volumes/ramdisk/
@rapimo
rapimo / fixBrewLionPostgres.sh
Created February 18, 2013 07:49
according to http://nextmarvel.net/blog/2011/09/brew-install-postgresql-on-os-x-lion/ this script moves your OS X default postgres binaries into an archive folder and symlinks the homebrew versions in place of them
#!/bin/sh
BREW_POSTGRES_DIR=`brew info postgres | awk '{print $1"/bin"}' | grep "/postgresql/"`
LION_POSTGRES_DIR=`which postgres | xargs dirname`
LION_PSQL_DIR=`which psql | xargs dirname`
sudo mkdir -p $LION_POSTGRES_DIR/archive
sudo mkdir -p $LION_PSQL_DIR/archive
for i in `ls $BREW_POSTGRES_DIR`
@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"