Skip to content

Instantly share code, notes, and snippets.

@slabad
slabad / sp_whoisactive.sql
Created May 22, 2020 17:29
sp_whoisactive #tsql #mssql
EXEC sp_WhoIsActive
@filter = '',
@filter_type = 'session',
@not_filter = '',
@not_filter_type = 'session',
@show_own_spid = 0,
@show_system_spids = 0,
@show_sleeping_spids = 1,
@get_full_inner_text = 0,
@get_plans = 0,
@slabad
slabad / dynamic_table_create.sql
Last active June 12, 2020 19:00
Dynamic Table Creation #psql #postgres
DO
$$
BEGIN
EXECUTE format('CREATE TABLE %I AS SELECT * FROM backup', 'backup_' || to_char(CURRENT_DATE,'yyyy-mm-dd'));
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION fn_hello(v_table text)
returns void
@slabad
slabad / fdw_setup.sql
Last active July 1, 2020 14:48
Cross Database Querying on Postgres #psql #postgres
--for a cross db query on the same host
--no hostname or password is needed.
--the following script adds the extension,
-- creates a server, maps the postgres user,
-- and imports the foreign db into a new schema
BEGIN;
CREATE EXTENSION IF NOT EXISTS postgres_fdw;
DROP SERVER IF EXISTS fdw_server CASCADE;
@slabad
slabad / sbarns.sql
Last active April 20, 2021 14:31
sql101_hw_barns #sql101
--1. Create Tables:
Create table Genres (
Id int primary key
, genre varchar(50))
-- I would recommend making all your ids IDENTITY columns so those ids are generated automatically. I'm making the comment here but this would be true across the board.
Create table Movies (
id int primary key
@slabad
slabad / get_pg_version_number.sh
Created June 7, 2021 16:38
Get pg Version number #postgres @bash
pg_config --version|grep -oP "(?<=PostgreSQL ).*"
@slabad
slabad / mmcelheny_alias
Created September 20, 2021 13:29
mmcelheny alias example #bash #zsh
myaliases = {
# 'grep': 'grep --color=auto --exclude-dir={.bzr,CVS,.git,.hg,.svn}',
# one char shortcuts
'h': 'history show all | less',
'c': 'clear',
# ls shortcuts
'l': 'ls -F',
'la': 'ls -lAfh',
'll': 'ls -lFh',
'ldot': 'ls -ld .*',
@slabad
slabad / gin_index_example.sql
Created September 21, 2021 12:23
gin index example #postgres #psql #index
CREATE INDEX index_notifications_gin_subscriber ON notifications USING gin ((params ->> 'subscriber') gin_trgm_ops) WHERE params -> 'subscriber':: text IS NOT NULL;
@slabad
slabad / pg_index_usage_table.sql
Created September 21, 2021 13:27
Postgres Index Usage #postgres #index
SELECT
c.relname,
indexname,
c.reltuples AS num_rows,
pg_size_pretty(pg_relation_size(quote_ident(c.relname)::text)) AS table_size,
pg_size_pretty(pg_relation_size(quote_ident(indexrelname)::text)) AS index_size,
CASE WHEN indisunique THEN 'Y'
ELSE 'N'
END AS UNIQUE,
idx_scan AS number_of_scans,
@slabad
slabad / kmodiselle_test.sql
Last active September 23, 2021 14:56
kmodiselle_test
1. Create tables to hold data for your movie collection.
Create table Movies (id int identity primary key,
Title varchar (100),
Director varchar (100),
Runtime smallint,
Genre_id int,
Release_Year year);
Create table Genre (id int identity primary key,
@slabad
slabad / bash_cheat_sheet.sh
Created September 30, 2021 15:05
Bash Cheat Sheet #bash
#To display the largest folders/files including the sub-directories, run:
du -Sh | sort -rh | head -5
#du command: Estimate file space usage.
#-h : Print sizes in human-readable format (e.g., 10MB).
#-S : Do not include the size of subdirectories.
#-s : Display only a total for each argument.
#sort command : sort lines of text files.
#-r : Reverse the result of comparisons.