Skip to content

Instantly share code, notes, and snippets.

@sanjaypojo
sanjaypojo / stored_function.sql
Created August 2, 2017 19:05
Postgres Stored Function Template (plpgsql)
DROP FUNCTION IF EXISTS my_stored_function(integer, varchar, varchar );
CREATE OR REPLACE FUNCTION table_space.my_stored_function (var1 integer, var2 varchar, var3 varchar)
RETURNS TABLE (col1 integer, col2 integer, col3 varchar, col4 varchar)
AS
$BODY$
DECLARE
local_variable integer;
@sanjaypojo
sanjaypojo / table_size_query.sql
Created August 1, 2017 22:40
Postgres - Check table sizes
SELECT *, pg_size_pretty(total_bytes) AS total
, pg_size_pretty(index_bytes) AS INDEX
, pg_size_pretty(toast_bytes) AS toast
, pg_size_pretty(table_bytes) AS TABLE
FROM (
SELECT *, total_bytes-index_bytes-COALESCE(toast_bytes,0) AS table_bytes FROM (
SELECT c.oid,nspname AS table_schema, relname AS TABLE_NAME
, c.reltuples AS row_estimate
, pg_total_relation_size(c.oid) AS total_bytes
, pg_indexes_size(c.oid) AS index_bytes
Latency Comparison Numbers Time Light Distance Approximate Light Distance
-------------------------- ---- -------------- --------------------------
L1 cache reference 0.5 ns 0.15 m Diagonal across your smartphone
Branch mispredict 5 ns 1.5 m Height of Natalie Portman
L2 cache reference 7 ns 2.1 m Height of Shaq
Mutex lock/unlock 25 ns 7.5 m Height of a school flag pole
Main memory reference 100 ns 30 m Half a Manhattan city block (North/South)
Compress 1K bytes with Zippy 3,000 ns 900 m Width of Central Park
Send 1K bytes over 1 Gbps network 10,000 ns 3,000 m Width of Manhattan
Read 4K randomly from SSD* 150,000 ns 45,000 m NYC to Hempstead on Long Island