From c9be627ab93cb4eebe6c3f08b9f3c497b688a6e7 Mon Sep 17 00:00:00 2001 From: Jesse Zhang Date: Thu, 1 Aug 2019 11:26:37 -0700 Subject: [PATCH 1/3] WIP --- .gitignore | 0 partition-review/fun.sql | 60 ++++++++++++++++ partition-review/setup.sql | 137 +++++++++++++++++++++++++++++++++++++ 3 files changed, 197 insertions(+) create mode 100644 .gitignore create mode 100644 partition-review/fun.sql create mode 100644 partition-review/setup.sql diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/partition-review/fun.sql b/partition-review/fun.sql new file mode 100644 index 0000000..00874d3 --- /dev/null +++ b/partition-review/fun.sql @@ -0,0 +1,60 @@ +CREATE TABLE foo +( + a int, + b int, + c int, + d int +) DISTRIBUTED BY (a) + PARTITION BY RANGE (b) + SUBPARTITION BY RANGE (c) + (PARTITION primero START (0) END (42) ( + SUBPARTITION alpha START(100) END(102), + SUBPARTITION beta START(102) END(104), + SUBPARTITION charlie START(200) END(202) + ), + PARTITION segundo START (42) END (82) ( + SUBPARTITION delta START(202) END(204) + ) + ); + +SELECT * +FROM indices_of('foo'); + +CREATE TEMP VIEW yolo AS +SELECT classid::regclass, + describe(classid, objid, objsubid) dependent, + array_agg(describe(refclassid, refobjid, refobjsubid)) referenced, + deptype +FROM pg_depend dep +WHERE deptype IN ('P', 'S', 'I', 'a') + AND NOT EXISTS(SELECT 1 + FROM pg_constraint + WHERE objid = oid + AND contype = 'c' + AND classid = 'pg_constraint'::regclass) + AND objid > 16000 +GROUP BY objid, classid, objsubid, deptype; + +CREATE INDEX segundo_abcd ON foo_1_prt_segundo (a, b, c, d); +CREATE INDEX foo_abcd ON foo (a, b, c, d); +DROP INDEX foo_abcd; + +-- SELECT classid, describe(classid, objid, 0) FROM dependents_of('foo'); +-- SELECT * FROM constraints_of('foo'); +SELECT * +FROM yolo; +SELECT * +FROM indices_of('foo'); + +ALTER TABLE foo + ALTER PARTITION segundo ADD PARTITION echo START (204) END (206); +SELECT * +FROM indices_of('foo'); + +CREATE TABLE bar +( + LIKE foo +); +ALTER TABLE foo + ALTER PARTITION segundo EXCHANGE PARTITION echo WITH TABLE bar; +DROP TABLE bar; diff --git a/partition-review/setup.sql b/partition-review/setup.sql new file mode 100644 index 0000000..7f46faa --- /dev/null +++ b/partition-review/setup.sql @@ -0,0 +1,137 @@ +CREATE FUNCTION relations_of(root regclass) + RETURNS TABLE + ( + rel regclass + ) + LANGUAGE SQL + STABLE +AS +$fn$ +WITH RECURSIVE t(rel) AS ( + SELECT root + UNION ALL + SELECT inhrelid FROM pg_inherits JOIN t ON inhparent = rel +) +SELECT rel FROM t; +$fn$; + +CREATE FUNCTION describe(pg_constraint) RETURNS text + LANGUAGE SQL + STRICT STABLE AS +$fn$ +SELECT 'con:' || $1.conname || ' (of ' || $1.conrelid::regclass || ')'; +$fn$; + +CREATE FUNCTION describe(pg_rewrite) RETURNS text + LANGUAGE SQL + STRICT STABLE AS +$fn$ +SELECT 'view:' || $1.ev_class::regclass; +$fn$; + +CREATE FUNCTION describe(pg_type, regtype) RETURNS text + LANGUAGE SQL + STRICT STABLE AS +$fn$ +SELECT 'typ:' || $2; +$fn$; + +CREATE FUNCTION describe(pg_namespace) RETURNS text + LANGUAGE SQL + STRICT STABLE AS +$fn$ +SELECT 'nsp:' || $1.nspname; +$fn$; + +CREATE FUNCTION describe(pg_class, regclass, subid int) RETURNS text + LANGUAGE SQL + STRICT STABLE AS +$fn$ +SELECT CASE + WHEN relkind IN ('i', 'I') THEN 'idx:' || rel.relname + ELSE COALESCE('col:' || rel.relname || '.' || attname, + 'rel:' || rel.relname) END +FROM (VALUES ($2, $1.relname, $1.relkind, $3)) rel(oid, relname, relkind, attnum) + LEFT JOIN pg_attribute att + ON $2 = att.attrelid AND rel.attnum = att.attnum + ; +$fn$; + +CREATE FUNCTION describe(classid regclass, objid oid, objsubid int) RETURNS text + LANGUAGE SQL + STABLE AS +$fn$ +SELECT COALESCE(describe(con), describe(rel, rel.oid, objsubid), describe(rule), + describe(nsp), describe(typ, typ.oid)) +FROM (VALUES ($1, $2, $3)) t(classid, objid, objsubid) + LEFT JOIN pg_constraint con + ON 'pg_constraint'::regclass = classid AND con.oid = objid + LEFT JOIN pg_class rel + ON 'pg_class'::regclass = classid AND rel.oid = objid + LEFT JOIN pg_type typ + ON 'pg_type'::regclass = classid AND typ.oid = objid + LEFT JOIN pg_rewrite rule + ON 'pg_rewrite'::regclass = classid AND rule.oid = objid + LEFT JOIN pg_namespace nsp + ON 'pg_namespace'::regclass = classid AND nsp.oid = objid + ; +$fn$; + +CREATE FUNCTION dependents_of(root regclass) + RETURNS TABLE + ( + classid regclass, + objid oid + ) + LANGUAGE SQL + STABLE +AS +$fn$ +WITH RECURSIVE t(classid, objid, refclassid, refobjid, refobjsubid, deptype) AS ( + SELECT dep.classid, dep.objid, dep.refclassid, dep.refobjid, dep.refobjsubid, dep.deptype + FROM pg_depend dep + WHERE 'pg_class'::regclass = dep.refclassid + AND dep.refobjid = $1 + UNION + SELECT dep.classid, dep.objid, dep.refclassid, dep.refobjid, dep.refobjsubid, dep.deptype + FROM pg_depend dep JOIN t ON + t.classid = dep.refclassid + AND t.objid = dep.refobjid +) +SELECT classid, objid +FROM t +GROUP BY classid, objid +$fn$; + +CREATE FUNCTION constraints_of(root regclass) + RETURNS TABLE + ( + relid regclass, + contype "char", + conname name, + conindid regclass + ) + LANGUAGE SQL + STABLE +AS +$fn$ +SELECT con.conrelid, con.contype, con.conname, con.conindid +FROM pg_constraint con +WHERE contype IN ('u', 'p') + AND conrelid IN (SELECT * FROM relations_of(root)); +$fn$; + +CREATE FUNCTION indices_of(root regclass) + RETURNS TABLE + ( + relid regclass, + indexrelid regclass + ) + LANGUAGE SQL + STABLE +AS +$fn$ +SELECT ind.indrelid, ind.indexrelid +FROM pg_index ind +WHERE indrelid IN (SELECT * FROM relations_of(root)); +$fn$; From 0a13b568baf3b7deb25deceb5f4a9b9a919e82d3 Mon Sep 17 00:00:00 2001 From: Jesse Zhang Date: Thu, 1 Aug 2019 14:22:57 -0700 Subject: [PATCH 2/3] comment --- partition-review/fun.sql | 1 + 1 file changed, 1 insertion(+) diff --git a/partition-review/fun.sql b/partition-review/fun.sql index 00874d3..d20f958 100644 --- a/partition-review/fun.sql +++ b/partition-review/fun.sql @@ -27,6 +27,7 @@ SELECT classid::regclass, deptype FROM pg_depend dep WHERE deptype IN ('P', 'S', 'I', 'a') + -- filter out the clutter of check constraints AND NOT EXISTS(SELECT 1 FROM pg_constraint WHERE objid = oid From 0a9a712f5155254716ebd38785ec0e01ac1bbef7 Mon Sep 17 00:00:00 2001 From: Jesse Zhang Date: Thu, 1 Aug 2019 17:39:02 -0700 Subject: [PATCH 3/3] stuff --- partition-review/fun.sql | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/partition-review/fun.sql b/partition-review/fun.sql index d20f958..06b2f2a 100644 --- a/partition-review/fun.sql +++ b/partition-review/fun.sql @@ -36,26 +36,19 @@ WHERE deptype IN ('P', 'S', 'I', 'a') AND objid > 16000 GROUP BY objid, classid, objsubid, deptype; -CREATE INDEX segundo_abcd ON foo_1_prt_segundo (a, b, c, d); -CREATE INDEX foo_abcd ON foo (a, b, c, d); -DROP INDEX foo_abcd; - --- SELECT classid, describe(classid, objid, 0) FROM dependents_of('foo'); --- SELECT * FROM constraints_of('foo'); -SELECT * -FROM yolo; -SELECT * -FROM indices_of('foo'); - ALTER TABLE foo - ALTER PARTITION segundo ADD PARTITION echo START (204) END (206); + ADD CONSTRAINT foo_pk PRIMARY KEY (a, b, c, d); + SELECT * FROM indices_of('foo'); -CREATE TABLE bar +CREATE TABLE jazz ( - LIKE foo + e int, + f int ); -ALTER TABLE foo - ALTER PARTITION segundo EXCHANGE PARTITION echo WITH TABLE bar; -DROP TABLE bar; +EXPLAIN + SELECT e, f + FROM jazz + LEFT JOIN foo + ON e = a AND 0 = b AND 100 = c AND f = d;