David Fetter
PGCon 2018
May 31, 2018
dfetter@authentic8.com
ASSERTIONSASSERTIONS
AND HOW TO USE THEMAND HOW TO USE THEM
David Fetter
PGCon 2018
May 31, 2018
dfetter@authentic8.com
DAVID FETTERDAVID FETTER
DBA at Authentic8 and PostgreSQL Contributor
David Fetter
PGCon 2018
May 31, 2018
dfetter@authentic8.com
WHAT ARE THEY?WHAT ARE THEY?
Data constraints expressed in SQL
which can span multiple tables in ways we haven't
been able to before!
David Fetter
PGCon 2018
May 31, 2018
dfetter@authentic8.com
TOOL LENDING LIBRARYTOOL LENDING LIBRARY
Fungible tools which people check out to use.
Track this correctly.
David Fetter
PGCon 2018
May 31, 2018
dfetter@authentic8.com
CREATE TABLE IF NOT EXISTS available_tool (
tool_id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
tool_type TEXT NOT NULL,
available INTEGER DEFAULT 5 NOT NULL,
CHECK(available >= 0)
);
David Fetter
PGCon 2018
May 31, 2018
dfetter@authentic8.com
CREATE TABLE IF NOT EXISTS checked_out_tool (
checked_out_by TEXT NOT NULL,
tool_id INTEGER NOT NULL REFERENCES available_tool
);
David Fetter
PGCon 2018
May 31, 2018
dfetter@authentic8.com
CREATE ASSERTION out_of_tools CHECK(
NOT EXISTS (
SELECT 1
FROM
available_tool a
JOIN
checked_out_tool d
ON (
a.tool_id = d.tool_id
)
GROUP BY a.tool_id
HAVING count(*) > a.available
)
);
David Fetter
PGCon 2018
May 31, 2018
dfetter@authentic8.com
LIVE DEMO!LIVE DEMO!
David Fetter
PGCon 2018
May 31, 2018
dfetter@authentic8.com
...ALMOST WORKED...ALMOST WORKED
We need people working on this patch!
Speaker notes
David Fetter
PGCon 2018
May 31, 2018
dfetter@authentic8.com
POSTGRESQL 11?POSTGRESQL 11?
David Fetter
PGCon 2018
May 31, 2018
dfetter@authentic8.com
NOPE :(NOPE :(
Can get this effect in 11?
Speaker notes
David Fetter
PGCon 2018
May 31, 2018
dfetter@authentic8.com
YES!YES!
Is it elegant or easy to get right?
Speaker notes
David Fetter
PGCon 2018
May 31, 2018
dfetter@authentic8.com
NO!NO!
David Fetter
PGCon 2018
May 31, 2018
dfetter@authentic8.com
HOW?HOW?
David Fetter
PGCon 2018
May 31, 2018
dfetter@authentic8.com
A TRIGGERA TRIGGER
David Fetter
PGCon 2018
May 31, 2018
dfetter@authentic8.com
...WITH ITS OWN FUNCTION...WITH ITS OWN FUNCTION
David Fetter
PGCon 2018
May 31, 2018
dfetter@authentic8.com
...FOR EACH TABLE...FOR EACH TABLE
David Fetter
PGCon 2018
May 31, 2018
dfetter@authentic8.com
...FOR EACH ASSERTION...FOR EACH ASSERTION
David Fetter
PGCon 2018
May 31, 2018
dfetter@authentic8.com
LET'S GET STARTED.LET'S GET STARTED.
Trigger function for reaching from available to
checked_out
David Fetter
CREATE OR REPLACE FUNCTION check_checked_out_from_available()
RETURNS TRIGGER
LANGUAGE plpgsql
AS $$
DECLARE tool TEXT;
BEGIN
SELECT INTO tool a.tool_type
FROM available_tool a JOIN checked_out_tool d
ON (
a.tool_id = d.tool_id AND
d.tool_id = NEW.tool_id
)
GROUP BY a.available, a.tool_type
HAVING count(*) > a.available;
IF FOUND THEN
PGCon 2018
May 31, 2018
dfetter@authentic8.com
And the trigger
David Fetter
PGCon 2018
May 31, 2018
CREATE TRIGGER check_checked_out_from_available
AFTER INSERT OR UPDATE ON available_tool
FOR EACH ROW
EXECUTE PROCEDURE check_checked_out_from_available();
dfetter@authentic8.com
Trigger function for reaching out from available to
checked_out
David Fetter
CREATE OR REPLACE FUNCTION check_available_from_checked_out()
RETURNS TRIGGER
LANGUAGE plpgsql
AS $$
DECLARE
r RECORD;
BEGIN
SELECT tool_type, available INTO r
FROM available_tool a JOIN checked_out_tool d USING (tool_id)
WHERE NEW.tool_id = d.tool_id
GROUP BY d.tool_id, a.tool_type, a.available
HAVING count(*) > a.available;
IF FOUND THEN
RAISE 'There are already % checked-out %s.', r.available,
USING
PGCon 2018
May 31, 2018
dfetter@authentic8.com
And its trigger
David Fetter
PGCon 2018
May 31, 2018
CREATE TRIGGER check_available_from_checked_out
AFTER INSERT OR UPDATE ON checked_out_tool
FOR EACH ROW
EXECUTE PROCEDURE check_available_from_checked_out();
dfetter@authentic8.com
David Fetter
PGCon 2018
May 31, 2018
dfetter@authentic8.com
ANOTHER LIVE DEMO!ANOTHER LIVE DEMO!
David Fetter
PGCon 2018
May 31, 2018
dfetter@authentic8.com
HOW DID WE GET HERE?HOW DID WE GET HERE?
David Fetter
PGCon 2018
May 31, 2018
dfetter@authentic8.com
A Relational Model for Large Shared Data Banks
David Fetter
PGCon 2018
May 31, 2018
dfetter@authentic8.com
NAH!NAH!
David Fetter
PGCon 2018
May 31, 2018
dfetter@authentic8.com
Serializable Snapshot Isolation in PostgreSQL
Dan Ports and Kevin Grittner
Talk a little bit about SSI and dependency graphs.
Speaker notes
David Fetter
PGCon 2018
May 31, 2018
dfetter@authentic8.com
From: Peter Eisentraut
To: pgsql-hackers
Subject: [PATCH] SQL assertions prototype
Date: 2013-11-15 03:30:16
Message-ID: 1384486216.5008.17.camel@vanquo.pezone.net
David Fetter
PGCon 2018
May 31, 2018
dfetter@authentic8.com
NOTE DATENOTE DATE
Date: 2013-11-15 03:30:16
David Fetter
PGCon 2018
May 31, 2018
dfetter@authentic8.com
Years pass
David Fetter
PGCon 2018
May 31, 2018
dfetter@authentic8.com
Then
David Fetter
PGCon 2018
May 31, 2018
dfetter@authentic8.com
From: Joe Wildish
To: PostgreSQL Hackers
Subject: Re: Implementing SQL ASSERTION
Date: 2018-01-14 23:33:08
Message-ID: 985632EC-3E39-4C51-B47A-ED0ABF63D64F@elusive.cx
Views: Raw Message | Whole Thread | Download mbox
Hackers,
Attached is a WIP patch for SQL assertion. I am posting it for anyone w
be interested in seeing it, for comments/feedback, and to see if others
to collaborate on taking it further. It is not near production-ready (s
thoughts on that below).
David Fetter
PGCon 2018
May 31, 2018
dfetter@authentic8.com
A few months later...
David Fetter
PGCon 2018
May 31, 2018
dfetter@authentic8.com
From: Joe Wildish
To: David Fetter , PostgreSQL Hackers
Subject: Re: Implementing SQL ASSERTION
Date: 2018-04-29 18:18:00
Attached is a rebased patch for the prototype.
Cheers,
-Joe
David Fetter
PGCon 2018
May 31, 2018
dfetter@authentic8.com
David Fetter
PGCon 2018
May 31, 2018
dfetter@authentic8.com
David Fetter
PGCon 2018
May 31, 2018
dfetter@authentic8.com
David Fetter
PGCon 2018
May 31, 2018
dfetter@authentic8.com
David Fetter
PGCon 2018
May 31, 2018
dfetter@authentic8.com
David Fetter
PGCon 2018
May 31, 2018
dfetter@authentic8.com
David Fetter
PGCon 2018
May 31, 2018
dfetter@authentic8.com
David Fetter
PGCon 2018
May 31, 2018
dfetter@authentic8.com
WHAT'S IN THE BOX?WHAT'S IN THE BOX?
Documentation
pg_catalog
Information schema
SQL grammar
CREATE, ALTER, and DROP support
planner and executor
David Fetter
PGCon 2018
May 31, 2018
dfetter@authentic8.com
Questions?
Comments?
David Fetter
PGCon 2018
May 31, 2018
dfetter@authentic8.com
Thanks!
Merci!
谢谢!ありがとう!¡Gracias!
Teşekkürler!
ध वाद
Спасибо!

Assertions and how to use them

  • 1.
    David Fetter PGCon 2018 May31, 2018 dfetter@authentic8.com ASSERTIONSASSERTIONS AND HOW TO USE THEMAND HOW TO USE THEM
  • 2.
    David Fetter PGCon 2018 May31, 2018 dfetter@authentic8.com DAVID FETTERDAVID FETTER DBA at Authentic8 and PostgreSQL Contributor
  • 3.
    David Fetter PGCon 2018 May31, 2018 dfetter@authentic8.com WHAT ARE THEY?WHAT ARE THEY? Data constraints expressed in SQL which can span multiple tables in ways we haven't been able to before!
  • 4.
    David Fetter PGCon 2018 May31, 2018 dfetter@authentic8.com TOOL LENDING LIBRARYTOOL LENDING LIBRARY Fungible tools which people check out to use. Track this correctly.
  • 5.
    David Fetter PGCon 2018 May31, 2018 dfetter@authentic8.com CREATE TABLE IF NOT EXISTS available_tool ( tool_id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, tool_type TEXT NOT NULL, available INTEGER DEFAULT 5 NOT NULL, CHECK(available >= 0) );
  • 6.
    David Fetter PGCon 2018 May31, 2018 dfetter@authentic8.com CREATE TABLE IF NOT EXISTS checked_out_tool ( checked_out_by TEXT NOT NULL, tool_id INTEGER NOT NULL REFERENCES available_tool );
  • 7.
    David Fetter PGCon 2018 May31, 2018 dfetter@authentic8.com CREATE ASSERTION out_of_tools CHECK( NOT EXISTS ( SELECT 1 FROM available_tool a JOIN checked_out_tool d ON ( a.tool_id = d.tool_id ) GROUP BY a.tool_id HAVING count(*) > a.available ) );
  • 8.
    David Fetter PGCon 2018 May31, 2018 dfetter@authentic8.com LIVE DEMO!LIVE DEMO!
  • 9.
    David Fetter PGCon 2018 May31, 2018 dfetter@authentic8.com ...ALMOST WORKED...ALMOST WORKED We need people working on this patch! Speaker notes
  • 10.
    David Fetter PGCon 2018 May31, 2018 dfetter@authentic8.com POSTGRESQL 11?POSTGRESQL 11?
  • 11.
    David Fetter PGCon 2018 May31, 2018 dfetter@authentic8.com NOPE :(NOPE :( Can get this effect in 11? Speaker notes
  • 12.
    David Fetter PGCon 2018 May31, 2018 dfetter@authentic8.com YES!YES! Is it elegant or easy to get right? Speaker notes
  • 13.
    David Fetter PGCon 2018 May31, 2018 dfetter@authentic8.com NO!NO!
  • 14.
    David Fetter PGCon 2018 May31, 2018 dfetter@authentic8.com HOW?HOW?
  • 15.
    David Fetter PGCon 2018 May31, 2018 dfetter@authentic8.com A TRIGGERA TRIGGER
  • 16.
    David Fetter PGCon 2018 May31, 2018 dfetter@authentic8.com ...WITH ITS OWN FUNCTION...WITH ITS OWN FUNCTION
  • 17.
    David Fetter PGCon 2018 May31, 2018 dfetter@authentic8.com ...FOR EACH TABLE...FOR EACH TABLE
  • 18.
    David Fetter PGCon 2018 May31, 2018 dfetter@authentic8.com ...FOR EACH ASSERTION...FOR EACH ASSERTION
  • 19.
    David Fetter PGCon 2018 May31, 2018 dfetter@authentic8.com LET'S GET STARTED.LET'S GET STARTED.
  • 20.
    Trigger function forreaching from available to checked_out David Fetter CREATE OR REPLACE FUNCTION check_checked_out_from_available() RETURNS TRIGGER LANGUAGE plpgsql AS $$ DECLARE tool TEXT; BEGIN SELECT INTO tool a.tool_type FROM available_tool a JOIN checked_out_tool d ON ( a.tool_id = d.tool_id AND d.tool_id = NEW.tool_id ) GROUP BY a.available, a.tool_type HAVING count(*) > a.available; IF FOUND THEN
  • 21.
    PGCon 2018 May 31,2018 dfetter@authentic8.com
  • 22.
    And the trigger DavidFetter PGCon 2018 May 31, 2018 CREATE TRIGGER check_checked_out_from_available AFTER INSERT OR UPDATE ON available_tool FOR EACH ROW EXECUTE PROCEDURE check_checked_out_from_available();
  • 23.
  • 24.
    Trigger function forreaching out from available to checked_out David Fetter CREATE OR REPLACE FUNCTION check_available_from_checked_out() RETURNS TRIGGER LANGUAGE plpgsql AS $$ DECLARE r RECORD; BEGIN SELECT tool_type, available INTO r FROM available_tool a JOIN checked_out_tool d USING (tool_id) WHERE NEW.tool_id = d.tool_id GROUP BY d.tool_id, a.tool_type, a.available HAVING count(*) > a.available; IF FOUND THEN RAISE 'There are already % checked-out %s.', r.available, USING
  • 25.
    PGCon 2018 May 31,2018 dfetter@authentic8.com
  • 26.
    And its trigger DavidFetter PGCon 2018 May 31, 2018 CREATE TRIGGER check_available_from_checked_out AFTER INSERT OR UPDATE ON checked_out_tool FOR EACH ROW EXECUTE PROCEDURE check_available_from_checked_out();
  • 27.
  • 28.
    David Fetter PGCon 2018 May31, 2018 dfetter@authentic8.com ANOTHER LIVE DEMO!ANOTHER LIVE DEMO!
  • 29.
    David Fetter PGCon 2018 May31, 2018 dfetter@authentic8.com HOW DID WE GET HERE?HOW DID WE GET HERE?
  • 30.
    David Fetter PGCon 2018 May31, 2018 dfetter@authentic8.com A Relational Model for Large Shared Data Banks
  • 31.
    David Fetter PGCon 2018 May31, 2018 dfetter@authentic8.com NAH!NAH!
  • 32.
    David Fetter PGCon 2018 May31, 2018 dfetter@authentic8.com Serializable Snapshot Isolation in PostgreSQL Dan Ports and Kevin Grittner Talk a little bit about SSI and dependency graphs. Speaker notes
  • 33.
    David Fetter PGCon 2018 May31, 2018 dfetter@authentic8.com From: Peter Eisentraut To: pgsql-hackers Subject: [PATCH] SQL assertions prototype Date: 2013-11-15 03:30:16 Message-ID: 1384486216.5008.17.camel@vanquo.pezone.net
  • 34.
    David Fetter PGCon 2018 May31, 2018 dfetter@authentic8.com NOTE DATENOTE DATE Date: 2013-11-15 03:30:16
  • 35.
    David Fetter PGCon 2018 May31, 2018 dfetter@authentic8.com Years pass
  • 36.
    David Fetter PGCon 2018 May31, 2018 dfetter@authentic8.com Then
  • 37.
    David Fetter PGCon 2018 May31, 2018 dfetter@authentic8.com From: Joe Wildish To: PostgreSQL Hackers Subject: Re: Implementing SQL ASSERTION Date: 2018-01-14 23:33:08 Message-ID: 985632EC-3E39-4C51-B47A-ED0ABF63D64F@elusive.cx Views: Raw Message | Whole Thread | Download mbox Hackers, Attached is a WIP patch for SQL assertion. I am posting it for anyone w be interested in seeing it, for comments/feedback, and to see if others to collaborate on taking it further. It is not near production-ready (s thoughts on that below).
  • 38.
    David Fetter PGCon 2018 May31, 2018 dfetter@authentic8.com A few months later...
  • 39.
    David Fetter PGCon 2018 May31, 2018 dfetter@authentic8.com From: Joe Wildish To: David Fetter , PostgreSQL Hackers Subject: Re: Implementing SQL ASSERTION Date: 2018-04-29 18:18:00 Attached is a rebased patch for the prototype. Cheers, -Joe
  • 40.
    David Fetter PGCon 2018 May31, 2018 dfetter@authentic8.com David Fetter PGCon 2018 May 31, 2018 dfetter@authentic8.com David Fetter PGCon 2018 May 31, 2018 dfetter@authentic8.com David Fetter PGCon 2018 May 31, 2018 dfetter@authentic8.com David Fetter PGCon 2018 May 31, 2018 dfetter@authentic8.com David Fetter PGCon 2018 May 31, 2018 dfetter@authentic8.com David Fetter PGCon 2018 May 31, 2018 dfetter@authentic8.com David Fetter PGCon 2018 May 31, 2018 dfetter@authentic8.com WHAT'S IN THE BOX?WHAT'S IN THE BOX? Documentation pg_catalog Information schema SQL grammar CREATE, ALTER, and DROP support planner and executor
  • 41.
    David Fetter PGCon 2018 May31, 2018 dfetter@authentic8.com Questions? Comments?
  • 42.
    David Fetter PGCon 2018 May31, 2018 dfetter@authentic8.com Thanks! Merci! 谢谢!ありがとう!¡Gracias! Teşekkürler! ध वाद Спасибо!