3

I have a massive database that I don't mind leaving to search for a while, but I can't dump the whole database for various reasons. What is the easiest query I can write that will search all fields of all tables in a database for a specific string of text?

The following won't work but should demonstrate what I would like to see:

 SELECT * FROM * where * like '%mystring%'

Any ideas?

4

2 回答 2

9

You could take advantage of the fact that in PostgreSQL, tablename.* can be cast to text.

So, SELECT t.* FROM tablename t WHERE (t.*)::text LIKE '%somestring%' will return any row where any column contains somestring, no matter what the type of the column.

If used in a loop on SELECT table_schema, table_name FROM information_schema, it's comparable to a grep within a database dump, except you don't need the dump.

于 2013-06-14T15:42:38.957 回答
5

A SELECT statement always returns a result-set. A result set is a kind of table with column names and one row for each result.

You are looking for a string. So there is no need to query any non-textual column at all.

The 'from'-clause of the select has to be specified. You could query the metadata information of your database about all table names. Then obtain all columns which are textual (like char, varchar, clob, ...) to be used in the 'where'-clause. Then apply the constructed select on each table.

This algorithm could be expressed in a stored procedure or by any program.

What prevents you from dumping the database? On a UNIX system you could pipe the dump directly to the 'grep' command.

于 2013-06-14T13:33:24.053 回答