-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace C parse_type() function with PL/pgSQL
The C code cause a number of challenges, including the requirement for a superuser to install the extension, and would require a fair bit of refactoring to abide by [secure design principals]. It also makes installation difficult on Windows, and would likely be rejected by organizations like AWS that tend to balk at C code. So delete the `parse_type()` function and all the C code added in 4ec32e7 and rewrite the `format_type_string()` function based on the prototype that @ewie developed in the comments of #315. By looking up the type output format function in the catalog, we're able to correctly output the data type with its full SQL name as before. The exception is interval types, which require the PostgreSQL grammar to parse the interval fields (`second`, `interval minute`, etc.) into a bitmask for the `typmod`. So require that they be specified exactly as PostgreSQL outputs them. [secure design principals]: https://www.postgresql.org/docs/current/extend-extensions.html#EXTEND-EXTENSIONS-SECURITY
- Loading branch information
Showing
6 changed files
with
80 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
DROP FUNCTION parse_type(type text, OUT typid oid, OUT typmod int4); | ||
|
||
CREATE OR REPLACE FUNCTION format_type_string ( TEXT ) | ||
RETURNS TEXT AS $$ | ||
DECLARE | ||
want_type TEXT := $1; | ||
typmodin_arg cstring[]; | ||
typmodin_func regproc; | ||
typmod int; | ||
BEGIN | ||
IF want_type::regtype = 'interval'::regtype THEN | ||
-- RAISE NOTICE 'cannot resolve: %', want_type; -- TODO | ||
RETURN want_type; | ||
END IF; | ||
|
||
-- Extract type modifier from type declaration and format as cstring[] literal. | ||
typmodin_arg := translate(substring(want_type FROM '[(][^")]+[)]'), '()', '{}'); | ||
|
||
-- Find typmodin function for want_type. | ||
SELECT typmodin INTO typmodin_func | ||
FROM pg_catalog.pg_type | ||
WHERE oid = want_type::regtype; | ||
|
||
IF typmodin_func = 0 THEN | ||
-- Easy: types without typemods. | ||
RETURN format_type(want_type::regtype, null); | ||
END IF; | ||
|
||
-- Get typemod via type-specific typmodin function. | ||
EXECUTE format('SELECT %I(%L)', typmodin_func, typmodin_arg) INTO typmod; | ||
RETURN format_type(want_type::regtype, typmod); | ||
EXCEPTION WHEN OTHERS THEN RETURN NULL; | ||
END; | ||
$$ LANGUAGE PLPGSQL STABLE; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.