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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*--------------------------------------------------------------------
* Symbols referenced in this file:
* - cfunc_resolve_polymorphic_argtypes
*--------------------------------------------------------------------
*/
/*-------------------------------------------------------------------------
*
* 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
*
*-------------------------------------------------------------------------
*/
/*
* Hash table for cached functions
*/
typedef struct CachedFunctionHashEntry
CachedFunctionHashEntry;
static uint32 ;
static int ;
/*
* Initialize the hash table on first use.
*
* The hash table will be in TopMemoryContext regardless of caller's context.
*/
/*
* 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.
*/
/*
* cfunc_match: match function to use with cfunc_hash
*/
/*
* Look up the CachedFunction for the given hash key.
* Returns NULL if not present.
*/
/*
* Insert a hash table entry.
*/
/*
* Delete a hash table entry.
*/
/*
* 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.
*/
/*
* 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.
*/
/*
* libpg_query never has a real fn_expr to consult, and only invokes the
* plpgsql compile path in validator mode. Implement the validator branch
* of upstream's cfunc_resolve_polymorphic_argtypes (mapping polymorphic
* types to INT4OID family) and bail out for anything else.
*/
void
/*
* 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.
*/
/*
* 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.
*/