Skip to content

Commit

Permalink
Use rendezvous variables for set_user post hooks
Browse files Browse the repository at this point in the history
This patch changes the set_user hooks so that they're held in the rendezvous
variable hash map as function pointers, rather than in the set_user .so. This
way, we don't have to ensure that set_user comes before any extension that
implements them -- load order doesn't matter. Just query the hash map during the
hook execution and if there are hooks implemented, call them.
  • Loading branch information
mpalmi authored and gitstashpop committed Sep 28, 2018
1 parent 4543aec commit 6df909a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
27 changes: 17 additions & 10 deletions set_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,6 @@ static Oid save_OldUserId = InvalidOid;
static char *reset_token = NULL;
static ProcessUtility_hook_type prev_hook = NULL;

post_reset_user_hook_type post_reset_user_hook = NULL;
post_set_user_hook_type post_set_user_hook = NULL;

#ifdef HAS_ALTER_SYSTEM
/* 9.4 & up */
static bool Block_AS = false;
Expand Down Expand Up @@ -160,7 +157,7 @@ static void PU_hook(Node *parsetree, const char *queryString,
#endif
#endif

extern void PostSetUserHook(bool is_reset, const char *newuser);
static void PostSetUserHook(bool is_reset, const char *newuser);

extern Datum set_user(PG_FUNCTION_ARGS);
void _PG_init(void);
Expand Down Expand Up @@ -628,14 +625,24 @@ PU_hook(Node *parsetree, const char *queryString,
/*
* PostSetUserHook
*
* Handler for the post_set_user_hook
* Handler for set_user post hooks
*/
void
PostSetUserHook(bool is_reset, const char *username)
{
if (post_reset_user_hook && is_reset)
(*post_reset_user_hook) ();
else if (post_set_user_hook)
(*post_set_user_hook) (username);
return;
/* Inter-extension hooks */
SetUserHooks **post_hooks;

post_hooks = (SetUserHooks **) find_rendezvous_variable(SET_USER_HOOKS_KEY);
if (*post_hooks)
{
if (!is_reset && (*post_hooks)->post_set_user)
{
(*post_hooks)->post_set_user(username);
}
else if ((*post_hooks)->post_reset_user)
{
(*post_hooks)->post_reset_user();
}
}
}
13 changes: 7 additions & 6 deletions set_user.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#ifndef SET_USER_H
#define SET_USER_H

/* Expose a hook for other extensions to use after reset_user finishes up. */
typedef void (*post_reset_user_hook_type) (void);
extern PGDLLIMPORT post_reset_user_hook_type post_reset_user_hook;
typedef struct SetUserHooks
{
void (*post_set_user) (const char *username);
void (*post_reset_user) ();
} SetUserHooks;

#define SET_USER_HOOKS_KEY "SetUserHooks"

/* Expose a hook for other extensions to use after set_user finishes up. */
typedef void (*post_set_user_hook_type) (const char *username);
extern PGDLLIMPORT post_set_user_hook_type post_set_user_hook;
#endif

0 comments on commit 6df909a

Please sign in to comment.