-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpgrep.sql
More file actions
32 lines (29 loc) · 774 Bytes
/
pgrep.sql
File metadata and controls
32 lines (29 loc) · 774 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
CREATE OR REPLACE FUNCTION public.pgrep(
tablename_pattern character varying,
row_pattern character varying,
limit_rows integer)
RETURNS SETOF record AS
$BODY$
declare
r record;
result record;
tablename varchar;
query varchar;
begin
for tablename in select nspname||'.'||relname from pg_class a, pg_namespace b where a.relnamespace = b.oid and relname ilike tablename_pattern and relkind = 'r' loop
query := 'select * from '||tablename;
if limit_rows > 0 then
query := query || ' limit '||limit_rows;
end if;
for r in execute query loop
if r::varchar ilike row_pattern then
result := row(tablename, r::varchar);
return next result;
end if;
end loop;
end loop;
end;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100
ROWS 1000;