Skip to content

Instantly share code, notes, and snippets.

View schwartzmx's full-sized avatar
👓

Phil Schwartz schwartzmx

👓
View GitHub Profile
@schwartzmx
schwartzmx / dict_get_first.py
Created August 25, 2021 22:41
Dumb, probably unnecessary, helper for looking up a value from a dict for known permutations of a key, or fallback keys. Rather than `dict.get('Key', dict.get('KEY'))` use `get_first(dict, 'Key', 'KEY')` - save yourself 3 keystrokes in the case of 2 keys, more as the # of keys grow! :wow: /s
from typing import Any, Dict, Optional
def get_first(d: Dict[Any, Any], *keys: Any) -> Optional[Any]:
"""Return the first value from d that is not None utilizing the *keys as key lookups.
If no value can be found for any key, returns None.
Example:
>>> d = {
'tesT': 1,
'TeST': 2,
SELECT c.relname AS "Name",
CASE c.relkind WHEN 'r' THEN 'table' WHEN 'm' THEN 'materialized view' ELSE 'view' END AS "Engine",
pg_relation_size(c.oid) AS "Data_length",
pg_total_relation_size(c.oid) - pg_relation_size(c.oid) AS "Index_length",
obj_description(c.oid, 'pg_class') AS "Comment",
CASE WHEN c.relhasoids THEN 'oid' ELSE '' END AS "Oid",
c.reltuples as "Rows",
n.nspname
FROM pg_class c
JOIN pg_namespace n ON(n.nspname = current_schema() AND n.oid = c.relnamespace)
@schwartzmx
schwartzmx / threadpool_wrapper.py
Last active April 8, 2020 15:18
Wrapper around concurrent.futures.ThreadPoolExecutor
import logging
from concurrent.futures import (
ThreadPoolExecutor,
wait,
CancelledError,
TimeoutError,
ALL_COMPLETED,
)
import os
1..100 | % {
switch ($_) {
{ $_ % 3 -eq 0 -and $_ % 5 -eq 0 } { "fizzbuzz" }
{ $_ % 3 -eq 0 } { "fizz" }
{ $_ % 5 -eq 0 } { "buzz" }
default { $_ }
}
}
@schwartzmx
schwartzmx / pg_txn_wraparound.sql
Created April 12, 2018 15:56
check for txn wraparound (when wraparound vacuum freeze needs to occur)
SELECT
oid::regclass::text AS table,
age(relfrozenxid) AS xid_age,
mxid_age(relminmxid) AS mxid_age,
least(
(SELECT setting::int
FROM pg_settings
WHERE name = 'autovacuum_freeze_max_age') - age(relfrozenxid),
(SELECT setting::int
FROM pg_settings
@schwartzmx
schwartzmx / pg_disk_cache_hits.sql
Created April 12, 2018 15:55
disk & cache hits by table
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)
@schwartzmx
schwartzmx / pg_autovacuum_next_trigger.sql
Created April 12, 2018 14:30
check when autovacuum will trigger based on table > db autovacuum settings
-- check when autovacuum will trigger based on table > db autovacuum settings
WITH rel_set AS
(
SELECT
oid,
CASE split_part(split_part(array_to_string(reloptions, ','), 'autovacuum_vacuum_threshold=', 2), ',', 1)
WHEN '' THEN NULL
ELSE split_part(split_part(array_to_string(reloptions, ','), 'autovacuum_vacuum_threshold=', 2), ',', 1)::BIGINT
END AS rel_av_vac_threshold,
CASE split_part(split_part(array_to_string(reloptions, ','), 'autovacuum_vacuum_scale_factor=', 2), ',', 1)
@schwartzmx
schwartzmx / redshift_long_running_queries.sql
Created May 17, 2017 03:52
find long running queries from the stl_query log
select u.usename,
q.userid,
q.query,
q.pid,
q.database,
q.querytxt,
listagg(q2.text, ' ') within group (order by q2.sequence) as querytxt2,
q.starttime,
q.endtime,
aborted,
@schwartzmx
schwartzmx / Invoke-ParamQuery.ps1
Created March 8, 2017 20:18
Invoke a parameterized query through PowerShell using System.Data and a HashTable of parameters, returns System.Data.DataTable
Function Invoke-ParamQuery {
param(
[String]$Query,
$Parameters=@{},
[Data.SqlClient.SqlConnection]$Conn,
[int]$Timeout=3,
[switch]$CloseConn
)
if ($conn.State -eq "Closed") {
$conn.Open()
@schwartzmx
schwartzmx / WindowSwapper.ps1
Created January 13, 2017 06:39
Swap process windows repeatedly and bring process windows to foreground.
$code = @"
[DllImport("user32.dll", SetLastError=true)]
public static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetForegroundWindow(IntPtr hWnd);
"@
$win = Add-Type -MemberDefinition $code -Name SwitchWindow -Namespace SwitchWindow -PassThru