Query goal: | Do not cause a potential performance bottleneck by having a shared resource. By having a shared sequence it is not possible to change properties of sequences of different tables independently (for instance the owner column or step), i.e., it increases coupling between tables. By having a shared sequence it is impossible to specify the owner (table column) to the sequence generator. |
Notes about the query: | In case of the string_agg function, the line break (br) tag is used as a part of the separator for the better readability in case the query result is displayed in a web browser. |
Query type: | Problem detection (Each row in the result could represent a flaw in the design) |
Query reliability: | Medium (Medium number of false-positive results) |
Query license: | MIT License |
Fixing suggestion: | Use for each table a separate sequence generator. |
Data source: | INFORMATION_SCHEMA only |
SQL query: | Click on query to copy it
WITH serial_columns AS (SELECT c.table_schema, t.table_type, c.table_name , c.column_name, coalesce(c.column_default, d.domain_default) AS column_default FROM information_schema.columns c LEFT JOIN information_schema.domains d USING (domain_schema, domain_name) INNER JOIN information_schema.schemata s ON c.table_schema=s.schema_name INNER JOIN information_schema.tables t USING (table_schema, table_name) WHERE coalesce (c.column_default, d.domain_default) LIKE 'nextval%' AND (c.table_schema = 'public' OR s.schema_owner<>'postgres')), sequences AS (SELECT sc.table_schema, sc.table_type, sc.table_name , sc.column_name, s.sequence_schema, s.sequence_name FROM serial_columns AS sc, information_schema.sequences AS s WHERE column_default ILIKE '%' || 'nextval(''' || s.sequence_name || '''::regclass)' || '%' OR column_default='nextval(''' || s.sequence_schema || '.' || s.sequence_name || '''::regclass)') SELECT sequence_schema, sequence_name, Count(*) AS number_of_uses, string_agg(table_type || ': ' || table_schema || '.' || table_name || '.' || column_name, ';<br>' ORDER BY table_schema, table_name) AS columns FROM sequences GROUP BY sequence_schema, sequence_name HAVING Count(*)>1 ORDER BY sequence_schema, sequence_name; |
Collection name | Collection description |
---|---|
Find problems automatically | Queries, 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 . |
Category name | Category description |
---|---|
Comfortability of data management | Queries of this category provide information about the means that have been used to make the use or management of database more comfortable and thus, more efficient. |
Default value | Queries of this catergory provide information about the use of default values. |
Performance | Queries of this category provide information about indexes in a database. |
Sequence generators | Queries of this category provide information about sequence generators and their usage. |
Reference |
---|
https://dba.stackexchange.com/questions/193788/what-can-go-wrong-using-the-same-sequence-across-multiple-tables-in-postgres |