Goal Find the number of views and materialized views that have a column with the xid type and the number of routines that contain a UPDATE or a DELETE statement that search condition refers to the xmin column. If one uses optimistic approach for dealing with the concurrent modifications of data, then xmin values should be presented by views and used in routines that modify or delete rows.
Notes The query does not consider the routines that are a part of an extension.
Type Sofware measure (Numeric values (software measures) about the database)
License MIT License
Data Source INFORMATION_SCHEMA+system catalog
SQL Query
WITH routine_count AS (SELECT Count(*) AS number_of_routines_that_use_xmin
FROM 
  pg_catalog.pg_proc, 
  pg_catalog.pg_namespace
WHERE 
  pg_proc.pronamespace = pg_namespace.oid 
  AND pg_proc.prokind<>'a'
  AND pg_get_functiondef(pg_proc.oid)~*'[[:space:]](UPDATE|DELETE)[[:space:]][^;]*[[:space:]]WHERE[[:space:]][^;]*[[:space:]]xmin[[:space:]]*=[[:space:]]*' 
  AND pg_namespace.nspname NOT IN (SELECT schema_name
FROM INFORMATION_SCHEMA.schemata
WHERE schema_name<>'public' AND
schema_owner='postgres' AND schema_name IS NOT NULL)
AND NOT EXISTS (SELECT 1
FROM pg_catalog.pg_depend d 
WHERE EXISTS (SELECT 1 FROM pg_catalog.pg_extension e WHERE d.refobjid=e.oid) AND
d.objid=pg_proc.oid)),
view_count AS (SELECT
Count(*) AS number_of_views_that_provide_xmin_column
FROM (SELECT 
table_schema, 
table_name
FROM 
information_schema.views AS v
WHERE 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)
AND EXISTS (SELECT 1
FROM information_schema.columns AS c
WHERE v.table_schema=c.table_schema AND v.table_name=c.table_name AND c.data_type='xid')
UNION SELECT nspname, relname
FROM pg_class INNER JOIN pg_namespace ON pg_class.relnamespace=pg_namespace.oid
INNER JOIN pg_attribute ON pg_class.oid=pg_attribute.attrelid INNER JOIN pg_type ON
pg_attribute.atttypid=pg_type.oid
WHERE relkind = 'm' AND attnum>=1 AND attisdropped='f' AND typname='xid') AS foo)
SELECT number_of_routines_that_use_xmin, number_of_views_that_provide_xmin_column
FROM routine_count, view_count
WHERE number_of_routines_that_use_xmin>0 OR number_of_views_that_provide_xmin_column>0;

Collections

This query belongs to the following collections:

NameDescription
Find problems by overviewQueries that results point to different aspects of database that might have problems. A human reviewer has to decide based on the results as to whether there are problems or not .
Categories

This query is classified under the following categories:

NameDescription
Concurrency controlQueries of this category provide information about concurrency control.
Derived tablesQueries of this category provide information about the derived tables (views, materialized views), which are used to implement virtual data layer.
User-defined routinesQueries of this category provide information about the user-defined routines