aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils/adt/ri_triggers.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2008-09-15 23:37:49 +0000
committerTom Lane <tgl@sss.pgh.pa.us>2008-09-15 23:37:49 +0000
commite4aea74e1924195d73c79dd68fb88eb7914dbd77 (patch)
tree5e219ab0efdce0b7298d1670d0d92318a69b7c35 /src/backend/utils/adt/ri_triggers.c
parente5b1eed7c916bbe747ee86da815ebe4ea8bdd643 (diff)
downloadpostgresql-e4aea74e1924195d73c79dd68fb88eb7914dbd77.tar.gz
postgresql-e4aea74e1924195d73c79dd68fb88eb7914dbd77.zip
Fix caching of foreign-key-checking queries so that when a replan is needed,
we regenerate the SQL query text not merely the plan derived from it. This is needed to handle contingencies such as renaming of a table or column used in an FK. Pre-8.3, such cases worked despite the lack of replanning (because the cached plan needn't actually change), so this is a regression. Per bug #4417 from Benjamin Bihler.
Diffstat (limited to 'src/backend/utils/adt/ri_triggers.c')
-rw-r--r--src/backend/utils/adt/ri_triggers.c32
1 files changed, 29 insertions, 3 deletions
diff --git a/src/backend/utils/adt/ri_triggers.c b/src/backend/utils/adt/ri_triggers.c
index acb1118f649..874d6464a31 100644
--- a/src/backend/utils/adt/ri_triggers.c
+++ b/src/backend/utils/adt/ri_triggers.c
@@ -15,7 +15,7 @@
*
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
*
- * $PostgreSQL: pgsql/src/backend/utils/adt/ri_triggers.c,v 1.103.2.2 2008/05/19 04:14:33 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/utils/adt/ri_triggers.c,v 1.103.2.3 2008/09/15 23:37:49 tgl Exp $
*
* ----------
*/
@@ -3610,6 +3610,7 @@ static SPIPlanPtr
ri_FetchPreparedPlan(RI_QueryKey *key)
{
RI_QueryHashEntry *entry;
+ SPIPlanPtr plan;
/*
* On the first call initialize the hashtable
@@ -3625,7 +3626,30 @@ ri_FetchPreparedPlan(RI_QueryKey *key)
HASH_FIND, NULL);
if (entry == NULL)
return NULL;
- return entry->plan;
+
+ /*
+ * Check whether the plan is still valid. If it isn't, we don't want
+ * to simply rely on plancache.c to regenerate it; rather we should
+ * start from scratch and rebuild the query text too. This is to cover
+ * cases such as table/column renames. We depend on the plancache
+ * machinery to detect possible invalidations, though.
+ *
+ * CAUTION: this check is only trustworthy if the caller has already
+ * locked both FK and PK rels.
+ */
+ plan = entry->plan;
+ if (plan && SPI_plan_is_valid(plan))
+ return plan;
+
+ /*
+ * Otherwise we might as well flush the cached plan now, to free a
+ * little memory space before we make a new one.
+ */
+ entry->plan = NULL;
+ if (plan)
+ SPI_freeplan(plan);
+
+ return NULL;
}
@@ -3648,11 +3672,13 @@ ri_HashPreparedPlan(RI_QueryKey *key, SPIPlanPtr plan)
ri_InitHashTables();
/*
- * Add the new plan.
+ * Add the new plan. We might be overwriting an entry previously
+ * found invalid by ri_FetchPreparedPlan.
*/
entry = (RI_QueryHashEntry *) hash_search(ri_query_cache,
(void *) key,
HASH_ENTER, &found);
+ Assert(!found || entry->plan == NULL);
entry->plan = plan;
}