Sources oF FUNCTION, PROCEDURE
SELECT T.*
FROM (SELECT N.NSPNAME AS SCHEMA_NAME
, P.PRONAME AS OBJECT_NAME
, PG_GET_FUNCTIONDEF(P.OID) AS SOURCE
FROM PG_CATALOG.PG_PROC P
JOIN PG_CATALOG.PG_NAMESPACE N ON N.OID = P.PRONAMESPACE
WHERE N.NSPNAME NOT IN ('information_schema', 'pg_catalog')
) T
WHERE T.SCHEMA_NAME IN('blog')
-- AND UPPER(T.OBJECT_NAME) LIKE '%' || UPPER('keyword') || '%'
AND UPPER(T.SOURCE) LIKE '%' || UPPER('keyword') || '%'
;
Sources of VIEW
SELECT T.*
FROM (SELECT V.SCHEMANAME AS SCHEMA_NAME
, V.VIEWNAME AS VIEW_NAME
, V.DEFINITION AS SOURCE
FROM PG_CATALOG.PG_VIEWS V
WHERE V.SCHEMANAME NOT IN ('information_schema', 'pg_catalog')
) T
WHERE T.SCHEMA_NAME IN('blog')
-- AND UPPER(T.VIEW_NAME) LIKE '%' || UPPER('keyword') || '%'
AND UPPER(T.SOURCE) LIKE '%' || UPPER('keyword') || '%'
;
Table's Column list
SELECT T.*
FROM (SELECT NSP.NSPNAME AS SCHEMA_NAME
--, TBL.OID
, TBL.RELNAME AS TABLE_NAME
--, OBJ_DESCRIPTION((NSP.NSPNAME || '.' || TBL.RELNAME)::REGCLASS, 'pg_class') AS TABLE_DESC
, COL.ATTNUM AS COLUMN_ID
--, COL.ATTRELID
, COL.ATTNAME AS COLUMN_NAME
, CMT.DESCRIPTION AS COLUMN_DESC
--, COL.ATTTYPID
, COL.ATTNOTNULL AS NOT_NULL
, FORMAT_TYPE(COL.ATTTYPID, COL.ATTTYPMOD) AS DATA_TYPE
, PG_GET_EXPR(DEF.ADBIN, DEF.ADRELID) AS DEFAULT_VALUE
FROM PG_CATALOG.PG_NAMESPACE NSP
JOIN PG_CATALOG.PG_CLASS TBL ON TBL.RELNAMESPACE = NSP.OID
JOIN PG_CATALOG.PG_ATTRIBUTE COL ON COL.ATTRELID = TBL.OID
AND COL.ATTNUM > 0
LEFT OUTER JOIN PG_CATALOG.PG_ATTRDEF DEF ON DEF.ADRELID = COL.ATTRELID
AND DEF.ADNUM = COL.ATTNUM
LEFT OUTER JOIN PG_CATALOG.PG_DESCRIPTION CMT ON CMT.OBJOID = COL.ATTRELID
AND CMT.OBJSUBID = COL.ATTNUM
) T
WHERE T.SCHEMA_NAME = 'public'
--AND T.TABLE_NAME LIKE 'blog%'
ORDER BY T.TABLE_NAME, T.COLUMN_ID
;
Constraints of Tables
SELECT T.TABLE_NAME
, T.CONSTRAINT_NANE
, CASE T.CONTYPE
WHEN 'p' THEN 'PRIMARY KEY'
WHEN 'u' THEN 'UNIQUE'
WHEN 'f' THEN 'FOREIGN KEY'
END AS CONSTRAINT_TYPE
, 'ALTER TABLE'
|| ' '
|| T.SCHEMA_NAME
|| '.'
|| T.TABLE_NAME
|| ' '
|| 'ADD CONSTRAINT'
|| ' '
|| T.CONSTRAINT_NANE
|| ' '
|| CASE T.CONTYPE
WHEN 'p' THEN 'PRIMARY KEY'
WHEN 'u' THEN 'UNIQUE'
WHEN 'f' THEN 'FOREIGN KEY'
END
|| ' ('
|| T.COLUMN_NAMES
|| ') '
|| CASE T.CONTYPE
WHEN 'p' THEN 'USING INDEX TABLESPACE tbs01;'
WHEN 'u' THEN 'USING INDEX TABLESPACE tbs01;'
WHEN 'f' THEN 'REFERENCES ' || T.SCHEMA_NAME || '.' || T.REF_TABLE_NAME || ' (' || T.REF_COLUMN_NAMES || ');'
END AS CREATE_SCRIPT
, 'ALTER TABLE'
|| ' '
|| T.SCHEMA_NAME
|| '.'
|| T.TABLE_NAME
|| ' '
|| 'DROP CONSTRAINT IF EXISTS'
|| ' '
|| T.CONSTRAINT_NANE
|| ';' AS DROP_SCRIPT
FROM (SELECT CST.CONNAMESPACE::REGNAMESPACE::TEXT AS SCHEMA_NAME
, CST.CONRELID::REGCLASS::TEXT AS TABLE_NAME
, CST.CONNAME AS CONSTRAINT_NANE
, CST.CONTYPE
--, CST.OID AS CON_OID
--, CST.CONRELID AS TABLE_OID
--, CST.CONINDID AS INDEX_OID
--, CST.CONKEY
--, CST.CONFKEY
, (SELECT ARRAY_TO_STRING(ARRAY_AGG(COL.ATTNAME), ', ')
FROM UNNEST(CST.CONKEY) AS CAC(ATT_OID)
JOIN PG_CATALOG.PG_ATTRIBUTE COL ON COL.ATTRELID = CST.CONRELID
AND COL.ATTNUM = CAC.ATT_OID)
AS COLUMN_NAMES
, CST.CONFRELID::REGCLASS::TEXT AS REF_TABLE_NAME
, (SELECT ARRAY_TO_STRING(ARRAY_AGG(COL.ATTNAME), ', ')
FROM UNNEST(CST.CONFKEY) AS CAC(ATT_OID)
JOIN PG_CATALOG.PG_ATTRIBUTE COL ON COL.ATTRELID = CST.CONFRELID
AND COL.ATTNUM = CAC.ATT_OID)
AS REF_COLUMN_NAMES
FROM PG_CATALOG.PG_CONSTRAINT CST
) T
WHERE T.SCHEMA_NAME = 'public'
AND T.CONTYPE IN ('p', 'u', 'f')
AND T.TABLE_NAME = 'blog_post'
;