Goal Find base table columns with tsvector type in case of which it is not a generated column nor does the table has an associated trigger to calculate the tsvector value automatically.
Type Problem detection (Each row in the result could represent a flaw in the design)
Reliability Medium (Medium number of false-positive results)
License MIT License
Fixing Suggestion Use a trigger or a generated column to calculate the tsvector values automatically.
Data Source INFORMATION_SCHEMA+system catalog
SQL Query
WITH tsv_columns AS (SELECT table_schema, table_name, column_name, ordinal_position
FROM INFORMATION_SCHEMA.columns
WHERE (table_schema, table_name) IN (SELECT table_schema, table_name
FROM INFORMATION_SCHEMA.tables WHERE table_type='BASE TABLE') 
AND data_type='tsvector'
AND table_schema NOT IN (SELECT schema_name
FROM INFORMATION_SCHEMA.schemata
WHERE schema_name<>'public' AND
schema_owner='postgres' AND schema_name IS NOT NULL)),
tsv_columns_generated AS (SELECT table_schema, table_name, column_name
FROM INFORMATION_SCHEMA.columns
WHERE (table_schema, table_name) IN (SELECT table_schema, table_name
FROM INFORMATION_SCHEMA.tables WHERE table_type='BASE TABLE') 
AND data_type='tsvector'
AND is_generated='ALWAYS'
AND table_schema NOT IN (SELECT schema_name
FROM INFORMATION_SCHEMA.schemata
WHERE schema_name<>'public' AND
schema_owner='postgres' AND schema_name IS NOT NULL)),
tr AS (SELECT
    n.nspname AS trigger_schema,        
    c.relname AS trigger_table
   FROM pg_namespace n,
    pg_class c,
    pg_trigger t,
    pg_proc p,
    pg_namespace np
  WHERE n.oid = c.relnamespace 
  AND c.oid = t.tgrelid 
  AND t.tgfoid=p.oid
  AND p.pronamespace=np.oid
  AND NOT t.tgisinternal 
  AND NOT pg_is_other_temp_schema(n.oid)
  AND c.relkind IN ('r','p')
  AND p.proname~*'^tsvector_update_trigger')
SELECT table_schema, table_name, column_name
FROM tsv_columns AS tsv
WHERE NOT EXISTS (SELECT * 
FROM tsv_columns_generated AS tsvg
WHERE tsvg.table_schema=tsv.table_schema
AND tsvg.table_name=tsv.table_name
AND tsvg.column_name=tsv.column_name)
AND NOT EXISTS (SELECT *
FROM tr
WHERE tr.trigger_schema=tsv.table_schema
AND tr.trigger_table=tsv.table_name)
ORDER BY table_schema, table_name, ordinal_position;

Collections

This query belongs to the following collections:

NameDescription
Find problems automaticallyQueries, that results point to problems in the database. Each query in the collection produces an initial assessment. However, a human reviewer has the final say as to whether there is a problem or not .
Categories

This query is classified under the following categories:

NameDescription
Full text searchQueries of this category provide information about full text search
Generated columnsQueries of this category provide information about generated stored base table columns.
Triggers and rulesQueries of this category provide information about triggers and rules in a database.