Skip to content

Instantly share code, notes, and snippets.

@marklit
Last active August 20, 2018 03:56
Show Gist options
  • Select an option

  • Save marklit/3a8000c9ad442be6031d to your computer and use it in GitHub Desktop.

Select an option

Save marklit/3a8000c9ad442be6031d to your computer and use it in GitHub Desktop.
Postgres 9.5 on Ubuntu 15
-- From: http://michael.otacoo.com/postgresql-2/postgres-9-5-feature-highlight-brin-indexes/
CREATE TABLE brin_example AS SELECT generate_series(1,100000000) AS id;
CREATE INDEX btree_index ON brin_example(id);
-- By default, the block size is 128 pages. This resolution can be increased or decreased using the pages_per_range
CREATE INDEX brin_index ON brin_example USING brin(id);
CREATE INDEX brin_index_64 ON brin_example USING brin(id) WITH (pages_per_range = 64);
CREATE INDEX brin_index_256 ON brin_example USING brin(id) WITH (pages_per_range = 256);
CREATE INDEX brin_index_512 ON brin_example USING brin(id) WITH (pages_per_range = 512);
-- The lower the pages per block, the more space the index will occupy, but the less lossy the index will be, i.e. it will need to discard fewer rows.
SELECT relname, pg_size_pretty(pg_relation_size(oid))
FROM pg_class WHERE relname LIKE 'brin_%' OR
relname = 'btree_index' ORDER BY relname;
-- relname | pg_size_pretty
-- ----------------+----------------
-- brin_example | 3457 MB
-- brin_index | 104 kB
-- brin_index_256 | 64 kB
-- brin_index_512 | 40 kB
-- brin_index_64 | 192 kB
-- btree_index | 2142 MB
EXPLAIN ANALYZE SELECT id FROM brin_example WHERE id = 52342323;
echo "deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main 9.5" | sudo tee /etc/apt/sources.list.d/postgresql.list
gpg --keyserver pgp.mit.edu --recv-keys 7FCC7D46ACCC4CF8
gpg --armor --export 7FCC7D46ACCC4CF8 | sudo apt-key add -
sudo apt-get update
sudo apt-get install postgresql-9.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment