I'm just wondering if there is an established method for testing if a string can be used as an unquoted PostgreSQL identifier? (unquoted because almost any string can be a quoted identifier).
I ask because as shown in a previous question (How to quote qualified table name with format() function?), there are times when I would need to specify an identifier (such as the name of a table to be created) that does not yet exist, as string values (text) instead of a safer type such as regclass. Quoting the string/name can be problematic as shown there and probably else where. Without quoting, it's susceptible to SQL injection.
I guess if one programs it hard enough, a string parsing function can be written ultimately. Just wanted to check if there are existing solutions.
Related:
What are the valid formats of a PostgreSQL schema name?
Is the function PARSENAME() the opposite of QUOTENAME() (sql-server)
select format('%I','foo') = 'foo' as is_valid? – Mar 17 '18 at 08:40foo.bar, how it's supposed to know whether you mean tablebarin schemafoo, in which case it doesn't need to be quoted, or you mean tablefoo.barin the current schema, in which case it must be quoted? – Daniel Vérité Mar 17 '18 at 12:38foo.barwould be invalid as an unquoted ident, as it contains a dot., wouldn't it? The answer here (https://dba.stackexchange.com/a/45591/55439) mentions only underscore and dollar sign as acceptable non-letter characters. – tinlyx Mar 17 '18 at 17:28schema.tablejust like the SQL parser does, as your related question says qualified table name with format(). If not, the suggestion by @a_horse_with_no_name seems to answer the question. – Daniel Vérité Mar 17 '18 at 19:22