Stack-size considerations (Ext)
The database server allocates local storage in external routines from shared memory. This local storage is called the thread stack. The stack has a fixed length. Therefore, an external routine must not consume excessive stack space, either through large local-variable declarations or through excessively long call chains or recursion.
In addition, any nonstack storage that a thread allocates must be in shared memory. Otherwise, the memory is not visible when the thread moves from one VP to another.
The routine manager of the database server verifies that a large stack region is available to a thread before it calls a user-defined function, so stack exhaustion is generally not a problem.
For C UDRs, you can dynamically allocate stack space. In addition, the DataBlade® API provides memory-management routines that allocate space from shared memory rather than from process-private memory. If you use the DataBlade API, memory visibility is not a problem.
By default, the thread allocates a stack with the size that the STACKSIZE configuration parameter specifies. If the STACKSIZE configuration parameter is not set, the routine manager uses a default stack size of 32 KB. If less than 32 KB of stack space remains, the routine manager allocates the remaining stack space. To determine how much stack space a UDR requires, monitor the UDR with the onstat -g sts command.
- The thread ID
- The maximum stack size that is configured for each thread
- The maximum stack size that the thread uses
You can use the output of the threads that belong to user sessions to determine whether you need to alter the maximum stack size for a user session. To alter the maximum stack size for all user sessions, change the value of the STACKSIZE configuration parameter. To alter the maximum stack size for a single user session, change the value of the ONEDB_ STACKSIZE environment variable.
If the default stack size is not sufficient for your UDR, you can specify a stack size with the STACK routine modifier in the WITH clause of the CREATE FUNCTION or CREATE PROCEDURE statement. While a UDR that includes the STACK modifier runs, the database server allocates a thread-stack size of the specified number of bytes. Subsequent UDRs run in threads with a stack size that the STACKSIZE configuration parameter specifies, unless any subsequent UDRs specify the STACK modifier. If a routine does not need a larger stack, do not specify a stack size.