\pset null (null)
with 
d(logical_partition_key, model_usage) as ( values( 'TEST_DATA' ,'F(T61)(EXPORT)' )
),
usg_txt as (
 SELECT DISTINCT logical_partition_key,
      MODEL_USAGE as usage_text,
      REPLACE ( REPLACE ( REPLACE ( REPLACE ( PIU.MODEL_USAGE::text, '<t context="USAGE_TEXT">', ''), '<t context="FCN_NAME_MODFR">', ''), '<t context="FCN_USAGE_MODFR">', ''), '</t>', '') AS txt
    FROM d /*CAO_CALLOUT_GHOST_COMB_W*/ piu
    )
,
parse( logical_partition_key, usage_text,txt, rpo_txt, indx ) as 
(
select  d.logical_partition_key, d.usage_text,d.txt, coalesce(a.rpo[1],a.rpo[2]) as rpo_txt, a.pos 
from usg_txt d
     left join lateral regexp_matches( txt, '([ ,\-()/&])|([^ ,\-()/&]+)','g') with ordinality as a(rpo,pos) on true
)
, 
prv_nxt_token( logical_partition_key, usage_text,txt, rpo_txt, indx, mx_indx, prev,nxt,nxt2, prv2,prv3) as 
(     
/* Get prior and next token to support later logic */
select  p.logical_partition_key, p.usage_text,
       p.txt, p.rpo_txt, indx,
       max( indx) over ( partition by p.txt ) mx_indx,
       lag( p.rpo_txt,1 ) over ( partition by p.logical_partition_key,p.txt order by indx ) prev,
       lag( p.rpo_txt,-1 ) over ( partition by p.logical_partition_key,p.txt order by indx ) nxt,
       lag( p.rpo_txt,-2) over ( partition by p.logical_partition_key,p.txt order by indx ) nxt2,															   
       lag( p.rpo_txt,2 ) over ( partition by p.logical_partition_key,p.txt order by indx ) prv2,
       lag( p.rpo_txt,3 ) over ( partition by p.logical_partition_key,p.txt order by indx ) prv3
 from parse p 
 )
 select * from prv_nxt_token where logical_partition_key='TEST_DATA' and  usage_text='F(T61)(EXPORT)';

explain analyze with 
d(logical_partition_key, model_usage) as ( values( 'TEST_DATA' ,'F(T61)(EXPORT)' )
),
usg_txt as (
 SELECT DISTINCT logical_partition_key,
      MODEL_USAGE as usage_text,
      REPLACE ( REPLACE ( REPLACE ( REPLACE ( PIU.MODEL_USAGE::text, '<t context="USAGE_TEXT">', ''), '<t context="FCN_NAME_MODFR">', ''), '<t context="FCN_USAGE_MODFR">', ''), '</t>', '') AS txt
    FROM d /*CAO_CALLOUT_GHOST_COMB_W*/ piu
    )
,
parse( logical_partition_key, usage_text,txt, rpo_txt, indx ) as 
(
select  d.logical_partition_key, d.usage_text,d.txt, coalesce(a.rpo[1],a.rpo[2]) as rpo_txt, a.pos 
from usg_txt d
     left join lateral regexp_matches( txt, '([ ,\-()/&])|([^ ,\-()/&]+)','g') with ordinality as a(rpo,pos) on true
)
, 
prv_nxt_token( logical_partition_key, usage_text,txt, rpo_txt, indx, mx_indx, prev,nxt,nxt2, prv2,prv3) as 
(     
/* Get prior and next token to support later logic */
select  p.logical_partition_key, p.usage_text,
       p.txt, p.rpo_txt, indx,
       max( indx) over ( partition by p.txt ) mx_indx,
       lag( p.rpo_txt,1 ) over ( partition by p.logical_partition_key,p.txt order by indx ) prev,
       lag( p.rpo_txt,-1 ) over ( partition by p.logical_partition_key,p.txt order by indx ) nxt,
       lag( p.rpo_txt,-2) over ( partition by p.logical_partition_key,p.txt order by indx ) nxt2,
       lag( p.rpo_txt,2 ) over ( partition by p.logical_partition_key,p.txt order by indx ) prv2,
       lag( p.rpo_txt,3 ) over ( partition by p.logical_partition_key,p.txt order by indx ) prv3
 from parse p 
 )
 select * from prv_nxt_token where logical_partition_key='TEST_DATA' and  usage_text='F(T61)(EXPORT)';
 
select version();
select * from pg_settings;
