Re: [HACKERS] CHECK constraints inconsistencies
Re: [HACKERS] CHECK constraints inconsistencies
От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Michael Glaesemann writes: > In both cases, the CHECK constraint uses a function that is stable or > volatile. It was suggested that functions used in CHECK constraints be > restricted to immutable, This seems reasonable to me. I'm a bit surprised we do not have such a check already. Of course, a user could easily get into the sort of situation you describe anyway, just by lying about the volatility labeling of a user-defined function. But at least we could say it was his fault then ;-) regards, tom lane
Re: [HACKERS] CHECK constraints inconsistencies
От:
Bruno Wolff III <bruno@wolff.to>
Дата:
On Mon, Mar 01, 2004 at 20:28:02 -0500, Tom Lane wrote: > Michael Glaesemann writes: > > In both cases, the CHECK constraint uses a function that is stable or > > volatile. It was suggested that functions used in CHECK constraints be > > restricted to immutable, > > This seems reasonable to me. I'm a bit surprised we do not have such a > check already. There may be times you want to do this. For example you may want a timestamp to be in the past. In this case as long as it was in the past when the data was entered it will continue to be in the past (barring someone resetting the system time). This is something someone might actually check unlike comparing to random numbers. I think just noting that check constraints are only checked on inserts and updates and that this means that check constraints using volatile or stable functions need to be well thought out.
Re: [HACKERS] CHECK constraints inconsistencies
От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Rod Taylor writes: > On Mon, 2004-03-01 at 20:43, Bruno Wolff III wrote: >> Tom Lane wrote: >>> Michael Glaesemann writes: >>>> In both cases, the CHECK constraint uses a function that is stable or >>>> volatile. It was suggested that functions used in CHECK constraints be >>>> restricted to immutable, >>> >>> This seems reasonable to me. I'm a bit surprised we do not have such a >>> check already. >> >> There may be times you want to do this. For example you may want a timestamp >> to be in the past. In this case as long as it was in the past when the > Agreed that this is useful behaviour, but a trigger is usually a better > mechanism for confirming such data as you really only want to check it > when the value is changed. Yes. I was just arguing in a different thread that triggers are the right way to express one-time checks. A constraint notionally expresses an always-true condition. (The SQL spec defines this more formally as a condition that must hold at the completion of every statement or every transaction, depending on the "deferrability" property of the constraint.) We presently support only constraints that are of a form that need only be checked at row insert or update time. It would be inconsistent with the spec to consider that part of the fundamental semantics of check constraints, though --- it's just an implementation restriction. Someday we might want to remove that restriction. Requiring CHECK functions to be immutable is consistent with the existing implementation restriction. Misusing them in the way Bruno suggests is a cute trick, but I think we have to consider it to be gaming the implementation, not a supported way to do things. regards, tom lane