Skip to content

Commit 29452de

Browse files
committed
Doc: flesh out fmgr/README's description of context-node usage.
I wrote this to provide a home for a planned discussion of error return conventions for non-error-throwing functions. But it seems useful as documentation of existing code no matter what becomes of that proposal, so commit separately.
1 parent 2605643 commit 29452de

File tree

1 file changed

+37
-5
lines changed

1 file changed

+37
-5
lines changed

src/backend/utils/fmgr/README

+37-5
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,7 @@ to find out the OID of the specific function being called.
7878
context is NULL for an "ordinary" function call, but may point to additional
7979
info when the function is called in certain contexts. (For example, the
8080
trigger manager will pass information about the current trigger event here.)
81-
If context is used, it should point to some subtype of Node; the particular
82-
kind of context is indicated by the node type field. (A callee should
83-
always check the node type before assuming it knows what kind of context is
84-
being passed.) fmgr itself puts no other restrictions on the use of this
85-
field.
81+
Further details appear in "Function Call Contexts" below.
8682

8783
resultinfo is NULL when calling any function from which a simple Datum
8884
result is expected. It may point to some subtype of Node if the function
@@ -236,6 +232,42 @@ context. When and if the value is actually stored into a tuple, the
236232
tuple toaster will decide whether toasting is needed.
237233

238234

235+
Function Call Contexts
236+
----------------------
237+
238+
If a caller passes a non-NULL pointer in fcinfo->context, it should point
239+
to some subtype of Node; the particular kind of context is indicated by the
240+
node type field. (A callee should always check the node type, via IsA(),
241+
before assuming it knows what kind of context is being passed.) fmgr
242+
itself puts no other restrictions on the use of this field.
243+
244+
Current uses of this convention include:
245+
246+
* Trigger functions are passed an instance of struct TriggerData,
247+
containing information about the trigger context. (The trigger function
248+
does not receive any normal arguments.) See commands/trigger.h for
249+
more information and macros that are commonly used by trigger functions.
250+
251+
* Aggregate functions (or to be precise, their transition and final
252+
functions) are passed an instance of struct AggState, that is the executor
253+
state node for the calling Agg plan node; or if they are called as window
254+
functions, they receive an instance of struct WindowAggState. It is
255+
recommended that these pointers be used only via AggCheckCallContext()
256+
and sibling functions, which are declared in fmgr.h but are documented
257+
only with their source code in src/backend/executor/nodeAgg.c. Typically
258+
these context nodes are only of interest when the transition and final
259+
functions wish to optimize execution based on knowing that they are being
260+
used within an aggregate rather than as standalone SQL functions.
261+
262+
* True window functions receive an instance of struct WindowObject.
263+
(Like trigger functions, they don't receive any normal arguments.)
264+
See windowapi.h for more information.
265+
266+
* Procedures are passed an instance of struct CallContext, containing
267+
information about the context of the CALL statement, particularly
268+
whether it is within an "atomic" execution context.
269+
270+
239271
Functions Accepting or Returning Sets
240272
-------------------------------------
241273

0 commit comments

Comments
 (0)