This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
from concurrent.futures import ( | |
ThreadPoolExecutor, | |
wait, | |
CancelledError, | |
TimeoutError, | |
ALL_COMPLETED, | |
) | |
import os |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1..100 | % { | |
switch ($_) { | |
{ $_ % 3 -eq 0 -and $_ % 5 -eq 0 } { "fizzbuzz" } | |
{ $_ % 3 -eq 0 } { "fizz" } | |
{ $_ % 5 -eq 0 } { "buzz" } | |
default { $_ } | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function Invoke-ParamQuery { | |
param( | |
[String]$Query, | |
$Parameters=@{}, | |
[Data.SqlClient.SqlConnection]$Conn, | |
[int]$Timeout=3, | |
[switch]$CloseConn | |
) | |
if ($conn.State -eq "Closed") { | |
$conn.Open() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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 |
NewerOlder