Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit d691cb9

Browse files
committed
Fix erroneous handling of shared dependencies (ie dependencies on roles)
in CREATE OR REPLACE FUNCTION. The original code would update pg_shdepend as if a new function was being created, even if it wasn't, with two bad consequences: pg_shdepend might record the wrong owner for the function, and any dependencies for roles mentioned in the function's ACL would be lost. The fix is very easy: just don't touch pg_shdepend at all when doing a function replacement. Also update the CREATE FUNCTION reference page, which never explained exactly what changes and doesn't change in a function replacement. In passing, fix the CREATE VIEW reference page similarly; there's no code bug there, but the docs didn't say what happens.
1 parent caa4cfa commit d691cb9

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

doc/src/sgml/ref/create_function.sgml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/ref/create_function.sgml,v 1.86 2009/09/19 10:23:26 petere Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/ref/create_function.sgml,v 1.87 2009/10/02 18:13:04 tgl Exp $
33
-->
44

55
<refentry id="SQL-CREATEFUNCTION">
@@ -562,6 +562,14 @@ CREATE FUNCTION foo(int, int default 42) ...
562562
<literal>USAGE</literal> privilege on the language.
563563
</para>
564564

565+
<para>
566+
When <command>CREATE OR REPLACE FUNCTION</> is used to replace an
567+
existing function, the ownership and permissions of the function
568+
do not change. All other function properties are assigned the
569+
values specified or implied in the command. You must own the function
570+
to replace it (this includes being a member of the owning role).
571+
</para>
572+
565573
</refsect1>
566574

567575
<refsect1 id="sql-createfunction-examples">

doc/src/sgml/ref/create_view.sgml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/ref/create_view.sgml,v 1.41 2009/01/27 12:40:15 petere Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/ref/create_view.sgml,v 1.42 2009/10/02 18:13:04 tgl Exp $
33
PostgreSQL documentation
44
-->
55

@@ -149,6 +149,14 @@ CREATE VIEW vista AS SELECT text 'Hello World' AS hello;
149149
used by the view.
150150
</para>
151151

152+
<para>
153+
When <command>CREATE OR REPLACE VIEW</> is used on an
154+
existing view, only the view's defining SELECT rule is changed.
155+
Other view properties, including ownership, permissions, and non-SELECT
156+
rules, remain unchanged. You must own the view
157+
to replace it (this includes being a member of the owning role).
158+
</para>
159+
152160
</refsect1>
153161

154162
<refsect1>

src/backend/catalog/pg_proc.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/catalog/pg_proc.c,v 1.165 2009/09/22 23:43:37 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/catalog/pg_proc.c,v 1.166 2009/10/02 18:13:04 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -89,6 +89,7 @@ ProcedureCreate(const char *procedureName,
8989
bool internalInParam = false;
9090
bool internalOutParam = false;
9191
Oid variadicType = InvalidOid;
92+
Oid proowner = GetUserId();
9293
Relation rel;
9394
HeapTuple tup;
9495
HeapTuple oldtup;
@@ -290,7 +291,7 @@ ProcedureCreate(const char *procedureName,
290291
namestrcpy(&procname, procedureName);
291292
values[Anum_pg_proc_proname - 1] = NameGetDatum(&procname);
292293
values[Anum_pg_proc_pronamespace - 1] = ObjectIdGetDatum(procNamespace);
293-
values[Anum_pg_proc_proowner - 1] = ObjectIdGetDatum(GetUserId());
294+
values[Anum_pg_proc_proowner - 1] = ObjectIdGetDatum(proowner);
294295
values[Anum_pg_proc_prolang - 1] = ObjectIdGetDatum(languageObjectId);
295296
values[Anum_pg_proc_procost - 1] = Float4GetDatum(procost);
296297
values[Anum_pg_proc_prorows - 1] = Float4GetDatum(prorows);
@@ -353,7 +354,7 @@ ProcedureCreate(const char *procedureName,
353354
(errcode(ERRCODE_DUPLICATE_FUNCTION),
354355
errmsg("function \"%s\" already exists with same argument types",
355356
procedureName)));
356-
if (!pg_proc_ownercheck(HeapTupleGetOid(oldtup), GetUserId()))
357+
if (!pg_proc_ownercheck(HeapTupleGetOid(oldtup), proowner))
357358
aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_PROC,
358359
procedureName);
359360

@@ -471,7 +472,10 @@ ProcedureCreate(const char *procedureName,
471472
procedureName)));
472473
}
473474

474-
/* do not change existing ownership or permissions, either */
475+
/*
476+
* Do not change existing ownership or permissions, either. Note
477+
* dependency-update code below has to agree with this decision.
478+
*/
475479
replaces[Anum_pg_proc_proowner - 1] = false;
476480
replaces[Anum_pg_proc_proacl - 1] = false;
477481

@@ -498,12 +502,11 @@ ProcedureCreate(const char *procedureName,
498502
/*
499503
* Create dependencies for the new function. If we are updating an
500504
* existing function, first delete any existing pg_depend entries.
505+
* (However, since we are not changing ownership or permissions, the
506+
* shared dependencies do *not* need to change, and we leave them alone.)
501507
*/
502508
if (is_update)
503-
{
504509
deleteDependencyRecordsFor(ProcedureRelationId, retval);
505-
deleteSharedDependencyRecordsFor(ProcedureRelationId, retval, 0);
506-
}
507510

508511
myself.classId = ProcedureRelationId;
509512
myself.objectId = retval;
@@ -537,7 +540,8 @@ ProcedureCreate(const char *procedureName,
537540
}
538541

539542
/* dependency on owner */
540-
recordDependencyOnOwner(ProcedureRelationId, retval, GetUserId());
543+
if (!is_update)
544+
recordDependencyOnOwner(ProcedureRelationId, retval, proowner);
541545

542546
heap_freetuple(tup);
543547

0 commit comments

Comments
 (0)