Skip to content

Instantly share code, notes, and snippets.

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

Nik Samokhvalov NikolayS

🐘
Need help with Postgres? Let me know!
View GitHub Profile
@NikolayS
NikolayS / howto.md
Last active January 29, 2023 20:25
log_min_duration_statement = 0 and I/O impact

How to get an estimate of the impact of writing Postgres logs with log_min_duration_statement = 0:

  1. Do select pg_stat_statements_reset(); and wait N seconds (where N >> 60 – say 1-24 hours, covering typical busy hours). Remember when it was, and write down somewhere – this timestamp will be needed!

  2. Check if select count(*) from pg_stat_statements is lower than pg_stat_statements.max. If it's equal to it, then raise pg_stat_statements.max and restart with the step 1.

  3. Get the estimate:

\set TS_PGSS_RESET 'XXXX-XX-XX XX:XX:XX';
@NikolayS
NikolayS / pg_terminate_old.sql
Last active June 21, 2018 02:02
Terminate old Postgres sessions
-- This function terminates all Postgres sessions which state have been changed "age" minutes ago.
-- Usage example:
-- select * from flush_connections(60);
--
-- Or just (but result will be less readable):
-- select flush_connections(60);
--
-- By default, terminates only sessions with "state = 'idle'".
-- If needed, you can terminate ALL sessions, regardless of their states:
-- select * from flush_connections(60, true);
@NikolayS
NikolayS / becnh_m5.2xlarge.out
Last active November 17, 2021 21:52
pgbench: simple INSERTs, UPDATEs with and without triggers
# m5.2xlarge 32.0 GiB 8 vCPUs
# s=100, n=10
*** Only SELECTs, -T 30 -j4 -c12
transaction type: <builtin: select only>
scaling factor: 100
query mode: prepared
number of clients: 12
number of threads: 4
duration: 30 s
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