Skip to content

Instantly share code, notes, and snippets.

View CHERTS's full-sized avatar

Mikhail Grigorev CHERTS

View GitHub Profile
@CHERTS
CHERTS / pg_run_analyze_all_table.sql
Created April 2, 2024 08:38
PostgreSQL run analyze all table (using pl/pgsql script)
DO $$
DECLARE
tab RECORD;
schemaName VARCHAR := 'public';
BEGIN
for tab in (SELECT t.relname::varchar AS table_name
FROM pg_class t
JOIN pg_namespace n ON n.oid = t.relnamespace
WHERE t.relkind = 'r' and n.nspname::varchar = schemaName
ORDER BY 1)
@CHERTS
CHERTS / pg_create_database_if_not_exists
Created March 27, 2024 13:50
CREATE DATABASE NOT EXISTS in PostgreSQL (workaround from within psql)
# native using psql
SELECT 'CREATE DATABASE mydb' WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'mydb')\gexec
# native using shell + psql
echo "SELECT 'CREATE DATABASE mydb' WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'mydb')\gexec" | psql
@CHERTS
CHERTS / mysql_get_table_count.sql
Created March 27, 2024 13:26
Getting the number of records in all tables in MySQL (use count)
SET GROUP_CONCAT_MAX_LEN=131072;
SET @selects = NULL;
SELECT CONCAT('SELECT * FROM (\n', GROUP_CONCAT(single_select SEPARATOR ' UNION\n'), '\n ) Q ORDER BY Q.TABLE_ROWS DESC') AS sql_query
FROM (
SELECT CONCAT(
'SELECT "',
table_name,
'" AS TABLE_NAME, COUNT(1) AS TABLE_ROWS
FROM `',
table_schema,
@CHERTS
CHERTS / pg_get_table_count.sql
Created March 27, 2024 11:52
Getting the number of records in all tables in PostgreSQL (use count)
WITH tbl AS (
SELECT table_schema, table_name
FROM information_schema.tables
WHERE table_name NOT LIKE 'pg_%' AND table_schema NOT IN ('information_schema')
)
SELECT table_schema, table_name,
(xpath('/row/c/text()', query_to_xml(format('SELECT count(1) AS c FROM "%I"."%I"', table_schema, table_name), FALSE, TRUE, '')))[1]::text::bigint AS table_count
FROM tbl;
@CHERTS
CHERTS / gitignore_global
Last active March 14, 2024 11:30
Global Git excludes file
git config --global core.excludesfile ~/.gitignore_global
cat <<EOF > ~/.gitignore_global
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db
EOF
@CHERTS
CHERTS / .vimrc
Created January 26, 2024 06:54
My .vimrc file
vnoremap :w !xsel -b
" Make backspace delete lots of things
" set backspace=indent,eol,start
" Enable syntax highlighting
syntax on
" Enable line highlight
set cursorline
" Try to show at least three lines and two columns of context when
" scrolling
set scrolloff=3
@CHERTS
CHERTS / .bashrc
Created January 26, 2024 06:53
My .bachrc file
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
# Default editor
export EDITOR=vim
# Enable programmable completion features (you don't need to enable
@CHERTS
CHERTS / uuidv7_native.py
Last active September 15, 2023 13:48
Native UUIDv7 on Python 3
import random
import time
import uuid
import datetime
# UUID v7 format:
# 0 1 2 3
# 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
# | unixts |
@CHERTS
CHERTS / change_group_id.sh
Last active October 2, 2023 11:07
Changing GID (gid>1000) to system (gid<1000)
#!/bin/bash
#
# Program: Changing GID (gid>1000) to system (gid<1000) <change_group_id.sh>
#
# Author: Mikhail Grigorev <sleuthhound at gmail dot com>
#
# Current Version: 1.0
#
# Revision History:
@CHERTS
CHERTS / nginx.conf
Created July 29, 2023 13:39
Nginx best config (base config) for Ubuntu
user nginx;
worker_processes auto;
worker_cpu_affinity auto;
worker_rlimit_nofile 10000;
worker_shutdown_timeout 30;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
# Для Linux 2.6+ - epool, Для FreeBSD - kqueue