mirror of
https://gitea.com/mcereda/oam.git
synced 2026-02-09 05:44:23 +00:00
chore(postgres): dump acquired knowledge
This commit is contained in:
@@ -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 | |
|
||||
|
||||
54
snippets/postgres/functions.sql
Normal file
54
snippets/postgres/functions.sql
Normal file
@@ -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 <https://stackoverflow.com/questions/33555524/postgresql-shuffle-column-values#33555639>.
|
||||
*
|
||||
* 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
|
||||
**/
|
||||
@@ -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 <https://www.postgresql.org/docs/current/sql-createfunction.html>
|
||||
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');
|
||||
@@ -7,3 +7,5 @@ sudo hostnamectl
|
||||
sudo hostnamectl status --static
|
||||
|
||||
sudo hostnamectl set-hostname --pretty 'prometheus'
|
||||
|
||||
sudo systemctl list-units --state='failed'
|
||||
|
||||
Reference in New Issue
Block a user