Note
E'\\n'
→ E'\\\\n'
바꿔야할 수도 있음
FK 한 번에 확인하기
WITH CTE AS
(
SELECT
connamespace::regnamespace::TEXT AS SCHEMA_NAME,
conrelid::regclass::TEXT AS TABLE_NAME,
conname AS FK_NAME,
pg_get_constraintdef(oid) AS FK_DEF
FROM pg_constraint
WHERE contype = 'f'
ORDER BY 1, 2
)
SELECT TABLE_NAME, FK_NAME, FK_DEF
FROM CTE
WHERE SCHEMA_NAME = 'public'
AND TABLE_NAME SIMILAR TO '(pd|cb|td)%' -- FK 참조 주체 필터링
AND FK_DEF SIMILAR TO '%REFERENCES ' || '(pd|cb|td)%' -- FK 참조 대상 필터링
AND FK_DEF NOT SIMILAR TO '%REFERENCES ' || TABLE_NAME || '%' -- FK 자기 테이블 참조 필터링
;
응용 1 : FK 일괄 제거/재생성 쿼리 추출
응용 2 : 쿼리 버퍼로 추출하여 일괄 제거/재생성