К основному контенту

Исключение группы в регулярных выражениях (non-capturing group)


(?:from|join)[ \r\n\t]{0,}([a-z._]{0,})
?: begin non-capturing group
Выделенная группа не будет учитываться ...

пример получения всех таблиц из накопленной статистики запросов

       
with t1 as (
select queryid, calls, lower(query) as full_query, replace((regexp_matches(lower(query), '(?: from | join | lateral )[ \r\n\t]{0,}(["a-z0-9._]{0,})','gm'))[1],'"','') as foundedfulltable
from pg_stat_statements
where lower(query) not like '%function%'
)
, t2 as (
select queryid, calls, regexp_replace(full_query, E'[\\n\\r\\t\\u2028]+', ' ', 'gm' ) as full_query, foundedfulltable
,case when strpos(foundedfulltable,'.')>0 then (regexp_match(foundedfulltable, '([a-z_]{1,})\.{1}(?:[a-z0-9_]{1,})'))[1] else null end as foundedschema
,case when strpos(foundedfulltable,'.')>0 then (regexp_match(foundedfulltable, '(?:[a-z_]{1,})\.{1}([a-z0-9_]{1,})'))[1] else foundedfulltable end as foundedtable
from t1 where coalesce(foundedfulltable,'') != ''
)
-- select *, pg_typeof(foundedfulltable) from t2
, t3 as (
select pn.nspname as schemaname, pc.relname as objectname
from pg_catalog.pg_class pc
inner join pg_catalog.pg_namespace pn on pn.oid = pc.relnamespace
where relkind in ('r', 'v')
)

select distinct t2.calls, t3.*
from t2
inner join t3 on t3.schemaname = coalesce(t2.foundedschema,'public') and t3.objectname = t2.foundedtable
where not starts_with(t2.foundedtable, 'pg_')
order by 1 desc

Комментарии

Популярные сообщения из этого блога

Repeat Linux command every x seconds

watch -n 5 "ps -ef | grep COPY" watch -n 10 "psql -U postgres -c \"select clock_timestamp() - query_start as duration, substr(query,1,50) from pg_stat_activity where pid <> pg_backend_pid() and state='active' order by 1 desc\"" for i in {1..10}; do ps -ef | grep COPY; date ; sleep 5; done while true; do ps -ef | grep COPY ; date ; sleep 5; done