From 8911e13329809ce2c44aecd82a460e54ad164d25 Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Sat, 29 Mar 2025 16:56:23 -0400
Subject: [PATCH v10 3/6] Factor out plpgsql's management of its function
 cache.

SQL-language functions need precisely this same functionality to
manage a long-lived cache of functions.  Rather than duplicating
or reinventing that code, let's split it out into a new module
funccache.c so that it is available for any language that wants
to use it.

This is mostly an exercise in moving and renaming code, and should
not change any behavior.  I have added one feature that plpgsql
doesn't use but SQL functions will need: the cache lookup key
can include the output tuple descriptor when the function
returns composite.

Author: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/8216639.NyiUUSuA9g@aivenlaptop
---
 src/backend/utils/cache/Makefile    |   1 +
 src/backend/utils/cache/funccache.c | 612 ++++++++++++++++++++++++++++
 src/backend/utils/cache/meson.build |   1 +
 src/include/utils/funccache.h       | 134 ++++++
 src/pl/plpgsql/src/pl_comp.c        | 433 ++------------------
 src/pl/plpgsql/src/pl_funcs.c       |   9 +-
 src/pl/plpgsql/src/pl_handler.c     |  15 +-
 src/pl/plpgsql/src/plpgsql.h        |  45 +-
 src/tools/pgindent/typedefs.list    |   5 +
 9 files changed, 811 insertions(+), 444 deletions(-)
 create mode 100644 src/backend/utils/cache/funccache.c
 create mode 100644 src/include/utils/funccache.h

diff --git a/src/backend/utils/cache/Makefile b/src/backend/utils/cache/Makefile
index 5105018cb79..77b3e1a037b 100644
--- a/src/backend/utils/cache/Makefile
+++ b/src/backend/utils/cache/Makefile
@@ -16,6 +16,7 @@ OBJS = \
 	attoptcache.o \
 	catcache.o \
 	evtcache.o \
+	funccache.o \
 	inval.o \
 	lsyscache.o \
 	partcache.o \
diff --git a/src/backend/utils/cache/funccache.c b/src/backend/utils/cache/funccache.c
new file mode 100644
index 00000000000..203d17f2459
--- /dev/null
+++ b/src/backend/utils/cache/funccache.c
@@ -0,0 +1,612 @@
+/*-------------------------------------------------------------------------
+ *
+ * funccache.c
+ *	  Function cache management.
+ *
+ * funccache.c manages a cache of function execution data.  The cache
+ * is used by SQL-language and PL/pgSQL functions, and could be used by
+ * other function languages.  Each cache entry is specific to the execution
+ * of a particular function (identified by OID) with specific input data
+ * types; so a polymorphic function could have many associated cache entries.
+ * Trigger functions similarly have a cache entry per trigger.  These rules
+ * allow the cached data to be specific to the particular data types the
+ * function call will be dealing with.
+ *
+ *
+ * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * IDENTIFICATION
+ *	  src/backend/utils/cache/funccache.c
+ *
+ *-------------------------------------------------------------------------
+ */
+#include "postgres.h"
+
+#include "commands/event_trigger.h"
+#include "commands/trigger.h"
+#include "common/hashfn.h"
+#include "funcapi.h"
+#include "catalog/pg_proc.h"
+#include "utils/funccache.h"
+#include "utils/hsearch.h"
+#include "utils/syscache.h"
+
+
+/*
+ * Hash table for cached functions
+ */
+static HTAB *cfunc_hashtable = NULL;
+
+typedef struct CachedFunctionHashEntry
+{
+	CachedFunctionHashKey key;	/* hash key, must be first */
+	CachedFunction *function;	/* points to data of language-specific size */
+} CachedFunctionHashEntry;
+
+#define FUNCS_PER_USER		128 /* initial table size */
+
+static uint32 cfunc_hash(const void *key, Size keysize);
+static int	cfunc_match(const void *key1, const void *key2, Size keysize);
+
+
+/*
+ * Initialize the hash table on first use.
+ *
+ * The hash table will be in TopMemoryContext regardless of caller's context.
+ */
+static void
+cfunc_hashtable_init(void)
+{
+	HASHCTL		ctl;
+
+	/* don't allow double-initialization */
+	Assert(cfunc_hashtable == NULL);
+
+	ctl.keysize = sizeof(CachedFunctionHashKey);
+	ctl.entrysize = sizeof(CachedFunctionHashEntry);
+	ctl.hash = cfunc_hash;
+	ctl.match = cfunc_match;
+	cfunc_hashtable = hash_create("Cached function hash",
+								  FUNCS_PER_USER,
+								  &ctl,
+								  HASH_ELEM | HASH_FUNCTION | HASH_COMPARE);
+}
+
+/*
+ * cfunc_hash: hash function for cfunc hash table
+ *
+ * We need special hash and match functions to deal with the optional
+ * presence of a TupleDesc in the hash keys.  As long as we have to do
+ * that, we might as well also be smart about not comparing unused
+ * elements of the argtypes arrays.
+ */
+static uint32
+cfunc_hash(const void *key, Size keysize)
+{
+	const CachedFunctionHashKey *k = (const CachedFunctionHashKey *) key;
+	uint32		h;
+
+	Assert(keysize == sizeof(CachedFunctionHashKey));
+	/* Hash all the fixed fields except callResultType */
+	h = DatumGetUInt32(hash_any((const unsigned char *) k,
+								offsetof(CachedFunctionHashKey, callResultType)));
+	/* Incorporate input argument types */
+	if (k->nargs > 0)
+		h = hash_combine(h,
+						 DatumGetUInt32(hash_any((const unsigned char *) k->argtypes,
+												 k->nargs * sizeof(Oid))));
+	/* Incorporate callResultType if present */
+	if (k->callResultType)
+		h = hash_combine(h, hashRowType(k->callResultType));
+	return h;
+}
+
+/*
+ * cfunc_match: match function to use with cfunc_hash
+ */
+static int
+cfunc_match(const void *key1, const void *key2, Size keysize)
+{
+	const CachedFunctionHashKey *k1 = (const CachedFunctionHashKey *) key1;
+	const CachedFunctionHashKey *k2 = (const CachedFunctionHashKey *) key2;
+
+	Assert(keysize == sizeof(CachedFunctionHashKey));
+	/* Compare all the fixed fields except callResultType */
+	if (memcmp(k1, k2, offsetof(CachedFunctionHashKey, callResultType)) != 0)
+		return 1;				/* not equal */
+	/* Compare input argument types (we just verified that nargs matches) */
+	if (k1->nargs > 0 &&
+		memcmp(k1->argtypes, k2->argtypes, k1->nargs * sizeof(Oid)) != 0)
+		return 1;				/* not equal */
+	/* Compare callResultType */
+	if (k1->callResultType)
+	{
+		if (k2->callResultType)
+		{
+			if (!equalRowTypes(k1->callResultType, k2->callResultType))
+				return 1;		/* not equal */
+		}
+		else
+			return 1;			/* not equal */
+	}
+	else
+	{
+		if (k2->callResultType)
+			return 1;			/* not equal */
+	}
+	return 0;					/* equal */
+}
+
+/*
+ * Look up the CachedFunction for the given hash key.
+ * Returns NULL if not present.
+ */
+static CachedFunction *
+cfunc_hashtable_lookup(CachedFunctionHashKey *func_key)
+{
+	CachedFunctionHashEntry *hentry;
+
+	if (cfunc_hashtable == NULL)
+		return NULL;
+
+	hentry = (CachedFunctionHashEntry *) hash_search(cfunc_hashtable,
+													 func_key,
+													 HASH_FIND,
+													 NULL);
+	if (hentry)
+		return hentry->function;
+	else
+		return NULL;
+}
+
+/*
+ * Insert a hash table entry.
+ */
+static void
+cfunc_hashtable_insert(CachedFunction *function,
+					   CachedFunctionHashKey *func_key)
+{
+	CachedFunctionHashEntry *hentry;
+	bool		found;
+
+	if (cfunc_hashtable == NULL)
+		cfunc_hashtable_init();
+
+	hentry = (CachedFunctionHashEntry *) hash_search(cfunc_hashtable,
+													 func_key,
+													 HASH_ENTER,
+													 &found);
+	if (found)
+		elog(WARNING, "trying to insert a function that already exists");
+
+	/*
+	 * If there's a callResultType, copy it into TopMemoryContext.  If we're
+	 * unlucky enough for that to fail, leave the entry with null
+	 * callResultType, which will probably never match anything.
+	 */
+	if (func_key->callResultType)
+	{
+		MemoryContext oldcontext = MemoryContextSwitchTo(TopMemoryContext);
+
+		hentry->key.callResultType = NULL;
+		hentry->key.callResultType = CreateTupleDescCopy(func_key->callResultType);
+		MemoryContextSwitchTo(oldcontext);
+	}
+
+	hentry->function = function;
+
+	/* Set back-link from function to hashtable key */
+	function->fn_hashkey = &hentry->key;
+}
+
+/*
+ * Delete a hash table entry.
+ */
+static void
+cfunc_hashtable_delete(CachedFunction *function)
+{
+	CachedFunctionHashEntry *hentry;
+	TupleDesc	tupdesc;
+
+	/* do nothing if not in table */
+	if (function->fn_hashkey == NULL)
+		return;
+
+	/*
+	 * We need to free the callResultType if present, which is slightly tricky
+	 * because it has to be valid during the hashtable search.  Fortunately,
+	 * because we have the hashkey back-link, we can grab that pointer before
+	 * deleting the hashtable entry.
+	 */
+	tupdesc = function->fn_hashkey->callResultType;
+
+	hentry = (CachedFunctionHashEntry *) hash_search(cfunc_hashtable,
+													 function->fn_hashkey,
+													 HASH_REMOVE,
+													 NULL);
+	if (hentry == NULL)
+		elog(WARNING, "trying to delete function that does not exist");
+
+	/* Remove back link, which no longer points to allocated storage */
+	function->fn_hashkey = NULL;
+
+	/* Release the callResultType if present */
+	if (tupdesc)
+		FreeTupleDesc(tupdesc);
+}
+
+/*
+ * Compute the hashkey for a given function invocation
+ *
+ * The hashkey is returned into the caller-provided storage at *hashkey.
+ * Note however that if a callResultType is incorporated, we've not done
+ * anything about copying that.
+ */
+static void
+compute_function_hashkey(FunctionCallInfo fcinfo,
+						 Form_pg_proc procStruct,
+						 CachedFunctionHashKey *hashkey,
+						 Size cacheEntrySize,
+						 bool includeResultType,
+						 bool forValidator)
+{
+	/* Make sure pad bytes within fixed part of the struct are zero */
+	memset(hashkey, 0, offsetof(CachedFunctionHashKey, argtypes));
+
+	/* get function OID */
+	hashkey->funcOid = fcinfo->flinfo->fn_oid;
+
+	/* get call context */
+	hashkey->isTrigger = CALLED_AS_TRIGGER(fcinfo);
+	hashkey->isEventTrigger = CALLED_AS_EVENT_TRIGGER(fcinfo);
+
+	/* record cacheEntrySize so multiple languages can share hash table */
+	hashkey->cacheEntrySize = cacheEntrySize;
+
+	/*
+	 * If DML trigger, include trigger's OID in the hash, so that each trigger
+	 * usage gets a different hash entry, allowing for e.g. different relation
+	 * rowtypes or transition table names.  In validation mode we do not know
+	 * what relation or transition table names are intended to be used, so we
+	 * leave trigOid zero; the hash entry built in this case will never be
+	 * used for any actual calls.
+	 *
+	 * We don't currently need to distinguish different event trigger usages
+	 * in the same way, since the special parameter variables don't vary in
+	 * type in that case.
+	 */
+	if (hashkey->isTrigger && !forValidator)
+	{
+		TriggerData *trigdata = (TriggerData *) fcinfo->context;
+
+		hashkey->trigOid = trigdata->tg_trigger->tgoid;
+	}
+
+	/* get input collation, if known */
+	hashkey->inputCollation = fcinfo->fncollation;
+
+	/*
+	 * We include only input arguments in the hash key, since output argument
+	 * types can be deduced from those, and it would require extra cycles to
+	 * include the output arguments.  But we have to resolve any polymorphic
+	 * argument types to the real types for the call.
+	 */
+	if (procStruct->pronargs > 0)
+	{
+		hashkey->nargs = procStruct->pronargs;
+		memcpy(hashkey->argtypes, procStruct->proargtypes.values,
+			   procStruct->pronargs * sizeof(Oid));
+		cfunc_resolve_polymorphic_argtypes(procStruct->pronargs,
+										   hashkey->argtypes,
+										   NULL,	/* all args are inputs */
+										   fcinfo->flinfo->fn_expr,
+										   forValidator,
+										   NameStr(procStruct->proname));
+	}
+
+	/*
+	 * While regular OUT arguments are sufficiently represented by the
+	 * resolved input arguments, a function returning composite has additional
+	 * variability: ALTER TABLE/ALTER TYPE could affect what it returns. Also,
+	 * a function returning RECORD may depend on a column definition list to
+	 * determine its output rowtype.  If the caller needs the exact result
+	 * type to be part of the hash lookup key, we must run
+	 * get_call_result_type() to find that out.
+	 */
+	if (includeResultType)
+	{
+		Oid			resultTypeId;
+		TupleDesc	tupdesc;
+
+		switch (get_call_result_type(fcinfo, &resultTypeId, &tupdesc))
+		{
+			case TYPEFUNC_COMPOSITE:
+			case TYPEFUNC_COMPOSITE_DOMAIN:
+				hashkey->callResultType = tupdesc;
+				break;
+			default:
+				/* scalar result, or indeterminate rowtype */
+				break;
+		}
+	}
+}
+
+/*
+ * This is the same as the standard resolve_polymorphic_argtypes() function,
+ * except that:
+ * 1. We go ahead and report the error if we can't resolve the types.
+ * 2. We treat RECORD-type input arguments (not output arguments) as if
+ *    they were polymorphic, replacing their types with the actual input
+ *    types if we can determine those.  This allows us to create a separate
+ *    function cache entry for each named composite type passed to such an
+ *    argument.
+ * 3. In validation mode, we have no inputs to look at, so assume that
+ *    polymorphic arguments are integer, integer-array or integer-range.
+ */
+void
+cfunc_resolve_polymorphic_argtypes(int numargs,
+								   Oid *argtypes, char *argmodes,
+								   Node *call_expr, bool forValidator,
+								   const char *proname)
+{
+	int			i;
+
+	if (!forValidator)
+	{
+		int			inargno;
+
+		/* normal case, pass to standard routine */
+		if (!resolve_polymorphic_argtypes(numargs, argtypes, argmodes,
+										  call_expr))
+			ereport(ERROR,
+					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+					 errmsg("could not determine actual argument "
+							"type for polymorphic function \"%s\"",
+							proname)));
+		/* also, treat RECORD inputs (but not outputs) as polymorphic */
+		inargno = 0;
+		for (i = 0; i < numargs; i++)
+		{
+			char		argmode = argmodes ? argmodes[i] : PROARGMODE_IN;
+
+			if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE)
+				continue;
+			if (argtypes[i] == RECORDOID || argtypes[i] == RECORDARRAYOID)
+			{
+				Oid			resolvedtype = get_call_expr_argtype(call_expr,
+																 inargno);
+
+				if (OidIsValid(resolvedtype))
+					argtypes[i] = resolvedtype;
+			}
+			inargno++;
+		}
+	}
+	else
+	{
+		/* special validation case (no need to do anything for RECORD) */
+		for (i = 0; i < numargs; i++)
+		{
+			switch (argtypes[i])
+			{
+				case ANYELEMENTOID:
+				case ANYNONARRAYOID:
+				case ANYENUMOID:	/* XXX dubious */
+				case ANYCOMPATIBLEOID:
+				case ANYCOMPATIBLENONARRAYOID:
+					argtypes[i] = INT4OID;
+					break;
+				case ANYARRAYOID:
+				case ANYCOMPATIBLEARRAYOID:
+					argtypes[i] = INT4ARRAYOID;
+					break;
+				case ANYRANGEOID:
+				case ANYCOMPATIBLERANGEOID:
+					argtypes[i] = INT4RANGEOID;
+					break;
+				case ANYMULTIRANGEOID:
+					argtypes[i] = INT4MULTIRANGEOID;
+					break;
+				default:
+					break;
+			}
+		}
+	}
+}
+
+/*
+ * delete_function - clean up as much as possible of a stale function cache
+ *
+ * We can't release the CachedFunction struct itself, because of the
+ * possibility that there are fn_extra pointers to it.  We can release
+ * the subsidiary storage, but only if there are no active evaluations
+ * in progress.  Otherwise we'll just leak that storage.  Since the
+ * case would only occur if a pg_proc update is detected during a nested
+ * recursive call on the function, a leak seems acceptable.
+ *
+ * Note that this can be called more than once if there are multiple fn_extra
+ * pointers to the same function cache.  Hence be careful not to do things
+ * twice.
+ */
+static void
+delete_function(CachedFunction *func)
+{
+	/* remove function from hash table (might be done already) */
+	cfunc_hashtable_delete(func);
+
+	/* release the function's storage if safe and not done already */
+	if (func->use_count == 0 &&
+		func->dcallback != NULL)
+	{
+		func->dcallback(func);
+		func->dcallback = NULL;
+	}
+}
+
+/*
+ * Compile a cached function, if no existing cache entry is suitable.
+ *
+ * fcinfo is the current call information.
+ *
+ * function should be NULL or the result of a previous call of
+ * cached_function_compile() for the same fcinfo.  The caller will
+ * typically save the result in fcinfo->flinfo->fn_extra, or in a
+ * field of a struct pointed to by fn_extra, to re-use in later
+ * calls within the same query.
+ *
+ * ccallback and dcallback are function-language-specific callbacks to
+ * compile and delete a cached function entry.  dcallback can be NULL
+ * if there's nothing for it to do.
+ *
+ * cacheEntrySize is the function-language-specific size of the cache entry
+ * (which embeds a CachedFunction struct and typically has many more fields
+ * after that).
+ *
+ * If includeResultType is true and the function returns composite,
+ * include the actual result descriptor in the cache lookup key.
+ *
+ * If forValidator is true, we're only compiling for validation purposes,
+ * and so some checks are skipped.
+ *
+ * Note: it's important for this to fall through quickly if the function
+ * has already been compiled.
+ *
+ * Note: this function leaves the "use_count" field as zero.  The caller
+ * is expected to increment the use_count and decrement it when done with
+ * the cache entry.
+ */
+CachedFunction *
+cached_function_compile(FunctionCallInfo fcinfo,
+						CachedFunction *function,
+						CachedFunctionCompileCallback ccallback,
+						CachedFunctionDeleteCallback dcallback,
+						Size cacheEntrySize,
+						bool includeResultType,
+						bool forValidator)
+{
+	Oid			funcOid = fcinfo->flinfo->fn_oid;
+	HeapTuple	procTup;
+	Form_pg_proc procStruct;
+	CachedFunctionHashKey hashkey;
+	bool		function_valid = false;
+	bool		hashkey_valid = false;
+
+	/*
+	 * Lookup the pg_proc tuple by Oid; we'll need it in any case
+	 */
+	procTup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcOid));
+	if (!HeapTupleIsValid(procTup))
+		elog(ERROR, "cache lookup failed for function %u", funcOid);
+	procStruct = (Form_pg_proc) GETSTRUCT(procTup);
+
+	/*
+	 * Do we already have a cache entry for the current FmgrInfo?  If not, try
+	 * to find one in the hash table.
+	 */
+recheck:
+	if (!function)
+	{
+		/* Compute hashkey using function signature and actual arg types */
+		compute_function_hashkey(fcinfo, procStruct, &hashkey,
+								 cacheEntrySize, includeResultType,
+								 forValidator);
+		hashkey_valid = true;
+
+		/* And do the lookup */
+		function = cfunc_hashtable_lookup(&hashkey);
+	}
+
+	if (function)
+	{
+		/* We have a compiled function, but is it still valid? */
+		if (function->fn_xmin == HeapTupleHeaderGetRawXmin(procTup->t_data) &&
+			ItemPointerEquals(&function->fn_tid, &procTup->t_self))
+			function_valid = true;
+		else
+		{
+			/*
+			 * Nope, so remove it from hashtable and try to drop associated
+			 * storage (if not done already).
+			 */
+			delete_function(function);
+
+			/*
+			 * If the function isn't in active use then we can overwrite the
+			 * func struct with new data, allowing any other existing fn_extra
+			 * pointers to make use of the new definition on their next use.
+			 * If it is in use then just leave it alone and make a new one.
+			 * (The active invocations will run to completion using the
+			 * previous definition, and then the cache entry will just be
+			 * leaked; doesn't seem worth adding code to clean it up, given
+			 * what a corner case this is.)
+			 *
+			 * If we found the function struct via fn_extra then it's possible
+			 * a replacement has already been made, so go back and recheck the
+			 * hashtable.
+			 */
+			if (function->use_count != 0)
+			{
+				function = NULL;
+				if (!hashkey_valid)
+					goto recheck;
+			}
+		}
+	}
+
+	/*
+	 * If the function wasn't found or was out-of-date, we have to compile it.
+	 */
+	if (!function_valid)
+	{
+		/*
+		 * Calculate hashkey if we didn't already; we'll need it to store the
+		 * completed function.
+		 */
+		if (!hashkey_valid)
+			compute_function_hashkey(fcinfo, procStruct, &hashkey,
+									 cacheEntrySize, includeResultType,
+									 forValidator);
+
+		/*
+		 * Create the new function struct, if not done already.  The function
+		 * structs are never thrown away, so keep them in TopMemoryContext.
+		 */
+		Assert(cacheEntrySize >= sizeof(CachedFunction));
+		if (function == NULL)
+		{
+			function = (CachedFunction *)
+				MemoryContextAllocZero(TopMemoryContext, cacheEntrySize);
+		}
+		else
+		{
+			/* re-using a previously existing struct, so clear it out */
+			memset(function, 0, cacheEntrySize);
+		}
+
+		/*
+		 * Fill in the CachedFunction part.  fn_hashkey and use_count remain
+		 * zeroes for now.
+		 */
+		function->fn_xmin = HeapTupleHeaderGetRawXmin(procTup->t_data);
+		function->fn_tid = procTup->t_self;
+		function->dcallback = dcallback;
+
+		/*
+		 * Do the hard, language-specific part.
+		 */
+		ccallback(fcinfo, procTup, &hashkey, function, forValidator);
+
+		/*
+		 * Add the completed struct to the hash table.
+		 */
+		cfunc_hashtable_insert(function, &hashkey);
+	}
+
+	ReleaseSysCache(procTup);
+
+	/*
+	 * Finally return the compiled function
+	 */
+	return function;
+}
diff --git a/src/backend/utils/cache/meson.build b/src/backend/utils/cache/meson.build
index 104b28737d7..a1784dce585 100644
--- a/src/backend/utils/cache/meson.build
+++ b/src/backend/utils/cache/meson.build
@@ -4,6 +4,7 @@ backend_sources += files(
   'attoptcache.c',
   'catcache.c',
   'evtcache.c',
+  'funccache.c',
   'inval.c',
   'lsyscache.c',
   'partcache.c',
diff --git a/src/include/utils/funccache.h b/src/include/utils/funccache.h
new file mode 100644
index 00000000000..e0112ebfa11
--- /dev/null
+++ b/src/include/utils/funccache.h
@@ -0,0 +1,134 @@
+/*-------------------------------------------------------------------------
+ *
+ * funccache.h
+ *	  Function cache definitions.
+ *
+ * See funccache.c for comments.
+ *
+ * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * src/include/utils/funccache.h
+ *
+ *-------------------------------------------------------------------------
+ */
+#ifndef FUNCCACHE_H
+#define FUNCCACHE_H
+
+#include "access/htup_details.h"
+#include "fmgr.h"
+#include "storage/itemptr.h"
+
+struct CachedFunctionHashKey;	/* forward references */
+struct CachedFunction;
+
+/*
+ * Callback that cached_function_compile() invokes when it's necessary to
+ * compile a cached function.  The callback must fill in *function (except
+ * for the fields of struct CachedFunction), or throw an error if trouble.
+ *	fcinfo: current call information
+ *	procTup: function's pg_proc row from catcache
+ *	hashkey: hash key that will be used for the function
+ *	function: pre-zeroed workspace, of size passed to cached_function_compile()
+ *	forValidator: passed through from cached_function_compile()
+ */
+typedef void (*CachedFunctionCompileCallback) (FunctionCallInfo fcinfo,
+											   HeapTuple procTup,
+											   const struct CachedFunctionHashKey *hashkey,
+											   struct CachedFunction *function,
+											   bool forValidator);
+
+/*
+ * Callback called when discarding a cache entry.  Free any free-able
+ * subsidiary data of cfunc, but not the struct CachedFunction itself.
+ */
+typedef void (*CachedFunctionDeleteCallback) (struct CachedFunction *cfunc);
+
+/*
+ * Hash lookup key for functions.  This must account for all aspects
+ * of a specific call that might lead to different data types or
+ * collations being used within the function.
+ */
+typedef struct CachedFunctionHashKey
+{
+	Oid			funcOid;
+
+	bool		isTrigger;		/* true if called as a DML trigger */
+	bool		isEventTrigger; /* true if called as an event trigger */
+
+	/* be careful that pad bytes in this struct get zeroed! */
+
+	/*
+	 * We include the language-specific size of the function's cache entry in
+	 * the cache key.  This covers the case where CREATE OR REPLACE FUNCTION
+	 * is used to change the implementation language, and the new language
+	 * also uses funccache.c but needs a different-sized cache entry.
+	 */
+	Size		cacheEntrySize;
+
+	/*
+	 * For a trigger function, the OID of the trigger is part of the hash key
+	 * --- we want to compile the trigger function separately for each trigger
+	 * it is used with, in case the rowtype or transition table names are
+	 * different.  Zero if not called as a DML trigger.
+	 */
+	Oid			trigOid;
+
+	/*
+	 * We must include the input collation as part of the hash key too,
+	 * because we have to generate different plans (with different Param
+	 * collations) for different collation settings.
+	 */
+	Oid			inputCollation;
+
+	/* Number of arguments (counting input arguments only, ie pronargs) */
+	int			nargs;
+
+	/* If you change anything below here, fix hashing code in funccache.c! */
+
+	/*
+	 * If relevant, the result descriptor for a function returning composite.
+	 */
+	TupleDesc	callResultType;
+
+	/*
+	 * Input argument types, with any polymorphic types resolved to actual
+	 * types.  Only the first nargs entries are valid.
+	 */
+	Oid			argtypes[FUNC_MAX_ARGS];
+} CachedFunctionHashKey;
+
+/*
+ * Representation of a compiled function.  This struct contains just the
+ * fields that funccache.c needs to deal with.  It will typically be
+ * embedded in a larger struct containing function-language-specific data.
+ */
+typedef struct CachedFunction
+{
+	/* back-link to hashtable entry, or NULL if not in hash table */
+	CachedFunctionHashKey *fn_hashkey;
+	/* xmin and ctid of function's pg_proc row; used to detect invalidation */
+	TransactionId fn_xmin;
+	ItemPointerData fn_tid;
+	/* deletion callback */
+	CachedFunctionDeleteCallback dcallback;
+
+	/* this field changes when the function is used: */
+	uint64		use_count;
+} CachedFunction;
+
+extern CachedFunction *cached_function_compile(FunctionCallInfo fcinfo,
+											   CachedFunction *function,
+											   CachedFunctionCompileCallback ccallback,
+											   CachedFunctionDeleteCallback dcallback,
+											   Size cacheEntrySize,
+											   bool includeResultType,
+											   bool forValidator);
+extern void cfunc_resolve_polymorphic_argtypes(int numargs,
+											   Oid *argtypes,
+											   char *argmodes,
+											   Node *call_expr,
+											   bool forValidator,
+											   const char *proname);
+
+#endif							/* FUNCCACHE_H */
diff --git a/src/pl/plpgsql/src/pl_comp.c b/src/pl/plpgsql/src/pl_comp.c
index 6fdba95962d..1a091d0c55f 100644
--- a/src/pl/plpgsql/src/pl_comp.c
+++ b/src/pl/plpgsql/src/pl_comp.c
@@ -52,20 +52,6 @@ PLpgSQL_function *plpgsql_curr_compile;
 /* A context appropriate for short-term allocs during compilation */
 MemoryContext plpgsql_compile_tmp_cxt;
 
-/* ----------
- * Hash table for compiled functions
- * ----------
- */
-static HTAB *plpgsql_HashTable = NULL;
-
-typedef struct plpgsql_hashent
-{
-	PLpgSQL_func_hashkey key;
-	PLpgSQL_function *function;
-} plpgsql_HashEnt;
-
-#define FUNCS_PER_USER		128 /* initial table size */
-
 /* ----------
  * Lookup table for EXCEPTION condition names
  * ----------
@@ -86,11 +72,11 @@ static const ExceptionLabelMap exception_label_map[] = {
  * static prototypes
  * ----------
  */
-static PLpgSQL_function *do_compile(FunctionCallInfo fcinfo,
-									HeapTuple procTup,
-									PLpgSQL_function *function,
-									PLpgSQL_func_hashkey *hashkey,
-									bool forValidator);
+static void plpgsql_compile_callback(FunctionCallInfo fcinfo,
+									 HeapTuple procTup,
+									 const CachedFunctionHashKey *hashkey,
+									 CachedFunction *cfunc,
+									 bool forValidator);
 static void plpgsql_compile_error_callback(void *arg);
 static void add_parameter_name(PLpgSQL_nsitem_type itemtype, int itemno, const char *name);
 static void add_dummy_return(PLpgSQL_function *function);
@@ -105,19 +91,6 @@ static PLpgSQL_type *build_datatype(HeapTuple typeTup, int32 typmod,
 									Oid collation, TypeName *origtypname);
 static void plpgsql_start_datums(void);
 static void plpgsql_finish_datums(PLpgSQL_function *function);
-static void compute_function_hashkey(FunctionCallInfo fcinfo,
-									 Form_pg_proc procStruct,
-									 PLpgSQL_func_hashkey *hashkey,
-									 bool forValidator);
-static void plpgsql_resolve_polymorphic_argtypes(int numargs,
-												 Oid *argtypes, char *argmodes,
-												 Node *call_expr, bool forValidator,
-												 const char *proname);
-static PLpgSQL_function *plpgsql_HashTableLookup(PLpgSQL_func_hashkey *func_key);
-static void plpgsql_HashTableInsert(PLpgSQL_function *function,
-									PLpgSQL_func_hashkey *func_key);
-static void plpgsql_HashTableDelete(PLpgSQL_function *function);
-static void delete_function(PLpgSQL_function *func);
 
 /* ----------
  * plpgsql_compile		Make an execution tree for a PL/pgSQL function.
@@ -132,97 +105,24 @@ static void delete_function(PLpgSQL_function *func);
 PLpgSQL_function *
 plpgsql_compile(FunctionCallInfo fcinfo, bool forValidator)
 {
-	Oid			funcOid = fcinfo->flinfo->fn_oid;
-	HeapTuple	procTup;
-	Form_pg_proc procStruct;
 	PLpgSQL_function *function;
-	PLpgSQL_func_hashkey hashkey;
-	bool		function_valid = false;
-	bool		hashkey_valid = false;
-
-	/*
-	 * Lookup the pg_proc tuple by Oid; we'll need it in any case
-	 */
-	procTup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcOid));
-	if (!HeapTupleIsValid(procTup))
-		elog(ERROR, "cache lookup failed for function %u", funcOid);
-	procStruct = (Form_pg_proc) GETSTRUCT(procTup);
-
-	/*
-	 * See if there's already a cache entry for the current FmgrInfo. If not,
-	 * try to find one in the hash table.
-	 */
-	function = (PLpgSQL_function *) fcinfo->flinfo->fn_extra;
-
-recheck:
-	if (!function)
-	{
-		/* Compute hashkey using function signature and actual arg types */
-		compute_function_hashkey(fcinfo, procStruct, &hashkey, forValidator);
-		hashkey_valid = true;
-
-		/* And do the lookup */
-		function = plpgsql_HashTableLookup(&hashkey);
-	}
-
-	if (function)
-	{
-		/* We have a compiled function, but is it still valid? */
-		if (function->fn_xmin == HeapTupleHeaderGetRawXmin(procTup->t_data) &&
-			ItemPointerEquals(&function->fn_tid, &procTup->t_self))
-			function_valid = true;
-		else
-		{
-			/*
-			 * Nope, so remove it from hashtable and try to drop associated
-			 * storage (if not done already).
-			 */
-			delete_function(function);
-
-			/*
-			 * If the function isn't in active use then we can overwrite the
-			 * func struct with new data, allowing any other existing fn_extra
-			 * pointers to make use of the new definition on their next use.
-			 * If it is in use then just leave it alone and make a new one.
-			 * (The active invocations will run to completion using the
-			 * previous definition, and then the cache entry will just be
-			 * leaked; doesn't seem worth adding code to clean it up, given
-			 * what a corner case this is.)
-			 *
-			 * If we found the function struct via fn_extra then it's possible
-			 * a replacement has already been made, so go back and recheck the
-			 * hashtable.
-			 */
-			if (function->use_count != 0)
-			{
-				function = NULL;
-				if (!hashkey_valid)
-					goto recheck;
-			}
-		}
-	}
 
 	/*
-	 * If the function wasn't found or was out-of-date, we have to compile it
+	 * funccache.c manages re-use of existing PLpgSQL_function caches.
+	 *
+	 * In PL/pgSQL we use fn_extra directly as the pointer to the long-lived
+	 * function cache entry; we have no need for any query-lifespan cache.
+	 * Also, we don't need to make the cache key depend on composite result
+	 * type (at least for now).
 	 */
-	if (!function_valid)
-	{
-		/*
-		 * Calculate hashkey if we didn't already; we'll need it to store the
-		 * completed function.
-		 */
-		if (!hashkey_valid)
-			compute_function_hashkey(fcinfo, procStruct, &hashkey,
-									 forValidator);
-
-		/*
-		 * Do the hard part.
-		 */
-		function = do_compile(fcinfo, procTup, function,
-							  &hashkey, forValidator);
-	}
-
-	ReleaseSysCache(procTup);
+	function = (PLpgSQL_function *)
+		cached_function_compile(fcinfo,
+								fcinfo->flinfo->fn_extra,
+								plpgsql_compile_callback,
+								plpgsql_delete_callback,
+								sizeof(PLpgSQL_function),
+								false,
+								forValidator);
 
 	/*
 	 * Save pointer in FmgrInfo to avoid search on subsequent calls
@@ -244,8 +144,8 @@ struct compile_error_callback_arg
 /*
  * This is the slow part of plpgsql_compile().
  *
- * The passed-in "function" pointer is either NULL or an already-allocated
- * function struct to overwrite.
+ * The passed-in "cfunc" struct is expected to be zeroes, except
+ * for the CachedFunction fields, which we don't touch here.
  *
  * While compiling a function, the CurrentMemoryContext is the
  * per-function memory context of the function we are compiling. That
@@ -263,13 +163,14 @@ struct compile_error_callback_arg
  * NB: this code is not re-entrant.  We assume that nothing we do here could
  * result in the invocation of another plpgsql function.
  */
-static PLpgSQL_function *
-do_compile(FunctionCallInfo fcinfo,
-		   HeapTuple procTup,
-		   PLpgSQL_function *function,
-		   PLpgSQL_func_hashkey *hashkey,
-		   bool forValidator)
+static void
+plpgsql_compile_callback(FunctionCallInfo fcinfo,
+						 HeapTuple procTup,
+						 const CachedFunctionHashKey *hashkey,
+						 CachedFunction *cfunc,
+						 bool forValidator)
 {
+	PLpgSQL_function *function = (PLpgSQL_function *) cfunc;
 	Form_pg_proc procStruct = (Form_pg_proc) GETSTRUCT(procTup);
 	bool		is_dml_trigger = CALLED_AS_TRIGGER(fcinfo);
 	bool		is_event_trigger = CALLED_AS_EVENT_TRIGGER(fcinfo);
@@ -320,21 +221,6 @@ do_compile(FunctionCallInfo fcinfo,
 	 * reasons.
 	 */
 	plpgsql_check_syntax = forValidator;
-
-	/*
-	 * Create the new function struct, if not done already.  The function
-	 * structs are never thrown away, so keep them in TopMemoryContext.
-	 */
-	if (function == NULL)
-	{
-		function = (PLpgSQL_function *)
-			MemoryContextAllocZero(TopMemoryContext, sizeof(PLpgSQL_function));
-	}
-	else
-	{
-		/* re-using a previously existing struct, so clear it out */
-		memset(function, 0, sizeof(PLpgSQL_function));
-	}
 	plpgsql_curr_compile = function;
 
 	/*
@@ -349,8 +235,6 @@ do_compile(FunctionCallInfo fcinfo,
 	function->fn_signature = format_procedure(fcinfo->flinfo->fn_oid);
 	MemoryContextSetIdentifier(func_cxt, function->fn_signature);
 	function->fn_oid = fcinfo->flinfo->fn_oid;
-	function->fn_xmin = HeapTupleHeaderGetRawXmin(procTup->t_data);
-	function->fn_tid = procTup->t_self;
 	function->fn_input_collation = fcinfo->fncollation;
 	function->fn_cxt = func_cxt;
 	function->out_param_varno = -1; /* set up for no OUT param */
@@ -400,10 +284,14 @@ do_compile(FunctionCallInfo fcinfo,
 			numargs = get_func_arg_info(procTup,
 										&argtypes, &argnames, &argmodes);
 
-			plpgsql_resolve_polymorphic_argtypes(numargs, argtypes, argmodes,
-												 fcinfo->flinfo->fn_expr,
-												 forValidator,
-												 plpgsql_error_funcname);
+			/*
+			 * XXX can't we get rid of this in favor of using funccache.c's
+			 * results?  But why are we considering argmodes here not there??
+			 */
+			cfunc_resolve_polymorphic_argtypes(numargs, argtypes, argmodes,
+											   fcinfo->flinfo->fn_expr,
+											   forValidator,
+											   plpgsql_error_funcname);
 
 			in_arg_varnos = (int *) palloc(numargs * sizeof(int));
 			out_arg_variables = (PLpgSQL_variable **) palloc(numargs * sizeof(PLpgSQL_variable *));
@@ -819,11 +707,6 @@ do_compile(FunctionCallInfo fcinfo,
 	if (plpgsql_DumpExecTree)
 		plpgsql_dumptree(function);
 
-	/*
-	 * add it to the hash table
-	 */
-	plpgsql_HashTableInsert(function, hashkey);
-
 	/*
 	 * Pop the error context stack
 	 */
@@ -834,14 +717,13 @@ do_compile(FunctionCallInfo fcinfo,
 
 	MemoryContextSwitchTo(plpgsql_compile_tmp_cxt);
 	plpgsql_compile_tmp_cxt = NULL;
-	return function;
 }
 
 /* ----------
  * plpgsql_compile_inline	Make an execution tree for an anonymous code block.
  *
- * Note: this is generally parallel to do_compile(); is it worth trying to
- * merge the two?
+ * Note: this is generally parallel to plpgsql_compile_callback(); is it worth
+ * trying to merge the two?
  *
  * Note: we assume the block will be thrown away so there is no need to build
  * persistent data structures.
@@ -2437,242 +2319,3 @@ plpgsql_add_initdatums(int **varnos)
 	datums_last = plpgsql_nDatums;
 	return n;
 }
-
-
-/*
- * Compute the hashkey for a given function invocation
- *
- * The hashkey is returned into the caller-provided storage at *hashkey.
- */
-static void
-compute_function_hashkey(FunctionCallInfo fcinfo,
-						 Form_pg_proc procStruct,
-						 PLpgSQL_func_hashkey *hashkey,
-						 bool forValidator)
-{
-	/* Make sure any unused bytes of the struct are zero */
-	MemSet(hashkey, 0, sizeof(PLpgSQL_func_hashkey));
-
-	/* get function OID */
-	hashkey->funcOid = fcinfo->flinfo->fn_oid;
-
-	/* get call context */
-	hashkey->isTrigger = CALLED_AS_TRIGGER(fcinfo);
-	hashkey->isEventTrigger = CALLED_AS_EVENT_TRIGGER(fcinfo);
-
-	/*
-	 * If DML trigger, include trigger's OID in the hash, so that each trigger
-	 * usage gets a different hash entry, allowing for e.g. different relation
-	 * rowtypes or transition table names.  In validation mode we do not know
-	 * what relation or transition table names are intended to be used, so we
-	 * leave trigOid zero; the hash entry built in this case will never be
-	 * used for any actual calls.
-	 *
-	 * We don't currently need to distinguish different event trigger usages
-	 * in the same way, since the special parameter variables don't vary in
-	 * type in that case.
-	 */
-	if (hashkey->isTrigger && !forValidator)
-	{
-		TriggerData *trigdata = (TriggerData *) fcinfo->context;
-
-		hashkey->trigOid = trigdata->tg_trigger->tgoid;
-	}
-
-	/* get input collation, if known */
-	hashkey->inputCollation = fcinfo->fncollation;
-
-	if (procStruct->pronargs > 0)
-	{
-		/* get the argument types */
-		memcpy(hashkey->argtypes, procStruct->proargtypes.values,
-			   procStruct->pronargs * sizeof(Oid));
-
-		/* resolve any polymorphic argument types */
-		plpgsql_resolve_polymorphic_argtypes(procStruct->pronargs,
-											 hashkey->argtypes,
-											 NULL,
-											 fcinfo->flinfo->fn_expr,
-											 forValidator,
-											 NameStr(procStruct->proname));
-	}
-}
-
-/*
- * This is the same as the standard resolve_polymorphic_argtypes() function,
- * except that:
- * 1. We go ahead and report the error if we can't resolve the types.
- * 2. We treat RECORD-type input arguments (not output arguments) as if
- *    they were polymorphic, replacing their types with the actual input
- *    types if we can determine those.  This allows us to create a separate
- *    function cache entry for each named composite type passed to such an
- *    argument.
- * 3. In validation mode, we have no inputs to look at, so assume that
- *    polymorphic arguments are integer, integer-array or integer-range.
- */
-static void
-plpgsql_resolve_polymorphic_argtypes(int numargs,
-									 Oid *argtypes, char *argmodes,
-									 Node *call_expr, bool forValidator,
-									 const char *proname)
-{
-	int			i;
-
-	if (!forValidator)
-	{
-		int			inargno;
-
-		/* normal case, pass to standard routine */
-		if (!resolve_polymorphic_argtypes(numargs, argtypes, argmodes,
-										  call_expr))
-			ereport(ERROR,
-					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
-					 errmsg("could not determine actual argument "
-							"type for polymorphic function \"%s\"",
-							proname)));
-		/* also, treat RECORD inputs (but not outputs) as polymorphic */
-		inargno = 0;
-		for (i = 0; i < numargs; i++)
-		{
-			char		argmode = argmodes ? argmodes[i] : PROARGMODE_IN;
-
-			if (argmode == PROARGMODE_OUT || argmode == PROARGMODE_TABLE)
-				continue;
-			if (argtypes[i] == RECORDOID || argtypes[i] == RECORDARRAYOID)
-			{
-				Oid			resolvedtype = get_call_expr_argtype(call_expr,
-																 inargno);
-
-				if (OidIsValid(resolvedtype))
-					argtypes[i] = resolvedtype;
-			}
-			inargno++;
-		}
-	}
-	else
-	{
-		/* special validation case (no need to do anything for RECORD) */
-		for (i = 0; i < numargs; i++)
-		{
-			switch (argtypes[i])
-			{
-				case ANYELEMENTOID:
-				case ANYNONARRAYOID:
-				case ANYENUMOID:	/* XXX dubious */
-				case ANYCOMPATIBLEOID:
-				case ANYCOMPATIBLENONARRAYOID:
-					argtypes[i] = INT4OID;
-					break;
-				case ANYARRAYOID:
-				case ANYCOMPATIBLEARRAYOID:
-					argtypes[i] = INT4ARRAYOID;
-					break;
-				case ANYRANGEOID:
-				case ANYCOMPATIBLERANGEOID:
-					argtypes[i] = INT4RANGEOID;
-					break;
-				case ANYMULTIRANGEOID:
-					argtypes[i] = INT4MULTIRANGEOID;
-					break;
-				default:
-					break;
-			}
-		}
-	}
-}
-
-/*
- * delete_function - clean up as much as possible of a stale function cache
- *
- * We can't release the PLpgSQL_function struct itself, because of the
- * possibility that there are fn_extra pointers to it.  We can release
- * the subsidiary storage, but only if there are no active evaluations
- * in progress.  Otherwise we'll just leak that storage.  Since the
- * case would only occur if a pg_proc update is detected during a nested
- * recursive call on the function, a leak seems acceptable.
- *
- * Note that this can be called more than once if there are multiple fn_extra
- * pointers to the same function cache.  Hence be careful not to do things
- * twice.
- */
-static void
-delete_function(PLpgSQL_function *func)
-{
-	/* remove function from hash table (might be done already) */
-	plpgsql_HashTableDelete(func);
-
-	/* release the function's storage if safe and not done already */
-	if (func->use_count == 0)
-		plpgsql_free_function_memory(func);
-}
-
-/* exported so we can call it from _PG_init() */
-void
-plpgsql_HashTableInit(void)
-{
-	HASHCTL		ctl;
-
-	/* don't allow double-initialization */
-	Assert(plpgsql_HashTable == NULL);
-
-	ctl.keysize = sizeof(PLpgSQL_func_hashkey);
-	ctl.entrysize = sizeof(plpgsql_HashEnt);
-	plpgsql_HashTable = hash_create("PLpgSQL function hash",
-									FUNCS_PER_USER,
-									&ctl,
-									HASH_ELEM | HASH_BLOBS);
-}
-
-static PLpgSQL_function *
-plpgsql_HashTableLookup(PLpgSQL_func_hashkey *func_key)
-{
-	plpgsql_HashEnt *hentry;
-
-	hentry = (plpgsql_HashEnt *) hash_search(plpgsql_HashTable,
-											 func_key,
-											 HASH_FIND,
-											 NULL);
-	if (hentry)
-		return hentry->function;
-	else
-		return NULL;
-}
-
-static void
-plpgsql_HashTableInsert(PLpgSQL_function *function,
-						PLpgSQL_func_hashkey *func_key)
-{
-	plpgsql_HashEnt *hentry;
-	bool		found;
-
-	hentry = (plpgsql_HashEnt *) hash_search(plpgsql_HashTable,
-											 func_key,
-											 HASH_ENTER,
-											 &found);
-	if (found)
-		elog(WARNING, "trying to insert a function that already exists");
-
-	hentry->function = function;
-	/* prepare back link from function to hashtable key */
-	function->fn_hashkey = &hentry->key;
-}
-
-static void
-plpgsql_HashTableDelete(PLpgSQL_function *function)
-{
-	plpgsql_HashEnt *hentry;
-
-	/* do nothing if not in table */
-	if (function->fn_hashkey == NULL)
-		return;
-
-	hentry = (plpgsql_HashEnt *) hash_search(plpgsql_HashTable,
-											 function->fn_hashkey,
-											 HASH_REMOVE,
-											 NULL);
-	if (hentry == NULL)
-		elog(WARNING, "trying to delete function that does not exist");
-
-	/* remove back link, which no longer points to allocated storage */
-	function->fn_hashkey = NULL;
-}
diff --git a/src/pl/plpgsql/src/pl_funcs.c b/src/pl/plpgsql/src/pl_funcs.c
index 6b5394fc5fa..bc7a61feb4d 100644
--- a/src/pl/plpgsql/src/pl_funcs.c
+++ b/src/pl/plpgsql/src/pl_funcs.c
@@ -718,7 +718,7 @@ plpgsql_free_function_memory(PLpgSQL_function *func)
 	int			i;
 
 	/* Better not call this on an in-use function */
-	Assert(func->use_count == 0);
+	Assert(func->cfunc.use_count == 0);
 
 	/* Release plans associated with variable declarations */
 	for (i = 0; i < func->ndatums; i++)
@@ -767,6 +767,13 @@ plpgsql_free_function_memory(PLpgSQL_function *func)
 	func->fn_cxt = NULL;
 }
 
+/* Deletion callback used by funccache.c */
+void
+plpgsql_delete_callback(CachedFunction *cfunc)
+{
+	plpgsql_free_function_memory((PLpgSQL_function *) cfunc);
+}
+
 
 /**********************************************************************
  * Debug functions for analyzing the compiled code
diff --git a/src/pl/plpgsql/src/pl_handler.c b/src/pl/plpgsql/src/pl_handler.c
index 1bf12232862..e9a72929947 100644
--- a/src/pl/plpgsql/src/pl_handler.c
+++ b/src/pl/plpgsql/src/pl_handler.c
@@ -202,7 +202,6 @@ _PG_init(void)
 
 	MarkGUCPrefixReserved("plpgsql");
 
-	plpgsql_HashTableInit();
 	RegisterXactCallback(plpgsql_xact_cb, NULL);
 	RegisterSubXactCallback(plpgsql_subxact_cb, NULL);
 
@@ -247,7 +246,7 @@ plpgsql_call_handler(PG_FUNCTION_ARGS)
 	save_cur_estate = func->cur_estate;
 
 	/* Mark the function as busy, so it can't be deleted from under us */
-	func->use_count++;
+	func->cfunc.use_count++;
 
 	/*
 	 * If we'll need a procedure-lifespan resowner to execute any CALL or DO
@@ -284,7 +283,7 @@ plpgsql_call_handler(PG_FUNCTION_ARGS)
 	PG_FINALLY();
 	{
 		/* Decrement use-count, restore cur_estate */
-		func->use_count--;
+		func->cfunc.use_count--;
 		func->cur_estate = save_cur_estate;
 
 		/* Be sure to release the procedure resowner if any */
@@ -334,7 +333,7 @@ plpgsql_inline_handler(PG_FUNCTION_ARGS)
 	func = plpgsql_compile_inline(codeblock->source_text);
 
 	/* Mark the function as busy, just pro forma */
-	func->use_count++;
+	func->cfunc.use_count++;
 
 	/*
 	 * Set up a fake fcinfo with just enough info to satisfy
@@ -398,8 +397,8 @@ plpgsql_inline_handler(PG_FUNCTION_ARGS)
 		ResourceOwnerDelete(simple_eval_resowner);
 
 		/* Function should now have no remaining use-counts ... */
-		func->use_count--;
-		Assert(func->use_count == 0);
+		func->cfunc.use_count--;
+		Assert(func->cfunc.use_count == 0);
 
 		/* ... so we can free subsidiary storage */
 		plpgsql_free_function_memory(func);
@@ -415,8 +414,8 @@ plpgsql_inline_handler(PG_FUNCTION_ARGS)
 	ResourceOwnerDelete(simple_eval_resowner);
 
 	/* Function should now have no remaining use-counts ... */
-	func->use_count--;
-	Assert(func->use_count == 0);
+	func->cfunc.use_count--;
+	Assert(func->cfunc.use_count == 0);
 
 	/* ... so we can free subsidiary storage */
 	plpgsql_free_function_memory(func);
diff --git a/src/pl/plpgsql/src/plpgsql.h b/src/pl/plpgsql/src/plpgsql.h
index b67847b5111..41e52b8ce71 100644
--- a/src/pl/plpgsql/src/plpgsql.h
+++ b/src/pl/plpgsql/src/plpgsql.h
@@ -21,6 +21,7 @@
 #include "commands/trigger.h"
 #include "executor/spi.h"
 #include "utils/expandedrecord.h"
+#include "utils/funccache.h"
 #include "utils/typcache.h"
 
 
@@ -941,40 +942,6 @@ typedef struct PLpgSQL_stmt_dynexecute
 	List	   *params;			/* USING expressions */
 } PLpgSQL_stmt_dynexecute;
 
-/*
- * Hash lookup key for functions
- */
-typedef struct PLpgSQL_func_hashkey
-{
-	Oid			funcOid;
-
-	bool		isTrigger;		/* true if called as a DML trigger */
-	bool		isEventTrigger; /* true if called as an event trigger */
-
-	/* be careful that pad bytes in this struct get zeroed! */
-
-	/*
-	 * For a trigger function, the OID of the trigger is part of the hash key
-	 * --- we want to compile the trigger function separately for each trigger
-	 * it is used with, in case the rowtype or transition table names are
-	 * different.  Zero if not called as a DML trigger.
-	 */
-	Oid			trigOid;
-
-	/*
-	 * We must include the input collation as part of the hash key too,
-	 * because we have to generate different plans (with different Param
-	 * collations) for different collation settings.
-	 */
-	Oid			inputCollation;
-
-	/*
-	 * We include actual argument types in the hash key to support polymorphic
-	 * PLpgSQL functions.  Be careful that extra positions are zeroed!
-	 */
-	Oid			argtypes[FUNC_MAX_ARGS];
-} PLpgSQL_func_hashkey;
-
 /*
  * Trigger type
  */
@@ -990,13 +957,12 @@ typedef enum PLpgSQL_trigtype
  */
 typedef struct PLpgSQL_function
 {
+	CachedFunction cfunc;		/* fields managed by funccache.c */
+
 	char	   *fn_signature;
 	Oid			fn_oid;
-	TransactionId fn_xmin;
-	ItemPointerData fn_tid;
 	PLpgSQL_trigtype fn_is_trigger;
 	Oid			fn_input_collation;
-	PLpgSQL_func_hashkey *fn_hashkey;	/* back-link to hashtable key */
 	MemoryContext fn_cxt;
 
 	Oid			fn_rettype;
@@ -1036,9 +1002,8 @@ typedef struct PLpgSQL_function
 	bool		requires_procedure_resowner;	/* contains CALL or DO? */
 	bool		has_exception_block;	/* contains BEGIN...EXCEPTION? */
 
-	/* these fields change when the function is used */
+	/* this field changes when the function is used */
 	struct PLpgSQL_execstate *cur_estate;
-	unsigned long use_count;
 } PLpgSQL_function;
 
 /*
@@ -1287,7 +1252,6 @@ extern PGDLLEXPORT int plpgsql_recognize_err_condition(const char *condname,
 extern PLpgSQL_condition *plpgsql_parse_err_condition(char *condname);
 extern void plpgsql_adddatum(PLpgSQL_datum *newdatum);
 extern int	plpgsql_add_initdatums(int **varnos);
-extern void plpgsql_HashTableInit(void);
 
 /*
  * Functions in pl_exec.c
@@ -1335,6 +1299,7 @@ extern PGDLLEXPORT const char *plpgsql_stmt_typename(PLpgSQL_stmt *stmt);
 extern const char *plpgsql_getdiag_kindname(PLpgSQL_getdiag_kind kind);
 extern void plpgsql_mark_local_assignment_targets(PLpgSQL_function *func);
 extern void plpgsql_free_function_memory(PLpgSQL_function *func);
+extern void plpgsql_delete_callback(CachedFunction *cfunc);
 extern void plpgsql_dumptree(PLpgSQL_function *func);
 
 /*
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index ff75a508876..144c4e9662c 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -381,6 +381,11 @@ CURLM
 CURLoption
 CV
 CachedExpression
+CachedFunction
+CachedFunctionCompileCallback
+CachedFunctionDeleteCallback
+CachedFunctionHashEntry
+CachedFunctionHashKey
 CachedPlan
 CachedPlanSource
 CallContext
-- 
2.43.5

