Skip to content

Instantly share code, notes, and snippets.

View schwartzmx's full-sized avatar
👓

Phil Schwartz schwartzmx

👓
View GitHub Profile
@schwartzmx
schwartzmx / redshift_revoke_drop_user.sql
Last active May 26, 2023 17:14
Various queries for finding and generating statements to revoke all privileges and drop a user from AWS Redshift.
-- check if they own any databases
select d.datname as name,
pg_catalog.pg_get_userbyid(d.datdba) as db_owner,
'alter database '+d.datname+' owner to <user>;' as chg_owner
from pg_catalog.pg_database d
where pg_catalog.pg_get_userbyid(d.datdba) = '<user>'
order by d.datname;
-- check if they own any schemas
select nspname, usename, 'alter schema '+nspname+' owner to <user>;' as chg_owner
@schwartzmx
schwartzmx / BlockUserInput.ps1
Created January 13, 2017 06:07
Block user input. Mouse, and keyboard are disabled for the duration.
$code = @"
[DllImport("user32.dll")]
public static extern bool BlockInput(bool fBlockIt);
"@
$userInput = Add-Type -MemberDefinition $code -Name UserInput -Namespace UserInput -PassThru
Function Disable-UserInput {
param([int]$Seconds=5)
$userInput::BlockInput($true) | out-null
@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
@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 / 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 / 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 / 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_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
1..100 | % {
switch ($_) {
{ $_ % 3 -eq 0 -and $_ % 5 -eq 0 } { "fizzbuzz" }
{ $_ % 3 -eq 0 } { "fizz" }
{ $_ % 5 -eq 0 } { "buzz" }
default { $_ }
}
}
@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