Skip to content

Instantly share code, notes, and snippets.

@kidd
kidd / util.clj
Last active September 6, 2023 17:04
(defn apply-if
"if(p(m)) then f(m, ...args) else m
Useful for `maybe-*` like updates."
[m p f & args]
(if (p m)
(apply f m args)
m))
(defn fork [l c r]
@kidd
kidd / postgres_fdw.sql
Created August 16, 2023 09:42
Demo of postgres_fdw to "tunnel" access to a remote table;
create extension postgres_fdw ;
create server dev foreign data wrapper postgres_fdw options (host 'localhost', dbname 'db_dev');
select user;
create user mapping for my_user server dev options (user 'my_user' );
create schema dev;
import foreign schema public limit to ( table_to_copy ) from server dev into dev;
select * from dev.table_to_copy;
@kidd
kidd / duckdb.sql
Last active January 18, 2025 13:00
arg_max example in duckdb
D create table test as SELECT i as i ,random() as r, i%4 as gr from generate_series(1,100) t(i);
D select * from test;
┌───────┬──────────────────────┬───────┐
│ i │ r │ gr │
│ int64 │ double │ int64 │
├───────┼──────────────────────┼───────┤
│ 1 │ 0.14984029697949075 │ 1 │
│ 2 │ 0.20274345139105418 │ 2 │
│ 3 │ 0.07478489612107744 │ 3 │
@kidd
kidd / foreign_keys.sql
Created October 5, 2022 11:02
sql to find foreign keys of a table
SELECT
r.table_name
FROM information_schema.constraint_column_usage u
INNER JOIN information_schema.referential_constraints fk
ON u.constraint_catalog = fk.unique_constraint_catalog
AND u.constraint_schema = fk.unique_constraint_schema
AND u.constraint_name = fk.unique_constraint_name
INNER JOIN information_schema.key_column_usage r
ON r.constraint_catalog = fk.constraint_catalog
AND r.constraint_schema = fk.constraint_schema
# deps jq curl 'http httpie'
deps() {
for dep in "$@"; do
read cmd package <<<"$dep"
package=${package:-$cmd}
mute which "$cmd" ||
apt install -y $package ||
exit 1
done
}
#!/bin/bash
pg_conf_file=/var/lib/postgresql/data/postgresql.conf
echo "\
log_statement = 'all'
log_disconnections = off
log_duration = on
log_min_duration_statement = -1
shared_preload_libraries = 'pg_stat_statements'
def testing(fname):
print(fname)
cmds = { "testing": testing }
if __name__ == "__main__":
cmds[sys.argv[1]](*sys.argv[2:])
ropsql() {
PSQLRC=<(
cat ~/.psqlrc &&
echo 'set session characteristics as transaction read only;') \
psql "$@"
}
mb:ropsql() {
`aws secretsmanager get-secret-value --secret-id "${1? secret arn needed }" | jq '.SecretString | fromjson | "ropsql -d \(.mb_db_connection_uri | sub("&loginTimeout.*"; ""))"' -r`
@kidd
kidd / etl.py
Last active April 27, 2022 10:13
def testing(foo, fname, **kwargs):
"""This is functions does this and that.
The longer explanation is here
"""
print(foo, fname, kwargs)
def parse_args(a=[]):
r, extra = [], []
for i in a:
@kidd
kidd / etl.py
Last active April 27, 2022 10:12
def testing(foo, fname, **kwargs):
print(foo, fname, kwargs)
def parse_args(a=[]):
r, extra = [], []
for i in a:
if re.match("^--[^-].*=.+", i):
r.append(re.split("=", i[2:], 2))
else:
extra.append(i)