mirror of
https://github.com/postgres/postgres.git
synced 2025-07-30 11:03:19 +03:00
Avoid caching expression state trees for domain constraints across queries.
In commit8abb3cda0d
I attempted to cache the expression state trees constructed for domain CHECK constraints for the life of the backend (assuming the domain's constraints don't get redefined). However, this turns out not to work very well, because execQual.c will run those state trees with ecxt_per_query_memory pointing to a query-lifespan context, and in some situations we'll end up with pointers into that context getting stored into the state trees. This happens in particular with SQL-language functions, as reported by Emre Hasegeli, but there are many other cases. To fix, keep only the expression plan trees for domain CHECK constraints in the typcache's data structure, and revert to performing ExecInitExpr (at least) once per query to set up expression state trees in the query's context. Eventually it'd be nice to undo this, but that will require some careful thought about memory management for expression state trees, and it seems far too late for any such redesign in 9.5. This way is still much more efficient than what happened before8abb3cda0
.
This commit is contained in:
@ -682,6 +682,31 @@ select dom_check(0);
|
||||
drop function dom_check(int);
|
||||
drop domain di;
|
||||
--
|
||||
-- Check use of a (non-inline-able) SQL function in a domain constraint;
|
||||
-- this has caused issues in the past
|
||||
--
|
||||
create function sql_is_distinct_from(anyelement, anyelement)
|
||||
returns boolean language sql
|
||||
as 'select $1 is distinct from $2 limit 1';
|
||||
create domain inotnull int
|
||||
check (sql_is_distinct_from(value, null));
|
||||
select 1::inotnull;
|
||||
inotnull
|
||||
----------
|
||||
1
|
||||
(1 row)
|
||||
|
||||
select null::inotnull;
|
||||
ERROR: value for domain inotnull violates check constraint "inotnull_check"
|
||||
create table dom_table (x inotnull);
|
||||
insert into dom_table values ('1');
|
||||
insert into dom_table values (1);
|
||||
insert into dom_table values (null);
|
||||
ERROR: value for domain inotnull violates check constraint "inotnull_check"
|
||||
drop table dom_table;
|
||||
drop domain inotnull;
|
||||
drop function sql_is_distinct_from(anyelement, anyelement);
|
||||
--
|
||||
-- Renaming
|
||||
--
|
||||
create domain testdomain1 as int;
|
||||
|
@ -515,6 +515,30 @@ drop function dom_check(int);
|
||||
|
||||
drop domain di;
|
||||
|
||||
--
|
||||
-- Check use of a (non-inline-able) SQL function in a domain constraint;
|
||||
-- this has caused issues in the past
|
||||
--
|
||||
|
||||
create function sql_is_distinct_from(anyelement, anyelement)
|
||||
returns boolean language sql
|
||||
as 'select $1 is distinct from $2 limit 1';
|
||||
|
||||
create domain inotnull int
|
||||
check (sql_is_distinct_from(value, null));
|
||||
|
||||
select 1::inotnull;
|
||||
select null::inotnull;
|
||||
|
||||
create table dom_table (x inotnull);
|
||||
insert into dom_table values ('1');
|
||||
insert into dom_table values (1);
|
||||
insert into dom_table values (null);
|
||||
|
||||
drop table dom_table;
|
||||
drop domain inotnull;
|
||||
drop function sql_is_distinct_from(anyelement, anyelement);
|
||||
|
||||
--
|
||||
-- Renaming
|
||||
--
|
||||
|
Reference in New Issue
Block a user