Skip to content

Commit 8f72a57

Browse files
committed
Fix format_type() to restore its old behavior.
Commit a26116c accidentally changed the behavior of the SQL format_type() function while refactoring. For the reasons explained in that function's comment, a NULL typemod argument should behave differently from a -1 argument. Since we've managed to break this, add a regression test memorializing the intended behavior. In passing, be consistent about the type of the "flags" parameter. Noted by Rushabh Lathia, though I revised the patch some more. Discussion: https://postgr.es/m/CAGPqQf3RB2q-d2Awp_-x-Ur6aOxTUwnApt-vm-iTtceZxYnePg@mail.gmail.com
1 parent 1437824 commit 8f72a57

File tree

4 files changed

+46
-12
lines changed

4 files changed

+46
-12
lines changed

contrib/postgres_fdw/deparse.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,7 @@ foreign_expr_walker(Node *node,
854854
static char *
855855
deparse_type_name(Oid type_oid, int32 typemod)
856856
{
857-
uint8 flags = FORMAT_TYPE_TYPEMOD_GIVEN;
857+
bits16 flags = FORMAT_TYPE_TYPEMOD_GIVEN;
858858

859859
if (!is_builtin(type_oid))
860860
flags |= FORMAT_TYPE_FORCE_QUALIFY;

src/backend/utils/adt/format_type.c

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,23 @@ format_type(PG_FUNCTION_ARGS)
6363
Oid type_oid;
6464
int32 typemod;
6565
char *result;
66+
bits16 flags = FORMAT_TYPE_ALLOW_INVALID;
6667

6768
/* Since this function is not strict, we must test for null args */
6869
if (PG_ARGISNULL(0))
6970
PG_RETURN_NULL();
7071

7172
type_oid = PG_GETARG_OID(0);
72-
typemod = PG_ARGISNULL(1) ? -1 : PG_GETARG_INT32(1);
7373

74-
result = format_type_extended(type_oid, typemod,
75-
FORMAT_TYPE_TYPEMOD_GIVEN |
76-
FORMAT_TYPE_ALLOW_INVALID);
74+
if (PG_ARGISNULL(1))
75+
typemod = -1;
76+
else
77+
{
78+
typemod = PG_GETARG_INT32(1);
79+
flags |= FORMAT_TYPE_TYPEMOD_GIVEN;
80+
}
81+
82+
result = format_type_extended(type_oid, typemod, flags);
7783

7884
PG_RETURN_TEXT_P(cstring_to_text(result));
7985
}
@@ -82,21 +88,23 @@ format_type(PG_FUNCTION_ARGS)
8288
* format_type_extended
8389
* Generate a possibly-qualified type name.
8490
*
85-
* The default is to only qualify if the type is not in the search path, to
86-
* ignore the given typmod, and to raise an error if a non-existent type_oid is
87-
* given.
91+
* The default behavior is to only qualify if the type is not in the search
92+
* path, to ignore the given typmod, and to raise an error if a non-existent
93+
* type_oid is given.
8894
*
8995
* The following bits in 'flags' modify the behavior:
9096
* - FORMAT_TYPE_TYPEMOD_GIVEN
91-
* consider the given typmod in the output (may be -1 to request
92-
* the default behavior)
93-
*
97+
* include the typmod in the output (typmod could still be -1 though)
9498
* - FORMAT_TYPE_ALLOW_INVALID
9599
* if the type OID is invalid or unknown, return ??? or such instead
96100
* of failing
97-
*
98101
* - FORMAT_TYPE_FORCE_QUALIFY
99102
* always schema-qualify type names, regardless of search_path
103+
*
104+
* Note that TYPEMOD_GIVEN is not interchangeable with "typemod == -1";
105+
* see the comments above for format_type().
106+
*
107+
* Returns a palloc'd string.
100108
*/
101109
char *
102110
format_type_extended(Oid type_oid, int32 typemod, bits16 flags)

src/test/regress/expected/create_type.out

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,23 @@ TABLE mytab;
191191
(-44,5.5,12)
192192
(2 rows)
193193

194+
-- and test format_type() a bit more, too
195+
select format_type('varchar'::regtype, 42);
196+
format_type
197+
-----------------------
198+
character varying(38)
199+
(1 row)
200+
201+
select format_type('bpchar'::regtype, null);
202+
format_type
203+
-------------
204+
character
205+
(1 row)
206+
207+
-- this behavior difference is intentional
208+
select format_type('bpchar'::regtype, -1);
209+
format_type
210+
-------------
211+
bpchar
212+
(1 row)
213+

src/test/regress/sql/create_type.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,9 @@ WHERE attrelid = 'mytab'::regclass AND attnum > 0;
148148
-- might as well exercise the widget type while we're here
149149
INSERT INTO mytab VALUES ('(1,2,3)'), ('(-44,5.5,12)');
150150
TABLE mytab;
151+
152+
-- and test format_type() a bit more, too
153+
select format_type('varchar'::regtype, 42);
154+
select format_type('bpchar'::regtype, null);
155+
-- this behavior difference is intentional
156+
select format_type('bpchar'::regtype, -1);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy