Skip to content

Instantly share code, notes, and snippets.

View PostgreSqlStan's full-sized avatar

PostgreSQL Stan PostgreSqlStan

View GitHub Profile
@PostgreSqlStan
PostgreSqlStan / install_openssl.zsh
Last active September 19, 2021 13:47
Install openssl from source on MacOS
# tested on macOS 11.5.2 (Big Sur)
curl -OL https://www.openssl.org/source/openssl-3.0.0.tar.gz
tar -xvf openssl-3.0.0.tar.gz
cd openssl-3.0.0
./Configure # note the capital “C” - not an accident
make
make test
sudo make install
@PostgreSqlStan
PostgreSqlStan / install_xz.zsh
Last active September 19, 2021 13:47
Install xz from source on MacOS
# tested on macOS 11.5.2 (Big Sur)
curl -OL https://tukaani.org/xz/xz-5.2.5.tar.gz
tar -xvf xz-5.2.5.tar.gz
cd xz-5.2.5/
./configure
make
make test # optional
sudo make install
@PostgreSqlStan
PostgreSqlStan / install_python3.zsh
Last active December 9, 2021 21:16
Install python 3 from source on MacOS
# tested on macOS 11.5.2 (Big Sur) after installing openssl and xz
curl -OL https://www.python.org/ftp/python/3.9.7/Python-3.9.7.tgz
tar -xvf Python-3.9.7.tgz
cd Python-3.9.7/
./configure
make
make test # optional
sudo make install
@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",
@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 / .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 / 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 / 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 / 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 / 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