Skip to content

Instantly share code, notes, and snippets.

@bubiche
bubiche / local_model_claude.md
Last active April 12, 2026 17:48
Local model with Claude Code

Local mode toggle

Save this to ~/.local/bin/claude-local-mode

#!/usr/bin/env bash
set -euo pipefail

SETTINGS="$HOME/.claude/settings.json"
@bubiche
bubiche / count.bash
Created October 1, 2024 21:45
Count write QPS for PostgreSQL
#!/bin/bash
DB_NAME="your_database_name"
INTERVAL=5 # seconds between samples
previous_count=0
while true; do
current_count=$(psql -d $DB_NAME -t -c "SELECT tup_inserted + tup_updated + tup_deleted FROM pg_stat_database WHERE datname = '$DB_NAME';")
@bubiche
bubiche / postgres_replication_lag_debug.sql
Created August 26, 2024 11:21
Debug psql replication lag
-- More info: https://www.postgresql.org/docs/current/warm-standby.html#STREAMING-REPLICATION-MONITORING
-- run on primary
SELECT application_name,state,sync_state,client_addr,client_hostname,
pg_wal_lsn_diff(pg_current_wal_lsn(),sent_lsn) AS sent_lag,
pg_wal_lsn_diff(sent_lsn,flush_lsn) AS receiving_lag,
pg_wal_lsn_diff(flush_lsn,replay_lsn) AS replay_lag,
pg_wal_lsn_diff(pg_current_wal_lsn(),replay_lsn) AS total_lag,
now()-reply_time AS reply_delay
FROM pg_stat_replication;
@bubiche
bubiche / google_cloud_sql_auth_proxy_vm_startup.sh
Last active August 30, 2024 17:51
Startup script for Google Cloud VM used as Cloud SQL Auth Proxy
#!/bin/bash
PROJECT_ID=...
# we might have to update this value from time to time
CLOUD_SQL_AUTH_PROXY_VERSION=2.13.0
# The arrays INSTANCES, REGIONS, PORTS should have the same length, value at index i is used for the i-th instance
# Put the list of cloud sql instance names here, e.g. master and the replicas
declare -a INSTANCES=("instance-name-1" "instance-name-2")
@bubiche
bubiche / update_psql_sequence.sql
Created July 12, 2024 04:53
Update postgresql sequence values to max of the column that uses them
-- logical replica in postgresql won't have their sequence values updates
-- this can be used to sync sequence values if needed
-- to actually run the command uncomment the "EXECUTE setval_sql;" line
DO $$
DECLARE
seq_record RECORD;
max_val BIGINT;
setval_sql TEXT;
BEGIN
@bubiche
bubiche / old_debian_pglogical_install.sh
Last active July 5, 2024 05:30
Install pglogical on old Debian/Ubuntu
# Add postgres repo
# https://wiki.postgresql.org/wiki/Apt
sudo apt install curl ca-certificates
sudo install -d /usr/share/postgresql-common/pgdg
sudo curl -o /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc --fail https://www.postgresql.org/media/keys/ACCC4CF8.asc
# Note: we have to use apt-archive since the repos for old distros like ubuntu 18 (bionic) are archived
sudo sh -c 'echo "deb [signed-by=/usr/share/postgresql-common/pgdg/apt.postgresql.org.asc] https://apt-archive.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
sudo apt update
@bubiche
bubiche / hash_schema_validator.rb
Created July 7, 2023 03:16
Ruby hash schema validator
module HashSchemaValidator
def self.valid?(object, schema)
schema.all? do |key, type|
if type.instance_of?(Hash)
valid?(object[key], type)
elsif type.instance_of?(Class)
object[key].instance_of?(type)
elsif type.instance_of?(Array)
type[0].instance_of?(Class) ? object[key].all? { |element| element.instance_of?(type[0]) } : type.include?(object[key])
else
# https://datatracker.ietf.org/doc/html/rfc6238
# Secret can only use characters in BASE32_CHARS
module TOTP
def self.totp(secret, interval = 30, time = nil)
time ||= Time.now
time_steps = time.to_i / interval
hmac = OpenSSL::HMAC.digest(OpenSSL::Digest.new("sha1"), self.base32_decode(secret), [time_steps].pack(">q").reverse)
@bubiche
bubiche / multiple_github_accounts.md
Created May 3, 2023 02:14
Use multiple github accounts
eval("