Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ngzkp-0003m8-GE for pgsql-general@arkaria.postgresql.org; Wed, 20 Apr 2022 02:07:39 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1ngzko-0004PC-FS for pgsql-general@arkaria.postgresql.org; Wed, 20 Apr 2022 02:07:38 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ngzkn-0004Og-QL for pgsql-general@lists.postgresql.org; Wed, 20 Apr 2022 02:07:38 +0000 Received: from mail-pl1-x62d.google.com ([2607:f8b0:4864:20::62d]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1ngzkl-0003Ci-8F for pgsql-general@lists.postgresql.org; Wed, 20 Apr 2022 02:07:36 +0000 Received: by mail-pl1-x62d.google.com with SMTP id n8so469330plh.1 for ; Tue, 19 Apr 2022 19:07:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yugabyte.com; s=google; h=from:mime-version:subject:message-id:date:to; bh=PIzYp1EefrYRlDBOKvUFejgsjoMK2HkCi2BhGg3nj9I=; b=PWqHejQXcecvn8wWSuiL75ylIebw/khmB5k0LXiThl98TGswFblH7sQBhaynsUVbf1 xMHZROAmWWpjCbe80wtT7cGFKVdGk2c5SmLVml2I1qsfDuZub9i8uRszWSkDuU4v7SlA QXsHiB4pj/aA8ZZN6wOpCB8nKWOLoiynwxJLU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:mime-version:subject:message-id:date:to; bh=PIzYp1EefrYRlDBOKvUFejgsjoMK2HkCi2BhGg3nj9I=; b=H0iUnrFnS8D0/cZcrV8G52an63aTJdPWVHa0gcF1FF2eWA7gJr3Z30DYaSEVUKUqJD e5/IUall8GyT85cmLaK/CqVSjrzUwSQCT75yM7tkPdbuajsjbhoN01VQVHEpchOrFulT KiVVgWWB05GpLY/JXRCUv6W1/BnpxWYZLE0VVtm2kO/uzqxfuMGOoO9p81z1qjc8Gtbk o2vWIM6j+HD6AcGDsmQjvDLR2Xcr7T4RSzUO2eon0xCvqgMpA6vgaKMwDxT1WthBHGiN JJzyAD7ixvdz5XqbX5n9TWfdgysnddDqbn54sHeg/1gcoXoVQOxOQs8ZD83v8IJJo8YP P2Jg== X-Gm-Message-State: AOAM532vbHqIXc9KDA6mMSk2pCghI/OaIx+u0VdmsEqgqZDoEyT4x24w 0E+hAU/lFgyowLAIS1b7H6nptcUEnW4Kcw== X-Google-Smtp-Source: ABdhPJxtTsO5I+52Azv8hOcBQJ1HgXdC7HgjFO4iuauSHFpS2u7r9av9fa/HGG/KLdjorKGrWZzfQg== X-Received: by 2002:a17:902:854c:b0:159:a70:deca with SMTP id d12-20020a170902854c00b001590a70decamr9168835plo.142.1650420453594; Tue, 19 Apr 2022 19:07:33 -0700 (PDT) Received: from smtpclient.apple ([2601:646:101:7fda:fc78:abf:9173:f425]) by smtp.gmail.com with ESMTPSA id q4-20020a17090a7a8400b001cd4a0c3270sm16987594pjf.7.2022.04.19.19.07.31 for (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 19 Apr 2022 19:07:32 -0700 (PDT) From: Bryn Llewellyn Content-Type: multipart/alternative; boundary="Apple-Mail=_A5CD4539-9899-468E-87C7-0788ED3DE888" Mime-Version: 1.0 (Mac OS X Mail 14.0 \(3654.120.0.1.13\)) Subject: =?utf-8?Q?Unexpected_result_from_ALTER_FUNCTION=E2=80=94_looks_li?= =?utf-8?Q?ke_a_bug?= Message-Id: <8AC9A37F-99BD-446F-A2F7-B89AD0022774@yugabyte.com> Date: Tue, 19 Apr 2022 19:07:30 -0700 To: pgsql-general list X-Mailer: Apple Mail (2.3654.120.0.1.13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Apple-Mail=_A5CD4539-9899-468E-87C7-0788ED3DE888 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 SUMMARY This part of the syntax diagram for "alter function": ALTER FUNCTION name [ ( [ [ argmode ] [ argname ] argtype [, ...] ] ) ] = action [ =E2=80=A6 ] says that the first "action" can be followed (without punctuation) by = zero, one, or many other actions. A semantic rule says that no = particular action can be specified more than once. My tests used these = possible actions: SECURITY { INVOKER | DEFINER } SET configuration_parameter TO value=20 IMMUTABLE | STABLE | VOLATILE PARALLEL { UNSAFE | RESTRICTED | SAFE } The values of the properties set this way can be seen with a suitable = query against "pg_catalog.pg_proc". (See the complete testcase below.) = Suppose that the history of events shows this status for the function = s1.f(): name | type | security | proconfig = | volatility | parallel =20 = ------+------+----------+-------------------------------------------------= --------+------------+------------ f | func | invoker | = | volatile | unsafe =20 This statement: alter function s1.f() security definer immutable parallel restricted; brings this new status: name | type | security | proconfig = | volatility | parallel =20 = ------+------+----------+-------------------------------------------------= --------+------------+------------ f | func | definer | = | immutable | restricted confirming that the three specified changes have been made using just a = single "alter function" statement. However, when "SET configuration_parameter" is specified along with = other changes, then the "parallel" specification (but only this) is = ignored. The other three specifications are honored. alter function s1.f() security invoker set timezone =3D 'UTC' stable parallel safe; It brings this new status: name | type | security | proconfig = | volatility | parallel =20 = ------+------+----------+-------------------------------------------------= --------+------------+------------ f | func | invoker | {TimeZone=3DUTC} = | stable | restricted This is the bug. Notice that with "alter procedure", the semantic difference between a = procedure and a function means that you cannot specify "parallel" here, = and so you can't demonstrate the bug here. SELF-CONTAINED, RE-RUNNABLE TESTCASE tested using PG Version 14.1 = --------------------------------------------------------------------------= ------ -- demo.sql ----------- \o spool.txt \c postgres postgres set client_min_messages =3D warning; drop database if exists db; create database db owner postgres; \c db postgres set client_min_messages =3D warning; drop schema if exists public cascade; create schema s1 authorization postgres; \i prepare-qry.sql create function s1.f() returns int language plpgsql as $body$ begin return 0; end; $body$; \t off execute qry; alter function s1.f() security definer immutable parallel restricted; \t on execute qry; -- Here is the bug. The test is meaningful only for a function. alter function s1.f() security invoker set timezone =3D 'UTC' stable parallel safe; execute qry; \o = --------------------------------------------------------------------------= ------ -- prepare-qry.sql ------------------ drop view if exists s1.subprograms cascade; create view s1.subprograms( name, pronamespace, type, security, proconfig, volatility, parallel) as select proname::text as name, pronamespace::regnamespace::text, case prokind when 'a' then 'agg' when 'w' then 'window' when 'p' then 'proc' else 'func' end, case when prosecdef then 'definer' else 'invoker' end, coalesce(proconfig::text, '') as proconfig, case when provolatile =3D 'i' then 'immutable' when provolatile =3D 's' then 'stable' when provolatile =3D 'v' then 'volatile' end, case when proparallel =3D 'r' then 'restricted' when proparallel =3D 's' then 'safe' when proparallel =3D 'u' then 'unsafe' end from pg_catalog.pg_proc where proowner::regrole::text =3D 'postgres' and pronamespace::regnamespace::text =3D 's1' and pronargs =3D 0; prepare qry as select rpad(name, 4) as name, rpad(type, 4) as type, rpad(security, 8) as security, rpad(proconfig, 55) as proconfig, rpad(volatility, 10) as volatility, rpad(parallel, 10) as parallel from s1.subprograms where type in ('func', 'proc') and pronamespace::regnamespace::text =3D 's1' order by name; = --------------------------------------------------------------------------= ------ spool.txt --------- name | type | security | proconfig = | volatility | parallel =20 = ------+------+----------+-------------------------------------------------= --------+------------+------------ f | func | invoker | = | volatile | unsafe =20 f | func | definer | = | immutable | restricted f | func | invoker | {TimeZone=3DUTC} = | stable | restricted --Apple-Mail=_A5CD4539-9899-468E-87C7-0788ED3DE888 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8
SUMMARY

This part of the syntax = diagram for "alter function":

ALTER FUNCTION name [ ( [ [ argmode ] = [ argname ] argtype [, ...] ] ) ] action [ =E2=80=A6 = ]

says that the first "action" can be followed (without = punctuation) by zero, one, or many other actions. A semantic rule says = that no particular action can be specified more than once. My tests used = these possible actions:

SECURITY { = INVOKER |  DEFINER }
SET configuration_parameter TO= value 
IMMUTABLE | STABLE | VOLATILE
PARALLEL { UNSAFE | RESTRICTED | SAFE = }

The values of the properties set this way can be seen with a = suitable query against "pg_catalog.pg_proc". (See the complete testcase = below.) Suppose that the history of events shows this status for the = function s1.f():

 name | = type | security |               =          proconfig         =                | volatility = |  parallel  
------+------+----------+--------------------------------------= -------------------+------------+------------
 f    | func | = invoker  |                 =                     =                     | = volatile   | unsafe   

This = statement:

alter function s1.f()
security definer
immutable
parallel= restricted;

brings this = new status:

 name | type | security |        =               =   proconfig              =           | volatility = |  parallel  
------+------+----------+--------------------------------------= -------------------+------------+------------
 f =    | func | definer  |           =                     =                     =       | immutable =  | restricted

confirming that the three specified = changes have been made using just a single "alter function" = statement.

However, when "SET configuration_parameter" is specified = along with other changes, then the "parallel" specification (but = only this) is ignored. The other three specifications are = honored.

alter function s1.f()
security invoker
set timezone =3D 'UTC'
stable
parallel = safe;

It brings this new status:

 name | type | security = |                    =     proconfig            =             | volatility = |  parallel  
------+------+----------+--------------------------------------= -------------------+------------+------------
 f =    | func | invoker  | {TimeZone=3DUTC}     =                     =                  | stable =     | restricted

This is the bug.
Notice that with "alter procedure", = the semantic difference between a procedure and a function means that = you cannot specify "parallel" here, and so you can't demonstrate the bug = here.

SELF-CONTAINED, = RE-RUNNABLE TESTCASE tested using PG Version 14.1

---------------------------------------------------------------= -----------------
-- demo.sql
-----------

\o spool.txt

\c postgres postgres
set client_min_messages =3D = warning;
drop database if exists db;
create = database db owner postgres;

\c db = postgres
set client_min_messages =3D warning;
drop schema if exists public cascade;
create = schema s1 authorization postgres;

\i = prepare-qry.sql

create function s1.f()
  returns int
  language = plpgsql
as $body$
begin
  return 0;
end;
$body$;

\t off
execute qry;

alter function = s1.f()
security definer
immutable
parallel restricted;

\t on
execute qry;

-- Here is the bug. = The test is meaningful only for a function.
alter function = s1.f()
security invoker
set timezone =3D = 'UTC'
stable
parallel safe;

execute qry;

\o

---------------------------------------------------------------= -----------------
-- prepare-qry.sql
------------------

drop view if exists s1.subprograms cascade;
create view s1.subprograms(
  name,
  pronamespace,
  type,
  security,
  proconfig,
  volatility,
  parallel)
as
select
  proname::text= as name,
  pronamespace::regnamespace::text,
  case prokind
  =   when 'a' then 'agg'
    when = 'w' then 'window'
    when 'p' then = 'proc'
    else 'func'
  end,
 case
  =   when prosecdef then 'definer'
  =   else 'invoker'
  end,
  coalesce(proconfig::text, '') as proconfig,
  case
    when = provolatile =3D 'i' then 'immutable'
  =   when provolatile =3D 's' then 'stable'
  =   when provolatile =3D 'v' then 'volatile'
  end,
  case
    when proparallel =3D 'r' then = 'restricted'
    when proparallel =3D 's' = then 'safe'
    when proparallel =3D 'u' = then 'unsafe'
  end
from = pg_catalog.pg_proc
where
  proowner::regrole::text =3D 'postgres' and
  pronamespace::regnamespace::text =3D 's1' and
  pronargs =3D 0;

prepare qry as
select
  rpad(name,        4) as = name,
  rpad(type,      =   4) as type,
  rpad(security,  =   8) as security,
  rpad(proconfig,  55) as proconfig,
  rpad(volatility, 10) as volatility,
  rpad(parallel,   10) as = parallel
from s1.subprograms
where type in = ('func', 'proc')
and   pronamespace::regnamespace::text =3D = 's1'
order by name;

---------------------------------------------------------------= -----------------
spool.txt
---------

 name | type | security |  =                     =   proconfig              =           | volatility = |  parallel  
------+------+----------+--------------------------------------= -------------------+------------+------------
 f  =   | func | invoker  |       =                     =                     =           | volatile   | = unsafe    

 f  =   | func | definer  |       =                     =                     =           | immutable  | = restricted

 f    | func = | invoker  | {TimeZone=3DUTC}        =                     =               | = stable     | restricted


= --Apple-Mail=_A5CD4539-9899-468E-87C7-0788ED3DE888--