Skip to content

Instantly share code, notes, and snippets.

@pstef
pstef / 1_recursive CTE pokemon teams.txt
Last active November 4, 2018 09:53
Using recursive CTE to find a pokemon team whose attacks cover the most enemy types
me@localhost postgres=# CREATE SCHEMA pokemon;
CREATE SCHEMA
Time: 9.719 ms
me@localhost postgres=# CREATE TABLE pokemon.types
(type_id INTEGER PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY (MINVALUE 0), name TEXT UNIQUE);
CREATE TABLE
Time: 51.735 ms
me@localhost postgres=# CREATE TABLE pokemon.pokemon
(pokemon_id INTEGER PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY, name TEXT UNIQUE, seq INT);
CREATE TABLE
@pstef
pstef / usertreesort.sql
Last active October 31, 2017 07:16
In-order sort for trees with user-defined ordering of tied vertices
WITH RECURSIVE
categories_rel (id, parent_id, "order") AS (VALUES
(1, NULL, 20), (2, NULL, 10),
(99, 1, 6), (12, 1, 1), (23, 2, 1),
(119, 99, 1), (121, 12, 1),
(1193, 119, 1), (1193, 1, 5), (7, 1, 2), (6, 1, 3)
),
tree (id, parent_id, r, path) AS (
SELECT id, parent_id, 0 AS r, ARRAY["order"], "order"
FROM categories_rel
diff --git a/src/backend/storage/lmgr/proc.c b/src/backend/storage/lmgr/proc.c
index bfa8499..fc9221b 100644
--- a/src/backend/storage/lmgr/proc.c
+++ b/src/backend/storage/lmgr/proc.c
@@ -287,6 +287,14 @@ void
InitProcess(void)
{
PGPROC *volatile *procgloballist;
+#define LIST_TYPE_ENTRY(e) [(e)] = (#e)
+ enum listType { autovac, bgworker, backend } which;
@pstef
pstef / recursive CTE tree
Last active May 28, 2017 19:36
Representing an adjacency list as a tree, using a recursive CTE
WITH RECURSIVE
categories_rel (id, parent_id) AS (VALUES
(1, NULL), (2, NULL),
(11, 1), (12, 1), (23, 2),
(119, 11), (121, 12),
(1193, 119), (1193, 1)
),
tree (id, parent_id, r, path) AS (
SELECT id, parent_id, 0, ARRAY[id]::int[]
FROM categories_rel
@pstef
pstef / Postgres-json-output.txt
Last active October 21, 2018 07:58
Showing of Postgres's features
WITH
manufacturers (name, headquarters) AS (VALUES
('BMW', 'Munich'), ('Toyota', 'Toyota'), ('Fiat', 'Turin')
),
models (manufacturer, name, year) AS (VALUES
('BMW', 'm4', 2000), ('BMW', 'm5', 2000),
('Toyota', 'corolla', 2000), ('Toyota', 'yaris', 2000),
('Fiat', 'panda', 2000), ('Fiat', 'uno', 2000)
),
products (manufacturer, model, color, quantity, pretax) AS (VALUES
psql -d 'linspector' <<EOF
DROP TABLE IF EXISTS symbols, "files";
CREATE TABLE symbols (
filename text NOT NULL,
flag "char" NOT NULL,
symbol text NOT NULL,
PRIMARY KEY (filename, flag, symbol)
);
COPY symbols (filename, flag, symbol) FROM stdin;
@pstef
pstef / last-components.sql
Last active August 29, 2015 13:56
<lluad> Is there a tidier way to get the last two components of a hostname than (regexp_matches(foo, '\.([^.]+\.[^.]+)$'))[1] ? It works, but kinda offends even me. It's only going to be used on hostnames, so I don't really care what it does to non-hostname input.
SELECT array_to_string(a[array_upper(a, 1)-1:array_upper(a, 1)], '.')
FROM string_to_array('gist.github.com', '.') a
@pstef
pstef / enum.c
Created March 21, 2013 13:34
Why I didn't like enum types in C until I learned about clang -Weverything -fsanitize=enum
#include <string.h>
#include <stdio.h>
enum car { honda };
enum number { ZERO, ONE };
enum number function_returning_number(char const *keyword) {
if (!strcmp(keyword, "zero"))
return ZERO;
@pstef
pstef / gist:5189951
Created March 18, 2013 19:13
Toggle Valgrind data collection on entry/exit of function
valgrind --tool=callgrind --toggle-collect=function ./program
@pstef
pstef / gist:5161793
Last active December 14, 2015 22:58
Books to read and re-read.
Kernighan and Ritchie: The C Programming Language
Stephen Prata: C Primer Plus
Brian Hook: Write portable code
Andrew Koenig: C traps and pitfalls
Peter v d Linden: Expert C programming - Deep secrets
C Unleashed
Jon Bentley: Programming Pearls
Robert Sedgewick: Algorithms in C