Re: Let's make PostgreSQL multi-threaded

From: Yura Sokolov <y(dot)sokolov(at)postgrespro(dot)ru>
To: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: Let's make PostgreSQL multi-threaded
Date: 2023-06-07 16:05:54
Message-ID: 8c105e05-2e05-68fa-cc6e-1c9b92e60d64@postgrespro.ru
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Fujii Masao 2023-06-07 16:25:38 Re: Return value of pg_promote()
Previous Message Fujii Masao 2023-06-07 15:53:56 Re: [DOCS] alter_foreign_table.sgml typo