postgresql/src/backend/utils
Tom Lane 8abb3cda0d Use the typcache to cache constraints for domain types.
Previously, we cached domain constraints for the life of a query, or
really for the life of the FmgrInfo struct that was used to invoke
domain_in() or domain_check().  But plpgsql (and probably other places)
are set up to cache such FmgrInfos for the whole lifespan of a session,
which meant they could be enforcing really stale sets of constraints.
On the other hand, searching pg_constraint once per query gets kind of
expensive too: testing says that as much as half the runtime of a
trivial query such as "SELECT 0::domaintype" went into that.

To fix this, delegate the responsibility for tracking a domain's
constraints to the typcache, which has the infrastructure needed to
detect syscache invalidation events that signal possible changes.
This not only removes unnecessary repeat reads of pg_constraint,
but ensures that we never apply stale constraint data: whatever we
use is the current data according to syscache rules.

Unfortunately, the current configuration of the system catalogs means
we have to flush cached domain-constraint data whenever either pg_type
or pg_constraint changes, which happens rather a lot (eg, creation or
deletion of a temp table will do it).  It might be worth rearranging
things to split pg_constraint into two catalogs, of which the domain
constraint one would probably be very low-traffic.  That's a job for
another patch though, and in any case this patch should improve matters
materially even with that handicap.

This patch makes use of the recently-added memory context reset callback
feature to manage the lifespan of domain constraint caches, so that we
don't risk deleting a cache that might be in the midst of evaluation.

Although this is a bug fix as well as a performance improvement, no
back-patch.  There haven't been many if any field complaints about
stale domain constraint checks, so it doesn't seem worth taking the
risk of modifying data structures as basic as MemoryContexts in back
branches.
2015-03-01 14:06:55 -05:00
..
adt Use the typcache to cache constraints for domain types. 2015-03-01 14:06:55 -05:00
cache Use the typcache to cache constraints for domain types. 2015-03-01 14:06:55 -05:00
error Remove remnants of ImmediateInterruptOK handling. 2015-02-03 23:25:47 +01:00
fmgr Use FLEXIBLE_ARRAY_MEMBER in a number of other places. 2015-02-21 16:12:14 -05:00
hash Update copyright for 2015 2015-01-06 11:43:47 -05:00
init Remove remnants of ImmediateInterruptOK handling. 2015-02-03 23:25:47 +01:00
mb Update copyright for 2015 2015-01-06 11:43:47 -05:00
misc Replace checkpoint_segments with min_wal_size and max_wal_size. 2015-02-23 18:53:02 +02:00
mmgr Improve mmgr README. 2015-02-27 20:32:34 -05:00
resowner Update copyright for 2015 2015-01-06 11:43:47 -05:00
sort Use FLEXIBLE_ARRAY_MEMBER in a number of other places. 2015-02-21 16:12:14 -05:00
time Advance backend's advertised xmin more aggressively. 2015-01-17 01:15:23 +02:00
.gitignore Avoid maintaining three separate copies of the error codes list. 2011-02-03 22:32:49 -05:00
Gen_dummy_probes.sed Update copyright for 2015 2015-01-06 11:43:47 -05:00
Gen_fmgrtab.pl Allow forcing nullness of columns during bootstrap. 2015-02-21 22:31:54 +01:00
Makefile Make handling of errcodes.h more consistent with other generated headers. 2011-02-04 09:29:10 -05:00
errcodes.txt Update copyright for 2015 2015-01-06 11:43:47 -05:00
generate-errcodes.pl Update copyright for 2015 2015-01-06 11:43:47 -05:00
probes.d Update copyright for 2015 2015-01-06 11:43:47 -05:00