libpg_query icon indicating copy to clipboard operation
libpg_query copied to clipboard

Providing types for plpgsql function signature

Open jsommr opened this issue 2 years ago • 0 comments

"UNKNOWN" is currently returned for all types in a plpgsql function signature because of this:

https://github.com/pganalyze/libpg_query/blob/d1d0186b80c1fbf496265789af89da4e7ca890ab/src/pg_query_parse_plpgsql.c#L193

Which calls a mocked function:

https://github.com/pganalyze/libpg_query/blob/e9e4ba9ffcbac02bce6656ecabd22c2b54eca1b2/scripts/extract_source.rb#L470

We see that UNKNOWN is hardcoded because of typ->typname = pstrdup("UNKNOWN");.

It is quite easy to change plpgsql_build_datatype into something that returns a PLpgSQL_type where typname is populated with a given type. Say we have create function test(x customtype, y int) ..... For x the type will be customtype, while it will be pg_catalog.int4 for y because of the way the parser works. Question: Is that the reason this isn't implemented - that built in types are turned into something else for function signatures, while simply being provided as-is from the DECLARE section?

I can't figure out what ideally should be returned here. We do get the original location of the type in the function signature, and can use that to extract the actual written type, like int instead of pg_catalog.int4. It's not super straight forward with signatures like create function complicated(x character(100), y time with time zone default '...', z "mySchema" . sometype[]) etc., but not knowing what would be the right thing to use here, I'd rather ask if some thought have gone into this, or what opinions you have?

jsommr avatar Dec 30 '21 14:12 jsommr