Skip to content

Instantly share code, notes, and snippets.

View PostgreSqlStan's full-sized avatar

PostgreSQL Stan PostgreSqlStan

View GitHub Profile
@PostgreSqlStan
PostgreSqlStan / bindlist.zsh
Last active April 5, 2024 18:33
bindlist (refactored)
# version 0.3 (see CHANGE NOTES comment below)
bindlist () {
local flag_all flag_help flag_list flag_raw
local usage=(
"Usage:"
" bindlist list bindings"
" bindlist -h --help print this message"
"Options:"
" -a --all include all bindings"
" -l --list single column output"
@PostgreSqlStan
PostgreSqlStan / bindlist.zsh
Last active September 18, 2023 02:51
zsh function: list key bindings with different display options
# NEW VERSION: see gist comment for link to refactored version
######################################################################################
# started as an alias, see https://postgresqlstan.github.io/zsh/binds/
# my 1st attempt to do anything semi-complicated with shell scripting
# don't know what i'm doing, just hacking pieces together until it works
bindlist () {
local flag_help flag_list flag_pretty
local usage=(
@PostgreSqlStan
PostgreSqlStan / install-pagila.md
Last active August 6, 2023 15:44
Install postgres pagila (DVD rental) database

Install pagila in local postgres db

These directions assume postgres is installed locally with a user—preferably not a superuser—with the same name as your OS username.

From the shell:

curl -OL https://github.com/devrimgunduz/pagila/archive/refs/tags/pagila-v3.1.0.tar.gz
tar -xvf pagila-pagila-v3.1.0.tar
cd pagila-pagila-v3.1.0
@PostgreSqlStan
PostgreSqlStan / issue_and_workarounds.sql
Last active April 21, 2024 00:43
issue and workaround examples
/*
TRYING TO UNDERSTAND THIS:
#1 - call twice nameit directly with expected results
#2 - call twice nameit from caller1 with unexpected results!
#3 - call nameit twice from caller2 which works
#4 - the issue can also be avoided by calling 2nd time in another session
*/
-- setup
@PostgreSqlStan
PostgreSqlStan / issue_example.sql
Created May 6, 2023 00:32
Why does the 2nd call show an OID instead of the name of the table?
drop schema if exists issue_repo cascade;
create schema issue_repo; set schema 'issue_repo';
create or replace procedure caller1 () as $proc$
begin call nameit('t'); end;
$proc$ language plpgsql;
create or replace procedure nameit (target regclass) as $proc$
begin raise notice '👻 target: %', target; end;
$proc$ language plpgsql;
@PostgreSqlStan
PostgreSqlStan / examples.sql
Last active April 28, 2023 02:31
create_text_table procedure explained with examples
-- see: https://postgresqlstan.github.io/postgresql/dynamic-sql/
create schema examples;
set schema 'examples';
CREATE PROCEDURE create_text_table (fields INT, tablename TEXT = '_import')
AS $$
/*
DESCRIPTION create text-only staging table
ARGUMENTS fields: number of fields (columns)
@PostgreSqlStan
PostgreSqlStan / procedure-create_text_table.sql
Last active April 27, 2023 13:12
PostgreSQL PL/pgSQL procedure: creates staging table of numbered, text columns (f1, f2, f3…)
CREATE PROCEDURE create_text_table (fields INT, tablename TEXT = '_import')
AS $$
/*
DESCRIPTION create text-only staging table
ARGUMENTS fields: number of fields (columns)
tablename: [optional, default='_import'] name of table
*/
DECLARE
fieldlist TEXT := '';
BEGIN
@PostgreSqlStan
PostgreSqlStan / .psqlrc
Created January 30, 2023 22:17
psql shortcut to save/restore original PROMPT1 setting
-- add to .psqlrc before changing PROMPT1
-- restores PROMPT1 to original setting
-- to use enter ":reset_prompt" (w/o quotes)
SELECT FORMAT($$\set PROMPT1 '%s'$$, :'PROMPT1') AS reset_prompt \gset
@PostgreSqlStan
PostgreSqlStan / less-cheatsheet.txt
Last active April 20, 2022 20:51
Summary of SUMMARY OF LESS COMMANDS
Summary of SUMMARY OF LESS COMMANDS
-<letter> Toggle a command line option [see OPTIONS below].
<number> Set N by entering number before command.
MOVING
--------------------- -----------------------------------------------------
e j ↓ <return> Forward one line (or N lines).
y k ↑ Backward one line (or N lines).
f <space> Forward one window (or N lines).
@PostgreSqlStan
PostgreSqlStan / ls_view.sql
Last active February 19, 2022 03:20
PostgreSQL view to list tables/views from all schemas
-- view: ls (tested on postgres 14.1)
CREATE OR REPLACE VIEW ls AS
SELECT
N.nspname AS "schema",
relname AS "relation",
CASE c.relkind
WHEN 'f' THEN 'fdw table'
WHEN 'r' THEN 'table'
WHEN 'v' THEN 'view'
WHEN 'm' THEN 'm view' END AS "type",