Skip to content

Instantly share code, notes, and snippets.

View kmoppel's full-sized avatar

Kaarel Moppel kmoppel

View GitHub Profile
@kmoppel
kmoppel / sync_repl_guard.py
Created July 9, 2025 08:17
Disables sync repl in case no streaming replicas for 1m
#!/usr/bin/env python3
import time
import psycopg
from psycopg.rows import dict_row
from datetime import datetime
CONNSTR = "dbname=postgres user=postgres port=5434"
@kmoppel
kmoppel / psycopg_latency_test.py
Last active August 5, 2025 07:22
Test multi-statement TX behaviour / latency of psycopg driver
import psycopg
import random
import time
CONNSTR = "postgresql://james:[email protected]:5432/postgres?sslmode=require"
SCALE = 10
BASE_SQL = """
BEGIN;
SELECT abalance FROM pgbench_accounts WHERE aid = {aid};
SELECT abalance FROM pgbench_accounts WHERE aid = {aid2};
@kmoppel
kmoppel / pg_repack_in_pure_sql.sh
Created August 5, 2025 23:57
Emulate pg_repack in pure SQL + triggers
#!/bin/bash
set -euo pipefail
SCALE=1200
FF=80
UNLOGGED="unlogged" # set to "" for normal tables
UNLOGGED=""
ROW_CHANGES=100000 # 0.1% of scale 1000
@kmoppel
kmoppel / check_backrest_last_backup_freshness.sh
Created September 2, 2025 11:00
A Cron script to report errors when nightly pgbackup backups have failed
#!/bin/bash
# PS Running in CRON mode - meaning output produced only on errors
# Expected inputs:
# 1) the pgbackrest config file path, e.g. /etc/pgbackrest/15/main/pgbackrest.conf
# 2) the stanza name
# 3) max age threshold in seconds of last valid backup.
# errors if no valid backup within the threshold