This is a very basic syntax issue. You are starting the variable with
query := 'SELECT...
with a single quote. The variable is then finished because of the single quote at
...(tc.constraint_type)='foreign key...
(the single quote before foreign key). You need to add an additional single quote so it looks like this:
.... (tc.constraint_type)=''foreign key'' OR...
. This will ensure your string is not broken and that a valid SELECT statement is generated. See:
36.2. Tips for Developing in PL/pgSQL —
hot2use 1 min ago