Skip to content

Instantly share code, notes, and snippets.

View Charmatzis's full-sized avatar
🎯
Focusing

Christos Charmatzis

🎯
Focusing
View GitHub Profile
@Charmatzis
Charmatzis / fix.primary.keys.sql
Created April 2, 2024 06:48
Fix PKs seqs in public schema in Postgres
CREATE OR REPLACE FUNCTION primary_id_sequences_from_all_tables()
RETURNS TABLE (table_name TEXT, column_name TEXT, data_type TEXT, max BIGINT, next BIGINT) AS $$
DECLARE
rec RECORD;
BEGIN
FOR rec IN
SELECT tc.table_name, kcu.column_name, c.data_type
FROM information_schema.table_constraints AS tc
JOIN information_schema.key_column_usage AS kcu
ON tc.constraint_name = kcu.constraint_name
select
name,
setting
from
pg_settings
where
name in ('wal_level', 'track_commit_timestamp', 'max_worker_processes', 'max_replication_slots', 'max_wal_senders ', 'shared_preload_libraries');
SELECT name,
setting,
@Charmatzis
Charmatzis / get_index_size.sql
Created November 14, 2023 06:45
Get index size (Postgres)
SELECT i.relname "Table Name",indexrelname "Index Name",
pg_size_pretty(pg_total_relation_size(relid)) As "Total Size",
pg_size_pretty(pg_indexes_size(relid)) as "Total Size of all Indexes",
pg_size_pretty(pg_relation_size(relid)) as "Table Size",
pg_size_pretty(pg_relation_size(indexrelid)) "Index Size",
reltuples::bigint "Estimated table row count"
FROM pg_stat_all_indexes i JOIN pg_class c ON i.relid=c.oid
WHERE i.relname='table_name';
@Charmatzis
Charmatzis / caclulate_tiles.sql
Created November 10, 2023 13:30
Get Tiles ZXY from a specific extent using PostGIS
-- Taken from https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#PostgreSQL
CREATE OR REPLACE FUNCTION lon2tile(lon DOUBLE PRECISION, zoom INTEGER)
RETURNS INTEGER AS
$BODY$
SELECT FLOOR( (lon + 180) / 360 * (1 << zoom) )::INTEGER;
$BODY$
LANGUAGE SQL IMMUTABLE;
CREATE OR REPLACE FUNCTION lat2tile(lat double precision, zoom integer)
RETURNS integer AS
@Charmatzis
Charmatzis / 1_shell.omp.json
Created May 19, 2023 09:09
oh-my-posh terminal theme
{
"$schema": "https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json",
"console_title_template": "{{.Folder}}{{if .Root}} :: root{{end}} :: {{.Shell}}",
"blocks": [
{
"alignment": "left",
"newline": true,
"segments": [
{
"foreground": "#ffbebc",
@Charmatzis
Charmatzis / count.sql
Last active February 14, 2023 07:17
Count for null and not null columns
select count(a.id), sum(case when b.id is null then 0 else 1 end) as b_exists,
sum(case when b.id is null then 1 else 0 end) as b_does_not_exist
from a left join b on a.b_id = b.id;
-- Or even better
select count(a.id),
count(b.id) filter b.id is not null as b_exists,
count(b.id) filter b.id is null as b_does_not_exist
from a left join b on a.b_id = b.id;
@Charmatzis
Charmatzis / Windows Defender Exclusions for Developer.ps1
Created June 11, 2022 13:36 — forked from nerzhulart/Windows Defender Exclusions for Developer.ps1
Adds Windows Defender exclusions for developers (Visual Studio, JetBrains Rider, IntellIJ Idea, Git, MsBuild, dotnet, mono etc.)
$userPath = $env:USERPROFILE
$pathExclusions = New-Object System.Collections.ArrayList
$processExclusions = New-Object System.Collections.ArrayList
$pathExclusions.Add('C:\Windows\Microsoft.NET') > $null
$pathExclusions.Add('C:\Windows\assembly') > $null
$pathExclusions.Add($userPath + '\AppData\Local\Microsoft\VisualStudio') > $null
$pathExclusions.Add('C:\ProgramData\Microsoft\VisualStudio\Packages') > $null
$pathExclusions.Add('C:\Program Files (x86)\MSBuild') > $null
$pathExclusions.Add('C:\Program Files (x86)\Microsoft Visual Studio 14.0') > $null
@Charmatzis
Charmatzis / monitoring.sql
Last active November 14, 2022 14:50
Monitoring PostgreSQL Disk I/O Performance
-- perform a "select pg_stat_reset();" when you want to reset counter statistics
with
all_tables as
(
SELECT *
FROM (
SELECT 'all'::text as table_name,
sum( (coalesce(heap_blks_read,0) + coalesce(idx_blks_read,0) + coalesce(toast_blks_read,0) + coalesce(tidx_blks_read,0)) ) as from_disk,
sum( (coalesce(heap_blks_hit,0) + coalesce(idx_blks_hit,0) + coalesce(toast_blks_hit,0) + coalesce(tidx_blks_hit,0)) ) as from_cache
FROM pg_statio_all_tables --> change to pg_statio_USER_tables if you want to check only user tables (excluding postgres's own tables)
@Charmatzis
Charmatzis / get_the_most_expensive_queries_postgres.sql
Last active February 10, 2022 11:13
Postgres - Most expensive queries
select round(( 100 * total_time / sum(total_time) over ())::numeric, 2) percent,
round(total_time::numeric, 2) as total,
calls,
round(mean_time::numeric, 2) as mean,
stddev_time,
query
from pg_stat_statements
order by total_time DESC
limit 10;
@Charmatzis
Charmatzis / psql_concat_uuid_with_single_quotes.sql
Last active February 10, 2022 11:11
concat UUID with single quotes in postgreSQL
SELECT concat(concat('"', string_agg(uuid, '","')) , '"')
FROM (
SELECT uuid::text
FROM public.my_table
) a;