ariga / atlas

Manage your database schema as code
https://atlasgo.io
Apache License 2.0
5.97k stars 265 forks source link

Feature Request: Support for `SET configuration_parameter { TO value | = value | FROM CURRENT }` in Function Definition Syntax #3054

Open mpyw opened 2 months ago

mpyw commented 2 months ago

Description:

I would like to request a new feature in Atlas HCL to support the following SQL syntax within function definitions:

-- Create "pg_try_advisory_lock_timeout" function
CREATE FUNCTION "public"."pg_try_advisory_lock_timeout"("key" text, "timeout" text)
RETURNS boolean
SET lock_timeout FROM CURRENT
LANGUAGE plpgsql
AS $$
  BEGIN
    EXECUTE format('SET SESSION lock_timeout TO %L;', timeout);
    PERFORM pg_advisory_lock(hashtext(key));
    RETURN true;
  EXCEPTION
    WHEN lock_not_available OR deadlock_detected THEN RETURN false;
  END
$$;

In this example, the function temporarily sets the advisory lock timeout and restores the previous value upon exit. The syntax SET ... FROM CURRENT is used to revert the session's configuration back to its original state, making it essential for scenarios where temporary session configurations are needed.

However, the current version of Atlas HCL does not seem to support the SET ... FROM CURRENT syntax within function definitions, which is not limited to lock_timeout but applies to any session configuration.

Adding support for this syntax in function definitions would significantly enhance Atlas's ability to manage and manipulate temporary session settings efficiently.

Thank you for considering this request.

mpyw commented 2 months ago

PostgreSQL: Documentation: 16: CREATE FUNCTION

CREATE [ OR REPLACE ] FUNCTION
    name ( [ [ argmode ] [ argname ] argtype [ { DEFAULT | = } default_expr ] [, ...] ] )
    [ RETURNS rettype
      | RETURNS TABLE ( column_name column_type [, ...] ) ]
  { LANGUAGE lang_name
    | TRANSFORM { FOR TYPE type_name } [, ... ]
    | WINDOW
    | { IMMUTABLE | STABLE | VOLATILE }
    | [ NOT ] LEAKPROOF
    | { CALLED ON NULL INPUT | RETURNS NULL ON NULL INPUT | STRICT }
    | { [ EXTERNAL ] SECURITY INVOKER | [ EXTERNAL ] SECURITY DEFINER }
    | PARALLEL { UNSAFE | RESTRICTED | SAFE }
    | COST execution_cost
    | ROWS result_rows
    | SUPPORT support_function
    | SET configuration_parameter { TO value | = value | FROM CURRENT }
    | AS 'definition'
    | AS 'obj_file', 'link_symbol'
    | sql_body
  } ...

Here -> | SET configuration_parameter { TO value | = value | FROM CURRENT }

mpyw commented 2 months ago

Note: It is important to mention that FROM CURRENT does not always refer to the actual current session state. Unfortunately, it refers to the state at the time the CREATE FUNCTION command is executed. Despite this limitation, using the syntax like SET lock_timeout TO '0' is still very meaningful, and therefore, I strongly request that this syntax be supported at a minimum.