supautils icon indicating copy to clipboard operation
supautils copied to clipboard

`run_process_utility_hook` is tangled everywhere, making logic hard to follow and properly review PRs

Open steve-chavez opened this issue 2 years ago • 1 comments

Problem

Right now the different modules logic are tangled per statement and the standard hook (run_process_utility_hook) is scattered everywhere. This makes it hard to:

  • maintain the code and to disable modules according to the pg version (https://github.com/supabase/supautils/issues/78, https://github.com/supabase/supautils/issues/50#issuecomment-1728441632).

  • properly review PRs, and we already had a consequence on https://github.com/supabase/supautils/pull/100

    • essentially, we have many case statements that have a return instead of a break. It's easy to do a return in some cases and miss hitting the run_process_utility_hook.

Solution

Refactor the codebase so each module has a single entrypoint and the standard hook is always invoked in one single place. Basically:

static void
supautils_hook(PROCESS_UTILITY_PARAMS)
{
  Node   *utility_stmt = pstmt->utilityStmt;
  ret1 = privileged_role_setup(utility_stmt)
  ret2 = reserved_roles_setup(utility_stmt)
  ret3 = privileged_extensions_setup(utility_stmt)
  ret4 = constrained_extensions_setup(utility_stmt)
  
  run_process_utility_hook(prev_hook);

  cleanup(ret1, ret2, ret3, ret4); // or have multiple cleanups depending on the module

steve-chavez avatar Feb 02 '24 00:02 steve-chavez

Check if the volatile added on https://github.com/supabase/supautils/pull/170/commits/ba8025c9468a2a96ba9d3db45a454f5587cdb897 can be removed when this refactor is done.

steve-chavez avatar Nov 27 '25 18:11 steve-chavez