On 26 Sep 2018, at 12:36, Peter Eisentraut <peter.eisentr...@2ndquadrant.com> wrote: > > On 25/09/2018 01:04, Joe Wildish wrote: >> Having said all that: there are obviously going to be some expressions >> that cannot be proven to have no potential for invalidating the assertion >> truth. I guess this is the prime concern from a concurrency PoV? > > Before we spend more time on this, I think we need to have at least a > plan for that.
Having thought about this some more: the answer could lie in using predicate locks, and enforcing that the transaction be SERIALIZABLE whenever an ASSERTION is triggered. To make use of the predicate locks we'd do a transformation on the ASSERTION expression. I believe that there is derivation, similar to the one mentioned up-thread re: "managers and administrators", that would essentially push predicates into the expression on the basis of the changed data. The semantics of the expression would remain unchanged, but it would mean that when the expression is rechecked, the minimal set of data is read and would therefore not conflict with other DML statements that had triggered the same ASSERTION but had modified unrelated data. Example: CREATE TABLE t (n INTEGER NOT NULL, m INTEGER NOT NULL, k INTEGER NOT NULL, PRIMARY KEY (n, m)); CREATE ASSERTION sum_k_at_most_10 CHECK (NOT EXISTS (SELECT * FROM (SELECT n, sum(k) FROM t GROUP BY n) AS r(n, ks) WHERE ks > 10)); On an INSERT/DELETE/UPDATE of "t", we would transform the inner-most expression of the ASSERTION to have a predicate of "WHERE n = NEW.n". In my experiments I can see that doing so allows concurrent transactions to COMMIT that have modified unrelated segments of "t" (assuming the planner uses Index Scan). The efficacy of this would be dictated by the granularity of the SIREAD locks; my understanding is that this can be as low as tuple-level in the case where Index Scans are used (and this is borne out in my experiments - ie. you don't want a SeqScan). > Perhaps we could should disallow cases that we can't > handle otherwise. But even that would need some analysis of which > practical cases we can and cannot handle, how we could extend support in > the future, etc. The optimisation I mentioned up-thread, plus the one hypothesised here, both rely on being able to derive the key of an expression from the underlying base tables/other expressions. We could perhaps disallow ASSERTIONS that don't have such properties? Beyond that I think it starts to get difficult (impossible?) to know which expressions are likely to be costly on the basis of static analysis. It could be legitimate to have an ASSERTION defined over what turns out to be a small subset of a very large table, for example. -Joe