Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc069655

Browse files
author
Neil Conway
committed
Allow ALTER FUNCTION to change a function's strictness, volatility, and
whether or not it is a security definer. Changing a function's strictnessis required by SQL2003, and the other capabilities make sense. Also, allowan optional RESTRICT noise word to be specified, for SQL conformance.Some trivial regression tests added and the documentation has beenupdated.
1 parent41e2a80 commitc069655

File tree

12 files changed

+384
-81
lines changed

12 files changed

+384
-81
lines changed

‎doc/src/sgml/ref/alter_function.sgml

Lines changed: 74 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/ref/alter_function.sgml,v 1.5 2004/06/25 21:55:50 tgl Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/ref/alter_function.sgml,v 1.6 2005/03/14 00:19:36 neilc Exp $
33
PostgreSQL documentation
44
-->
55

@@ -20,8 +20,15 @@ PostgreSQL documentation
2020

2121
<refsynopsisdiv>
2222
<synopsis>
23+
ALTER FUNCTION <replaceable>name</replaceable> ( [ <replaceable class="parameter">type</replaceable> [, ...] ] ) <replaceable class="PARAMETER">action</replaceable> [, ... ] [ RESTRICT ]
2324
ALTER FUNCTION <replaceable>name</replaceable> ( [ <replaceable class="parameter">type</replaceable> [, ...] ] ) RENAME TO <replaceable>newname</replaceable>
2425
ALTER FUNCTION <replaceable>name</replaceable> ( [ <replaceable class="parameter">type</replaceable> [, ...] ] ) OWNER TO <replaceable>newowner</replaceable>
26+
27+
where <replaceable class="PARAMETER">action</replaceable> is one of:
28+
29+
CALLED ON NULL INPUT | RETURNS NULL ON NULL INPUT | STRICT
30+
IMMUTABLE | STABLE | VOLATILE
31+
[ EXTERNAL ] SECURITY INVOKER | [ EXTERNAL ] SECURITY DEFINER
2532
</synopsis>
2633
</refsynopsisdiv>
2734

@@ -69,11 +76,65 @@ ALTER FUNCTION <replaceable>name</replaceable> ( [ <replaceable class="parameter
6976
<term><replaceable class="parameter">newowner</replaceable></term>
7077
<listitem>
7178
<para>
72-
The new owner of the function.
73-
To change the owner of a function, you must be a superuser.
74-
Note that if the function is marked
75-
<literal>SECURITY DEFINER</literal>,
76-
it will subsequently execute as the new owner.
79+
The new owner of the function. To change the owner of a
80+
function, you must be a superuser. Note that if the function is
81+
marked <literal>SECURITY DEFINER</literal>, it will subsequently
82+
execute as the new owner.
83+
</para>
84+
</listitem>
85+
</varlistentry>
86+
87+
<varlistentry>
88+
<term><literal>CALLED ON NULL INPUT</literal></term>
89+
<term><literal>RETURNS NULL ON NULL INPUT</literal></term>
90+
<term><literal>STRICT</literal></term>
91+
92+
<listitem>
93+
<para>
94+
<literal>CALLED ON NULL INPUT</literal> changes the function so
95+
that it will be invoked when some or all of its arguments are
96+
null. <literal>RETURNS NULL ON NULL INPUT</literal> or
97+
<literal>STRICT</literal> changes the function so that it
98+
always returns null if any of its arguments are null. See <xref
99+
linkend="sql-createfunction"> for more information.
100+
</para>
101+
</listitem>
102+
</varlistentry>
103+
104+
<varlistentry>
105+
<term><literal>IMMUTABLE</literal></term>
106+
<term><literal>STABLE</literal></term>
107+
<term><literal>VOLATILE</literal></term>
108+
109+
<listitem>
110+
<para>
111+
Change the volatility of the function to the specified
112+
type. See <xref linkend="sql-createfunction"> for more
113+
information about function volatility.
114+
</para>
115+
</listitem>
116+
</varlistentry>
117+
118+
<varlistentry>
119+
<term><literal><optional>EXTERNAL</optional> SECURITY INVOKER</literal></term>
120+
<term><literal><optional>EXTERNAL</optional> SECURITY DEFINER</literal></term>
121+
122+
<listitem>
123+
<para>
124+
Change whether the function is a security definer or not. The
125+
key word <literal>EXTERNAL</literal> is ignored for SQL
126+
conformance. See <xref linkend="sql-createfunction"> for more
127+
information about this capability.
128+
</para>
129+
</listitem>
130+
</varlistentry>
131+
132+
<varlistentry>
133+
<term><literal>RESTRICT</literal></term>
134+
135+
<listitem>
136+
<para>
137+
Ignored for conformance with the SQL standard.
77138
</para>
78139
</listitem>
79140
</varlistentry>
@@ -104,9 +165,13 @@ ALTER FUNCTION sqrt(integer) OWNER TO joe;
104165
<title>Compatibility</title>
105166

106167
<para>
107-
There is an <command>ALTER FUNCTION</command> statement in the SQL
108-
standard, but it does not provide the option to rename the
109-
function or change the owner.
168+
This statement is partially compatible with the <command>ALTER
169+
FUNCTION</> statement in the SQL standard. The standard allows more
170+
properties of a function to be modified, but does not provide the
171+
ability to rename a function, make a function a security definer,
172+
or change the owner or volatility of a function. The standard also
173+
requires the <literal>RESTRICT</> key word; it is optional in
174+
<productname>PostgreSQL</>.
110175
</para>
111176
</refsect1>
112177

‎doc/src/sgml/ref/alter_index.sgml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/ref/alter_index.sgml,v 1.4 2004/08/24 00:06:51 neilc Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/ref/alter_index.sgml,v 1.5 2005/03/14 00:19:36 neilc Exp $
33
PostgreSQL documentation
44
-->
55

@@ -20,10 +20,8 @@ PostgreSQL documentation
2020

2121
<refsynopsisdiv>
2222
<synopsis>
23-
ALTER INDEX <replaceable class="PARAMETER">name</replaceable>
24-
<replaceable class="PARAMETER">action</replaceable> [, ... ]
25-
ALTER INDEX <replaceable class="PARAMETER">name</replaceable>
26-
RENAME TO <replaceable class="PARAMETER">new_name</replaceable>
23+
ALTER INDEX <replaceable class="PARAMETER">name</replaceable> <replaceable class="PARAMETER">action</replaceable> [, ... ]
24+
ALTER INDEX <replaceable class="PARAMETER">name</replaceable> RENAME TO <replaceable class="PARAMETER">new_name</replaceable>
2725

2826
where <replaceable class="PARAMETER">action</replaceable> is one of:
2927

‎src/backend/commands/functioncmds.c

Lines changed: 141 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
* functioncmds.c
44
*
55
* Routines for CREATE and DROP FUNCTION commands and CREATE and DROP
6-
* CAST commands.
6+
* CAST commands.
77
*
88
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
99
* Portions Copyright (c) 1994, Regents of the University of California
1010
*
1111
*
1212
* IDENTIFICATION
13-
* $PostgreSQL: pgsql/src/backend/commands/functioncmds.c,v 1.55 2005/03/13 05:19:26 neilc Exp $
13+
* $PostgreSQL: pgsql/src/backend/commands/functioncmds.c,v 1.56 2005/03/14 00:19:36 neilc Exp $
1414
*
1515
* DESCRIPTION
1616
* These routines take the parse tree and pick out the
@@ -195,12 +195,75 @@ examine_parameter_list(List *parameter, Oid languageOid,
195195
returnparameterCount;
196196
}
197197

198+
/*
199+
* Recognize one of the options that can be passed to both CREATE
200+
* FUNCTION and ALTER FUNCTION and return it via one of the out
201+
* parameters. Returns true if the passed option was recognized. If
202+
* the out parameter we were going to assign to points to non-NULL,
203+
* raise a duplicate error.
204+
*/
205+
staticbool
206+
compute_common_attribute(DefElem*defel,
207+
DefElem**volatility_item,
208+
DefElem**strict_item,
209+
DefElem**security_item)
210+
{
211+
if (strcmp(defel->defname,"volatility")==0)
212+
{
213+
if (*volatility_item)
214+
gotoduplicate_error;
215+
216+
*volatility_item=defel;
217+
}
218+
elseif (strcmp(defel->defname,"strict")==0)
219+
{
220+
if (*strict_item)
221+
gotoduplicate_error;
222+
223+
*strict_item=defel;
224+
}
225+
elseif (strcmp(defel->defname,"security")==0)
226+
{
227+
if (*security_item)
228+
gotoduplicate_error;
229+
230+
*security_item=defel;
231+
}
232+
else
233+
return false;
234+
235+
/* Recognized an option */
236+
return true;
237+
238+
duplicate_error:
239+
ereport(ERROR,
240+
(errcode(ERRCODE_SYNTAX_ERROR),
241+
errmsg("conflicting or redundant options")));
242+
return false;/* keep compiler quiet */
243+
}
244+
245+
staticchar
246+
interpret_func_volatility(DefElem*defel)
247+
{
248+
char*str=strVal(defel->arg);
249+
250+
if (strcmp(str,"immutable")==0)
251+
returnPROVOLATILE_IMMUTABLE;
252+
elseif (strcmp(str,"stable")==0)
253+
returnPROVOLATILE_STABLE;
254+
elseif (strcmp(str,"volatile")==0)
255+
returnPROVOLATILE_VOLATILE;
256+
else
257+
{
258+
elog(ERROR,"invalid volatility \"%s\"",str);
259+
return0;/* keep compiler quiet */
260+
}
261+
}
198262

199263
/*
200264
* Dissect the list of options assembled in gram.y into function
201265
* attributes.
202266
*/
203-
204267
staticvoid
205268
compute_attributes_sql_style(List*options,
206269
List**as,
@@ -236,29 +299,13 @@ compute_attributes_sql_style(List *options,
236299
errmsg("conflicting or redundant options")));
237300
language_item=defel;
238301
}
239-
elseif (strcmp(defel->defname,"volatility")==0)
302+
elseif (compute_common_attribute(defel,
303+
&volatility_item,
304+
&strict_item,
305+
&security_item))
240306
{
241-
if (volatility_item)
242-
ereport(ERROR,
243-
(errcode(ERRCODE_SYNTAX_ERROR),
244-
errmsg("conflicting or redundant options")));
245-
volatility_item=defel;
246-
}
247-
elseif (strcmp(defel->defname,"strict")==0)
248-
{
249-
if (strict_item)
250-
ereport(ERROR,
251-
(errcode(ERRCODE_SYNTAX_ERROR),
252-
errmsg("conflicting or redundant options")));
253-
strict_item=defel;
254-
}
255-
elseif (strcmp(defel->defname,"security")==0)
256-
{
257-
if (security_item)
258-
ereport(ERROR,
259-
(errcode(ERRCODE_SYNTAX_ERROR),
260-
errmsg("conflicting or redundant options")));
261-
security_item=defel;
307+
/* recognized common option */
308+
continue;
262309
}
263310
else
264311
elog(ERROR,"option \"%s\" not recognized",
@@ -280,18 +327,7 @@ compute_attributes_sql_style(List *options,
280327
errmsg("no language specified")));
281328

282329
if (volatility_item)
283-
{
284-
if (strcmp(strVal(volatility_item->arg),"immutable")==0)
285-
*volatility_p=PROVOLATILE_IMMUTABLE;
286-
elseif (strcmp(strVal(volatility_item->arg),"stable")==0)
287-
*volatility_p=PROVOLATILE_STABLE;
288-
elseif (strcmp(strVal(volatility_item->arg),"volatile")==0)
289-
*volatility_p=PROVOLATILE_VOLATILE;
290-
else
291-
elog(ERROR,"invalid volatility \"%s\"",
292-
strVal(volatility_item->arg));
293-
}
294-
330+
*volatility_p=interpret_func_volatility(volatility_item);
295331
if (strict_item)
296332
*strict_p=intVal(strict_item->arg);
297333
if (security_item)
@@ -301,7 +337,7 @@ compute_attributes_sql_style(List *options,
301337

302338
/*-------------
303339
* Interpret the parameters *parameters and return their contents via
304-
*out parameters*isStrict_p and *volatility_p.
340+
* *isStrict_p and *volatility_p.
305341
*
306342
*These parameters supply optional information about a function.
307343
*All have defaults if not specified. Parameters:
@@ -347,9 +383,7 @@ compute_attributes_with_style(List *parameters, bool *isStrict_p, char *volatili
347383
* In all other cases
348384
*
349385
* AS <object reference, or sql code>
350-
*
351386
*/
352-
353387
staticvoid
354388
interpret_AS_clause(OidlanguageOid,constchar*languageName,List*as,
355389
char**prosrc_str_p,char**probin_str_p)
@@ -799,7 +833,74 @@ AlterFunctionOwner(List *name, List *argtypes, AclId newOwnerSysId)
799833
heap_close(rel,NoLock);
800834
}
801835

836+
/*
837+
* Implements the ALTER FUNCTION utility command (except for the
838+
* RENAME and OWNER clauses, which are handled as part of the generic
839+
* ALTER framework).
840+
*/
841+
void
842+
AlterFunction(AlterFunctionStmt*stmt)
843+
{
844+
HeapTupletup;
845+
OidfuncOid;
846+
Form_pg_procprocForm;
847+
Relationrel;
848+
ListCell*l;
849+
DefElem*volatility_item=NULL;
850+
DefElem*strict_item=NULL;
851+
DefElem*security_def_item=NULL;
852+
853+
rel=heap_openr(ProcedureRelationName,RowExclusiveLock);
854+
855+
funcOid=LookupFuncNameTypeNames(stmt->func->funcname,
856+
stmt->func->funcargs,
857+
false);
858+
859+
tup=SearchSysCacheCopy(PROCOID,
860+
ObjectIdGetDatum(funcOid),
861+
0,0,0);
862+
if (!HeapTupleIsValid(tup))/* should not happen */
863+
elog(ERROR,"cache lookup failed for function %u",funcOid);
864+
865+
procForm= (Form_pg_proc)GETSTRUCT(tup);
802866

867+
/* Permission check: must own function */
868+
if (!pg_proc_ownercheck(funcOid,GetUserId()))
869+
aclcheck_error(ACLCHECK_NOT_OWNER,ACL_KIND_PROC,
870+
NameListToString(stmt->func->funcname));
871+
872+
if (procForm->proisagg)
873+
ereport(ERROR,
874+
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
875+
errmsg("\"%s\" is an aggregate function",
876+
NameListToString(stmt->func->funcname))));
877+
878+
/* Examine requested actions. */
879+
foreach (l,stmt->actions)
880+
{
881+
DefElem*defel= (DefElem*)lfirst(l);
882+
883+
if (compute_common_attribute(defel,
884+
&volatility_item,
885+
&strict_item,
886+
&security_def_item)== false)
887+
elog(ERROR,"option \"%s\" not recognized",defel->defname);
888+
}
889+
890+
if (volatility_item)
891+
procForm->provolatile=interpret_func_volatility(volatility_item);
892+
if (strict_item)
893+
procForm->proisstrict=intVal(strict_item->arg);
894+
if (security_def_item)
895+
procForm->prosecdef=intVal(security_def_item->arg);
896+
897+
/* Do the update */
898+
simple_heap_update(rel,&tup->t_self,tup);
899+
CatalogUpdateIndexes(rel,tup);
900+
901+
heap_close(rel,NoLock);
902+
heap_freetuple(tup);
903+
}
803904

804905
/*
805906
* SetFunctionReturnType - change declared return type of a function

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp