Created
July 3, 2020 02:49
-
-
Save vipul-zambare006/21e8acd445795e0067fe0b5e73c949f0 to your computer and use it in GitHub Desktop.
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
CREATE OR REPLACE FUNCTION search_columns( | |
needle text, | |
haystack_tables name[] default '{}', | |
haystack_schema name[] default '{}' | |
) | |
RETURNS table(schemaname text, tablename text, columnname text, rowctid text) | |
AS $$ | |
begin | |
FOR schemaname,tablename,columnname IN | |
SELECT c.table_schema,c.table_name,c.column_name | |
FROM information_schema.columns c | |
JOIN information_schema.tables t ON | |
(t.table_name=c.table_name AND t.table_schema=c.table_schema) | |
WHERE (c.table_name=ANY(haystack_tables) OR haystack_tables='{}') | |
AND (c.table_schema=ANY(haystack_schema) OR haystack_schema='{}') | |
AND t.table_type='BASE TABLE' | |
LOOP | |
EXECUTE format('SELECT ctid FROM %I.%I WHERE cast(%I as text)=%L', | |
schemaname, | |
tablename, | |
columnname, | |
needle | |
) INTO rowctid; | |
IF rowctid is not null THEN | |
RETURN NEXT; | |
END IF; | |
END LOOP; | |
END; | |
$$ language plpgsql; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment