Home > Net >  Why can PL/pgSQL variables marked CONSTANT be passed as OUT parameters?
Why can PL/pgSQL variables marked CONSTANT be passed as OUT parameters?

Time:07-28

I am learning PL/pgSQL (coming from a C# background) and defined a procedure to swap two numbers:

CREATE PROCEDURE swap_nums(INOUT arg_left INT, INOUT arg_right INT) LANGUAGE plpgsql AS
$plpgsql$
DECLARE
  temp_num INT = arg_left;
BEGIN
  arg_left = arg_right;
  arg_right = temp_num;
END;
$plpgsql$;

I then call the swap_nums procedure as follows:

DO $plpgsql$
  DECLARE
    num1 CONSTANT INT NOT NULL = 10;
    num2 CONSTANT INT NOT NULL = 20;
  BEGIN
    RAISE NOTICE 'Before swap: num1=%, num2=%', num1, num2;
    CALL swap_nums(num1, num2); -- note CONSTANT variables can be altered via OUT params!
    RAISE NOTICE 'After  swap: num1=%, num2=%', num1, num2;
  END;
$plpgsql$;

The swap_nums function works as expected and I get this output:

NOTICE:  Before swap: num1=10, num2=20
NOTICE:  After  swap: num1=20, num2=10
DO

The enter image description here

CodePudding user response:

It's a bug. Fixed in pg15.

demo: https://dbfiddle.uk/?rdbms=postgres_15&fiddle=43797c7ab8e83f1f9058c6fa49ec9d60

Further info: https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=ccd10a9bfa54c1aad3561232bf24222f1b455e1c

  • Related