Skip to content

Instantly share code, notes, and snippets.

View NikolayS's full-sized avatar
🐘
Need help with Postgres? Let me know!

Nikolay Samokhvalov NikolayS

🐘
Need help with Postgres? Let me know!
View GitHub Profile
sudo add-apt-repository 'deb http://apt.postgresql.org/pub/repos/apt/ xenial-pgdg main'
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
sudo apt-get install postgresql-client-10
@NikolayS
NikolayS / alignment_padding_analysis.sql
Last active January 8, 2018 01:17
Bloat estimator test
with recursive constants as (
select 8 as chunk_size
), columns as (
select
table_schema,
table_name,
ordinal_position,
column_name,
udt_name,
typalign,
@NikolayS
NikolayS / ddl.sql
Last active February 23, 2018 14:40
Typeform Callback Processing in Postgres
-- table to store typeform responses
create table typeform (
id bigserial primary key,
created_at timestamptz default clock_timestamp(),
data jsonb
);
-- API function (PostgREST) to process callback and save data.
-- In TypeForm's interfaces, you need to setup callback, using URL: http://your_nginx_address/rpc/typeform
create or replace function v1.typeform*(q json) returns json as $$
@NikolayS
NikolayS / gist:1deefd26fa45c86d5963db070dc07179
Last active November 3, 2017 01:21
make all constraints DEFERRABLE
-- use this to make all constraints in "public" schema `deferrable initially immediate`
-- (edit schema name if needed)
do $$
declare
sql text;
begin
select into sql
string_agg(
'alter table ' || quote_ident(ns.nspname) || '.' || quote_ident(tb.relname)
@NikolayS
NikolayS / HowToTroubleshootAPI.md
Last active August 6, 2017 05:52
API Troubleshooting: How to Report a Bug

If you have any issue with REST API, please provide:

1) Full HTTP request:

  • HTTP method (GET, POST, ..)
  • Protocol+hostname+port (important! was it dev, staging, or production? Example: https://apiserver:4321
  • URI (examples: /people?id=eq.1, /rpc/some_function?user_id=eq.1)
  • All request headers
  • Payload (in case of POST, or PUT, or PATCH, usually in JSON format)

2) The actual response from the server:

@NikolayS
NikolayS / 1_boilerplate.sql
Last active June 8, 2017 22:59
Delete duplicates with using most recent "last created" timestamps for remaining records
drop table if exists moscow_weather;
create table moscow_weather (
id bigserial not null primary key,
year int2 not null,
month int2 not null,
created timestamptz not null default clock_timestamp(),
weather_is_fine boolean
);
@NikolayS
NikolayS / latency.txt
Created June 3, 2017 17:24 — forked from jboner/latency.txt
Latency Numbers Every Programmer Should Know
Latency Comparison Numbers
--------------------------
L1 cache reference 0.5 ns
Branch mispredict 5 ns
L2 cache reference 7 ns 14x L1 cache
Mutex lock/unlock 25 ns
Main memory reference 100 ns 20x L2 cache, 200x L1 cache
Compress 1K bytes with Zippy 3,000 ns 3 us
Send 1K bytes over 1 Gbps network 10,000 ns 10 us
Read 4K randomly from SSD* 150,000 ns 150 us ~1GB/sec SSD
@NikolayS
NikolayS / ban_n_delete.md
Last active May 1, 2017 19:02
ban+delete CTE
with usr as (
  select 1111111 as id  -- <<< EDIT USER ID HERE!
), collections_d as (
  delete from collection
  where user_id in (select id from usr) returning id
), posts_d as (
  delete from post
  where user_id in (select id from usr) returning id
), comments_d as (
@NikolayS
NikolayS / 01_check_schema_sql.sh
Last active May 6, 2019 01:44
Postgres + Ruby / Ruby on Rails
# This one-liner compares your local RoR's dump (db/structure.sql) with a remote DB's schema (Postgres)
#
# It ignores:
# - ownership & privileges information
# - empty lines
# - SQL comments
# - data in the "migrations" table from db/structure.sql
pg_dump --no-owner --no-privileges --schema-only -U username -h hostname dbname \
| egrep -v '^--|^$' | diff -b <(egrep -v '^INSERT INTO|^--|^$' db/structure.sql) -
@NikolayS
NikolayS / gist:520829b846827098c6d185827a5955e8
Created March 14, 2017 22:12
Postgres config recommendations for VM (1 vCPU, ~4GB RAM)
shared_buffers = 900MB
work_mem = 32MB
maintenance_work_mem = 128MB
checkpoint_timeout = 10min
checkpoint_completion_target = 0.7
effective_cache_size = 2.8GB
log_min_duration_statement = 100ms