From 151ff0f0f8fa6ec6a98c8f6a2d8b6feae9f43efb Mon Sep 17 00:00:00 2001 From: Michele Cereda Date: Thu, 3 Oct 2024 09:00:41 +0200 Subject: [PATCH] chore(postgres): dump acquired knowledge --- knowledge base/acronyms and abbreviations.md | 5 +- .../{postgresql.sh => postgres/commands.sh} | 0 snippets/postgres/functions.sql | 54 +++++++++++++++++++ .../{postgresql.sql => postgres/primer.sql} | 34 ++++++++++-- snippets/systemd.sh | 2 + 5 files changed, 91 insertions(+), 4 deletions(-) rename snippets/{postgresql.sh => postgres/commands.sh} (100%) create mode 100644 snippets/postgres/functions.sql rename snippets/{postgresql.sql => postgres/primer.sql} (86%) diff --git a/knowledge base/acronyms and abbreviations.md b/knowledge base/acronyms and abbreviations.md index a9ec493..9bdd5b5 100644 --- a/knowledge base/acronyms and abbreviations.md +++ b/knowledge base/acronyms and abbreviations.md @@ -38,7 +38,8 @@ | CSS | Cascading Style Sheets | | | CVE | Common Vulnerabilities and Exposures | | | CVS | Concurrent Versioning System | | -| DBMS | Data Base Management System | | +| DB | Data Base | | +| DBMS | DB Management System | | | DDL | Data Definition Language | | | DKIM | DomainKeys Identified Mail | | | DML | Data Manipulation Language | | @@ -79,6 +80,7 @@ | LED | Light Emitting Diode | | | LIFO | Last In First Out | | | M2COTS | Mass Market COTS | Widely available COTS products | +| MR | Merge Request | Prevalently used in GitLab | | NACL | Network ACL | | | OAM | [Open Application Model] | | | OAM | Operations, Administration and Management | | @@ -89,6 +91,7 @@ | PEBCAK | Problem Exists Between Chair And Keyboard | | | PGP | Pretty Good Privacy | | | PII | Personally Identifiable Information | | +| PR | Pull Request | Prevalently used in GitHub | | RBAC | Role-Based Access Control | | | ROM | Read-Only Memory | | | RPM | Revolutions Per Minute | | diff --git a/snippets/postgresql.sh b/snippets/postgres/commands.sh similarity index 100% rename from snippets/postgresql.sh rename to snippets/postgres/commands.sh diff --git a/snippets/postgres/functions.sql b/snippets/postgres/functions.sql new file mode 100644 index 0000000..064846a --- /dev/null +++ b/snippets/postgres/functions.sql @@ -0,0 +1,54 @@ +/** + * Shuffle values in a column + * Returns the number of affected rows + * + * Only works on that column, with no regards for data connections. + * Requires a way to identify the row in the source table during the update. + * └── Using 'ctid' over a table's primary key to avoid the need of specifying another column. + * ctids shall stay the same, but values shall be numbered randomly (hence the shuffling part). + * + * Credits to . + * + * SELECT user_id, name FROM band_members WHERE name IN ('mark', 'tom', 'travis') ORDER BY name; + * SELECT shuffle_column('band_members','name'); + * SELECT user_id, name FROM band_members WHERE name IN ('mark', 'tom', 'travis') ORDER BY name; + **/ + +CREATE OR REPLACE FUNCTION shuffle_column( + table_name TEXT, + column_name TEXT +) +RETURNS INTEGER +LANGUAGE plpgsql +AS $$ + DECLARE affected_rows INTEGER; + BEGIN + EXECUTE format(' + WITH + ctids AS ( + SELECT + ROW_NUMBER() OVER (), + ctid + FROM %I + ), + shuffled_values AS ( + SELECT + ROW_NUMBER() OVER (ORDER BY RANDOM()), + %I AS new_value + FROM %I + ) + UPDATE %I + SET %I = shuffled_values.new_value + FROM shuffled_values JOIN ctids ON shuffled_values.row_number = ctids.row_number + WHERE %I.ctid = ctids.ctid + ', table_name, column_name, table_name, table_name, column_name, table_name); + GET DIAGNOSTICS affected_rows = ROW_COUNT; + RETURN affected_rows; + END; +$$; + + +/** + * Replace UUID in a column + * Returns the number of affected rows + **/ diff --git a/snippets/postgresql.sql b/snippets/postgres/primer.sql similarity index 86% rename from snippets/postgresql.sql rename to snippets/postgres/primer.sql index e5a9785..fcf0189 100644 --- a/snippets/postgresql.sql +++ b/snippets/postgres/primer.sql @@ -1,3 +1,8 @@ +-- Single line comment +/* + * multi-line comment + */ + -- Add '+' to psql commands to get more information @@ -249,6 +254,11 @@ SELECT name FROM customers WHERE name LIKE '%banda%'; -- contains 'banda' SELECT id FROM customers WHERE City LIKE 'L___on'; -- starts with 'L', followed by any 3 characters, followed by 'on' +-- Comparison with lists +SELECT * FROM users WHERE username IN ('matthew', 'lucas', 'todd', 'roxy', 'kyle', 'ken', 'gideon'); +SELECT * FROM users WHERE username NOT IN ('knives', 'wallace'); + + -- Shuffle data -- source: https://stackoverflow.com/questions/33555524/postgresql-shuffle-column-values#33555639 WITH @@ -283,6 +293,24 @@ SELECT setseed(0.25), round(random()::DECIMAL, 15) AS random_number; -- seed mu -- Functions -- Refer -CREATE OR REPLACE FUNCTION return_1() RETURNS integer -LANGUAGE SQL -RETURN 1; +\df +\df+ to_char +SELECT routine_name FROM information_schema.routines WHERE routine_type = 'FUNCTION'; +SELECT p.proname FROM pg_catalog.pg_namespace n JOIN pg_catalog.pg_proc p ON p.pronamespace = n.oid WHERE p.prokind = 'f'; + +CREATE OR REPLACE FUNCTION return_1() RETURNS INTEGER LANGUAGE SQL RETURN 1; + + +-- Type casting +SELECT + CAST ('21' AS INTEGER), 420.69::INTEGER, + CAST('100' AS DOUBLE PRECISION), '100.93'::FLOAT, + CAST ('true' AS BOOLEAN), + CAST('2024-02-01 12:34:56' AS DATE), '01-OCT-2015' ::DATE, + CAST('2016-11-11' AS TIMESTAMP), '2016-11-11'::TIMESTAMP, + CAST ('15 minute' AS INTERVAL), '3 month' :: INTERVAL, + CAST ('20 days' AS TEXT), '24 hour':: TEXT, + CAST (ARRAY[1,3,5] AS TEXT), + '{1,2,3}'::INTEGER[] AS result_array; +SELECT to_char(42, '0000'); -- '0042' +SELECT to_number('12,454.9', '99G999D9S'); diff --git a/snippets/systemd.sh b/snippets/systemd.sh index 29f3683..c81c82b 100644 --- a/snippets/systemd.sh +++ b/snippets/systemd.sh @@ -7,3 +7,5 @@ sudo hostnamectl sudo hostnamectl status --static sudo hostnamectl set-hostname --pretty 'prometheus' + +sudo systemctl list-units --state='failed'