The list of all the queries

The use of xmin hidden column in views and routines

Query 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 about the query: The query does not consider the routines that are a part of an extension.
Query type: Sofware measure (Numeric values (software measures) about the database)
Query license: MIT License
Data source: INFORMATION_SCHEMA+system catalog
SQL query: Click on query to copy it

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 where the query belongs to

Collection nameCollection description
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 where the query belongs to

Category nameCategory description
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

Reference materials for further reading

Reference
https://momjian.us/main/blogs/pgblog/2017.html#October_4_2017
https://vladmihalcea.com/a-beginners-guide-to-database-locking-and-the-lost-update-phenomena/
https://www.2ndquadrant.com/en/blog/postgresql-anti-patterns-read-modify-write-cycles/
https://devcenter.heroku.com/articles/postgresql-concurrency

The list of all the queries