aboutsummaryrefslogtreecommitdiff
path: root/src/include/utils/funccache.h
blob: e0112ebfa11de389b7c073e021d6aed318e1e58a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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 */