Skip to content

Instantly share code, notes, and snippets.

View CHERTS's full-sized avatar

Mikhail Grigorev CHERTS

View GitHub Profile
@CHERTS
CHERTS / pg_save_load_via_copy_and_stdout_stdin.sh
Created April 24, 2025 14:57
[PostgreSQL] Save and load data via COPY and STDOUT/STDIN
# Prepare test data (table size: 1M rows, ~87MB)
psql -t -X postgres://user:[email protected]:5432/dbname -c "
CREATE TABLE customers (
id BIGINT GENERATED ALWAYS AS IDENTITY NOT NULL PRIMARY KEY,
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL,
age INTEGER NOT NULL
);
INSERT INTO customers(first_name,last_name,age) VALUES(CONCAT('FirstName',generate_series(1, 1000000)), CONCAT('LastName',round((random()*100)::integer,0)), round((random()*100)::integer,0));
"
@CHERTS
CHERTS / autovacuum_forecast.sql
Created March 6, 2025 09:41
PostgreSQL autovacuum forecast
WITH rel_set AS (
SELECT
oid
, CASE split_part(
split_part(array_to_string(reloptions, ','), 'autovacuum_vacuum_threshold=', 2)
, ','
, 1
)
WHEN '' THEN NULL
ELSE
@CHERTS
CHERTS / snapd_cleanup_unused.sh
Created January 14, 2025 13:57
Remove all unused version of snap packages
#!/bin/sh
LANG=en_US.UTF-8 snap list --all | awk '/disabled/{print $1, $3}' |
while read pkg revision; do
sudo snap remove "$pkg" --revision="$revision"
done
journalctl --vacuum-size=100M
@CHERTS
CHERTS / pg_get_cluster_role.sql
Last active November 8, 2024 10:19
Determine whether the running PostgreSQL cluster is the primary one or not.
SELECT DISTINCT
CASE
WHEN b.sender=0 AND c.receiver=0 THEN
'standalone'
WHEN b.sender>0 AND c.receiver=0 THEN
'primary'
WHEN b.sender=0 AND c.receiver>0 THEN
'replica'
WHEN b.sender>0 AND c.receiver>0 THEN
'primary+replica'
@CHERTS
CHERTS / install_latest_pgscv.sh
Created September 27, 2024 11:07
Install latest pgSCV
wget -qO - $(OS=$(uname -s | tr '[:upper:]' '[:lower:]'); ARCH=$(uname -p | sed 's/x86_64/amd64/g'); wget https://api.github.com/repos/CHERTS/pgscv/releases/latest -qO - | grep -wo "https.*${OS}_${ARCH}.tar.g
z") | tar xzf - -C /tmp && \
mv /tmp/pgscv.yaml /etc 2>/dev/null && \
mv /tmp/pgscv.service /etc/systemd/system 2>/dev/null && \
mv /tmp/pgscv.default /etc/default/pgscv 2>/dev/null && \
mv /tmp/pgscv /usr/sbin 2>/dev/null && \
chown postgres:postgres /etc/pgscv.yaml 2>/dev/null && \
systemctl daemon-reload && \
systemctl enable pgscv --now
@CHERTS
CHERTS / docker-compose.yml
Last active February 20, 2025 13:00
Redpanda + Redpanda console docker-compose file (one node host)
services:
redpanda:
image: docker.redpanda.com/redpandadata/redpanda:v24.2.5
container_name: redpanda
command:
- redpanda
- start
- --kafka-addr internal://0.0.0.0:9092,external://0.0.0.0:19092
# Address the broker advertises to clients that connect to the Kafka API.
# Use the internal addresses to connect to the Redpanda brokers'
@CHERTS
CHERTS / pg_change_owner.sql
Last active November 25, 2024 08:59
How to change owner of PostgreSQL database/schema/table/sequence/views/functions/types?
CREATE OR REPLACE PROCEDURE change_owner_exec(sql_exec text) AS
$$
BEGIN
RAISE INFO 'Exec: %', sql_exec;
EXECUTE sql_exec;
COMMIT;
RETURN;
END;
$$
LANGUAGE plpgsql;
@CHERTS
CHERTS / psaux.bash
Created July 29, 2024 13:32
ps aux written entirely in bash without ever forking
#!/bin/bash
# ps aux written entirely in bash without ever forking
# Author: Isabella Bosia
# Github: https://github.com/izabera/ps/tree/develop
#
# so initially i was hoping you could get everything from /proc/<pid>/status
# because it's easy to parse (in most cases) but apparently you can't get
# things like the cpu% :(
@CHERTS
CHERTS / create_publication_and_slot.sql
Created July 25, 2024 13:47
Create logical replication and test
-- Create publication
CREATE PUBLICATION cdc;
-- Create slot
SELECT pg_create_logical_replication_slot('test_slot_v1', 'pgoutput');
-- Create example table
CREATE TABLE replication_test_v1
(
id integer NOT NULL PRIMARY KEY,
@CHERTS
CHERTS / debezium_checking_capture_data.sql
Created July 25, 2024 13:29
Checking capturing data change use logical slot
-- Create slot
SELECT pg_create_logical_replication_slot('test_slot_v1', 'pgoutput');
-- Show publication name
SELECT pubname FROM pg_publication_tables GROUP BY pubname;
-- Peak changes (does not consume changes)
SELECT pg_logical_slot_peek_binary_changes('test_slot_v1', NULL, NULL, 'publication_names', 'dbz_publication', 'proto_version', '1');
-- Get changes (consumes changes)