07.06.2023 15:53, Robert Haas wrote:
> Right now, if you need a bit
> of additional session-local state, you just declare a variable and
> you're all set. That's not a perfect system and does cause some
> problems, but we can't go from there to a system where it's impossible
> to add session-local state without hacking core.
> or else it needs to
> be design in some kind of extensible way that doesn't require it to
> know the full details of every sort of object that's being used as
> session-local state anywhere in the system.
And it is quite possible. Although with indirection involved.
For example, we want to add session variable "my_hello_var".
We first need to declare "offset variable".
Then register it in a session.
And then use function and/or macros to get actual address:
/* session.h */
extern size_t RegisterSessionVar(size_t size);
extern void* CurSessionVar(size_t offset);
/* session.c */
typedef struct Session {
char *vars;
} Session;
static _Thread_local Session* curSession;
static size_t sessionVarsSize = 0;
size_t
RegisterSessionVar(size_t size)
{
size_t off = sessionVarsSize;
sessionVarsSize += size;
return off;
}
void*
CurSession(size_t offset)
{
return curSession->vars + offset;
}
/* module_internal.h */
typedef int my_hello_var_t;
extern size_t my_hello_var_offset;
/* access macros */
#define my_hello_var (*(my_hello_var_t*)(CurSessionVar(my_hello_var_offset)))
/* module.c */
size_t my_hello_var_offset = 0;
void
PG_init() {
RegisterSessionVar(sizeof(my_hello_var_t), &my_hello_var_offset);
}
For security reasons, offset could be mangled.
------
regards,
Yura Sokolov