Skip to content

Commit 6338b50

Browse files
committed
Fix dependency, when changing a function's argument/return type.
When a new base type is created using the old-style procedure of first creating the input/output functions with "opaque" in place of the base type, the "opaque" argument/return type is changed to the final base type, on CREATE TYPE. However, we did not create a pg_depend record when doing that, so the functions were left not depending on the type. Fixes bug #14706, reported by Karen Huddleston. Discussion: https://www.postgresql.org/message-id/20170614232259.1424.82774@wrigleys.postgresql.org
1 parent 8e7d584 commit 6338b50

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

src/backend/commands/functioncmds.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,6 +1220,8 @@ SetFunctionReturnType(Oid funcOid, Oid newRetType)
12201220
Relation pg_proc_rel;
12211221
HeapTuple tup;
12221222
Form_pg_proc procForm;
1223+
ObjectAddress func_address;
1224+
ObjectAddress type_address;
12231225

12241226
pg_proc_rel = heap_open(ProcedureRelationId, RowExclusiveLock);
12251227

@@ -1240,6 +1242,20 @@ SetFunctionReturnType(Oid funcOid, Oid newRetType)
12401242
CatalogUpdateIndexes(pg_proc_rel, tup);
12411243

12421244
heap_close(pg_proc_rel, RowExclusiveLock);
1245+
1246+
/*
1247+
* Also update the dependency to the new type. Opaque is a pinned type, so
1248+
* there is no old dependency record for it that we would need to remove.
1249+
*/
1250+
type_address.classId = TypeRelationId;
1251+
type_address.objectId = newRetType;
1252+
type_address.objectSubId = 0;
1253+
1254+
func_address.classId = ProcedureRelationId;
1255+
func_address.objectId = funcOid;
1256+
func_address.objectSubId = 0;
1257+
1258+
recordDependencyOn(&func_address, &type_address, DEPENDENCY_NORMAL);
12431259
}
12441260

12451261

@@ -1254,6 +1270,8 @@ SetFunctionArgType(Oid funcOid, int argIndex, Oid newArgType)
12541270
Relation pg_proc_rel;
12551271
HeapTuple tup;
12561272
Form_pg_proc procForm;
1273+
ObjectAddress func_address;
1274+
ObjectAddress type_address;
12571275

12581276
pg_proc_rel = heap_open(ProcedureRelationId, RowExclusiveLock);
12591277

@@ -1275,6 +1293,20 @@ SetFunctionArgType(Oid funcOid, int argIndex, Oid newArgType)
12751293
CatalogUpdateIndexes(pg_proc_rel, tup);
12761294

12771295
heap_close(pg_proc_rel, RowExclusiveLock);
1296+
1297+
/*
1298+
* Also update the dependency to the new type. Opaque is a pinned type, so
1299+
* there is no old dependency record for it that we would need to remove.
1300+
*/
1301+
type_address.classId = TypeRelationId;
1302+
type_address.objectId = newArgType;
1303+
type_address.objectSubId = 0;
1304+
1305+
func_address.classId = ProcedureRelationId;
1306+
func_address.objectId = funcOid;
1307+
func_address.objectSubId = 0;
1308+
1309+
recordDependencyOn(&func_address, &type_address, DEPENDENCY_NORMAL);
12781310
}
12791311

12801312

src/test/regress/expected/create_type.out

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,37 @@ ERROR: type "text_w_default" already exists
109109
DROP TYPE default_test_row CASCADE;
110110
NOTICE: drop cascades to function get_default_test()
111111
DROP TABLE default_test;
112+
-- Check type create with input/output incompatibility
113+
CREATE TYPE not_existing_type (INPUT = array_in,
114+
OUTPUT = array_out,
115+
ELEMENT = int,
116+
INTERNALLENGTH = 32);
117+
ERROR: function array_out(not_existing_type) does not exist
118+
-- Check dependency transfer of opaque functions when creating a new type
119+
CREATE FUNCTION base_fn_in(cstring) RETURNS opaque AS 'boolin'
120+
LANGUAGE internal IMMUTABLE STRICT;
121+
CREATE FUNCTION base_fn_out(opaque) RETURNS opaque AS 'boolout'
122+
LANGUAGE internal IMMUTABLE STRICT;
123+
CREATE TYPE base_type(INPUT = base_fn_in, OUTPUT = base_fn_out);
124+
WARNING: changing argument type of function base_fn_out from "opaque" to base_type
125+
WARNING: changing return type of function base_fn_in from "opaque" to base_type
126+
WARNING: changing return type of function base_fn_out from "opaque" to "cstring"
127+
DROP FUNCTION base_fn_in(cstring); -- error
128+
ERROR: cannot drop function base_fn_in(cstring) because other objects depend on it
129+
DETAIL: type base_type depends on function base_fn_in(cstring)
130+
function base_fn_out(base_type) depends on type base_type
131+
HINT: Use DROP ... CASCADE to drop the dependent objects too.
132+
DROP FUNCTION base_fn_out(opaque); -- error
133+
ERROR: function base_fn_out(opaque) does not exist
134+
DROP TYPE base_type; -- error
135+
ERROR: cannot drop type base_type because other objects depend on it
136+
DETAIL: function base_fn_out(base_type) depends on type base_type
137+
function base_fn_in(cstring) depends on type base_type
138+
HINT: Use DROP ... CASCADE to drop the dependent objects too.
139+
DROP TYPE base_type CASCADE;
140+
NOTICE: drop cascades to 2 other objects
141+
DETAIL: drop cascades to function base_fn_out(base_type)
142+
drop cascades to function base_fn_in(cstring)
112143
-- Check usage of typmod with a user-defined type
113144
-- (we have borrowed numeric's typmod functions)
114145
CREATE TEMP TABLE mytab (foo widget(42,13,7)); -- should fail

src/test/regress/sql/create_type.sql

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,23 @@ DROP TYPE default_test_row CASCADE;
109109

110110
DROP TABLE default_test;
111111

112+
-- Check type create with input/output incompatibility
113+
CREATE TYPE not_existing_type (INPUT = array_in,
114+
OUTPUT = array_out,
115+
ELEMENT = int,
116+
INTERNALLENGTH = 32);
117+
118+
-- Check dependency transfer of opaque functions when creating a new type
119+
CREATE FUNCTION base_fn_in(cstring) RETURNS opaque AS 'boolin'
120+
LANGUAGE internal IMMUTABLE STRICT;
121+
CREATE FUNCTION base_fn_out(opaque) RETURNS opaque AS 'boolout'
122+
LANGUAGE internal IMMUTABLE STRICT;
123+
CREATE TYPE base_type(INPUT = base_fn_in, OUTPUT = base_fn_out);
124+
DROP FUNCTION base_fn_in(cstring); -- error
125+
DROP FUNCTION base_fn_out(opaque); -- error
126+
DROP TYPE base_type; -- error
127+
DROP TYPE base_type CASCADE;
128+
112129
-- Check usage of typmod with a user-defined type
113130
-- (we have borrowed numeric's typmod functions)
114131

0 commit comments

Comments
 (0)