Filter Queries
- All the queries about database objects contain a subcondition to exclude from the result information about the system catalog.
- Although the statements use SQL constructs (common table expressions; NOT in subqueries) that could cause performance problems in case of large datasets, it shouldn't be a problem in case of the relatively small amount of data in the system catalog of a database.
#801. Regular expression with possibly a LIKE pattern
INFORMATION_SCHEMA+system catalog base tablesFind expressions that use a regular expression with a like predicate pattern.
#802. Roles with revoked PUBLIC execute privileges on routines
system catalog base tables onlyThis query identifies database roles that have explicitly revoked the default EXECUTE privilege from PUBLIC for newly created routines. By default, PostgreSQL automatically grants the EXECUTE privilege to PUBLIC (all users) whenever a new function or procedure is created. Revoking this default behavior is a critical security best practice, ensuring that access to potentially sensitive database logic must be explicitly granted.
#803. Routine body has keywords that are not in uppercase
INFORMATION_SCHEMA+system catalog base tablesKeywords in uppercase improve readability.
#804. Routine body only in uppercase
INFORMATION_SCHEMA+system catalog base tablesUppercase means screaming and having code entirely in uppercase makes its reading more difficult. On the other hand, it would be a good idea to have keywords in uppercase. Find routines that body contains a SQL data manipulation statement (which shouldn't be entirely in uppercase) but still the body is completely in uppercase.
#805. Routine body with ordering the query result based on positional references
INFORMATION_SCHEMA+system catalog base tablesFind routines where the query result is sorted based on the column number in the SELECT clause. Such query is sensitive towards changing the order of columns in the SELECT clause, i.e., if one changes the order of columns in the SELECT clause, then one must change the numbers in the ORDER BY clause as well, otherwise the query will produce undesired order of rows.
#806. Routine for reading data uses another routine to read some data
INFORMATION_SCHEMA+system catalog base tablesThis query analyzes the call graph of user-defined routines to identify nested data retrieval patterns. It flags routines that are operationally read-only (performing no DML) but rely on invoking other routines to access additional data. This indicates a layered architecture where data access logic is encapsulated and chained. Identifying these routines is essential for performance profiling, as the total cost of execution is distributed across the call stack rather than being contained within a single procedure body.
#807. Routines acquiring multi-row locks (FOR [NO KEY] UPDATE or FOR [KEY] SHARE) without an ORDER BY clause
INFORMATION_SCHEMA+system catalog base tablesThis query identifies user-defined SQL and PL/pgSQL routines (functions, procedures, and trigger functions) that execute a SELECT or PERFORM query with a row-level locking clause (FOR UPDATE, FOR NO KEY UPDATE, FOR SHARE, or FOR KEY SHARE) capable of locking multiple rows (or using LIMIT / FETCH FIRST ... ROWS), but omit an ORDER BY clause.
When a query locks multiple rows without a deterministic ORDER BY clause, PostgreSQL acquires row locks in the order tuples are visited by the execution plan (such as physical heap order or index scan order). Because tuple positions and access paths can vary across concurrent transactions, two transactions locking overlapping sets of rows can acquire locks in opposite orders, resulting in a deadlock. Although FOR SHARE locks are mutually compatible with other FOR SHARE locks, they conflict with write locks (UPDATE, DELETE, FOR [NO KEY] UPDATE); therefore, a multi-row FOR SHARE query can deadlock with a concurrent transaction that modifies the same rows unless both transactions acquire locks in a consistent order (e.g., ascending primary key order). Furthermore, using LIMIT or FETCH FIRST/NEXT with a locking clause without ORDER BY locks an arbitrary subset of rows. Queries that lock at most a single row (such as equality lookups on a PRIMARY KEY or UNIQUE index, or EXISTS (...) subqueries) cannot deadlock due to intra-query row ordering and are excluded.
#808. Routines declaring local variables with hardcoded length-constrained character types instead of %TYPE or TEXT
INFORMATION_SCHEMA+system catalog base tablesThis query identifies user-defined PL/pgSQL routines that declare local variables in their DECLARE block using hardcoded length-constrained character types (VARCHAR(n), CHARACTER VARYING(n), CHAR(n), or CHARACTER(n)). Hardcoding character length limits on local variables in procedural code is a maintainability anti-pattern for two main reasons:
- Schema evolution fragility: Local variables frequently store values fetched from or compared against table columns (such as entity codes or names). If a table column's maximum length is later increased via
ALTER TABLE(e.g., fromVARCHAR(7)toVARCHAR(15)), routines with a hardcodedVARCHAR(7)variable will silently remain outdated and fail at runtime with avalue too long for type character varying(7)error whenever longer values are processed. - Redundant runtime overhead: While length limits on table columns enforce persistent data integrity, applying arbitrary length constraints to temporary in-memory variables rarely serves a business purpose and introduces unnecessary length-validation checks during routine execution.
#809. Routines executing a separate INSERT followed by an UPDATE with a redundant EXISTS check on the inserted table instead of using a data-modifying CTE (INSERT ... RETURNING)
INFORMATION_SCHEMA+system catalog base tablesThis query identifies user-defined routines that execute a standalone INSERT INTO <table> statement and immediately follow it with an UPDATE statement that verifies the existence of the newly inserted row via EXISTS (SELECT ... FROM <same_table>), rather than chaining the operations atomically within a single statement using a data-modifying Common Table Expression (WITH ... AS (INSERT INTO ... RETURNING ...) UPDATE ...). Splitting coupled data modifications into a standalone INSERT followed by an UPDATE ... WHERE EXISTS (...) on the target table introduces both performance overhead and potential logic flaws:
- Redundant index scan and locking: Instead of passing the newly inserted row's key directly in memory via the
RETURNINGclause of a CTE, the database is forced to perform an additional index lookup (and often an extraFOR SHARElock) on the table into which it just inserted the row. - Flawed conditional execution: If the
INSERT ... SELECTstatement inserts zero rows (for example, because a referenced classifier is inactive or anON CONFLICT DO NOTHINGclause is triggered), but a matching row already existed in the table from a prior operation, the subsequentWHERE EXISTS (SELECT 1 FROM <table> ...)still evaluates toTRUEand erroneously updates the parent entity's audit fields (last_modified_by,last_modified_time).
#810. Routines lacking explicit locking (MVCC risks)
INFORMATION_SCHEMA+system catalog base tablesThis query identifies user-defined routines that may be susceptible to concurrency anomalies due to a lack of explicit locking. PostgreSQL utilizes Multi-Version Concurrency Control (MVCC), where SELECT statements do not block data modification operations. Consequently, routines that read data to inform subsequent modifications without acquiring row-level locks (e.g., FOR UPDATE, FOR SHARE) or using isolation levels higher than READ COMMITTED are prone to race conditions. This query flags such routines for review to ensure transactional integrity is maintained.
#811. Routines redundantly updating a modification time column that is already maintained by a BEFORE UPDATE trigger
INFORMATION_SCHEMA+system catalog base tablesThis query identifies user-defined routines (functions and procedures) that explicitly assign a value to a table's modification timestamp column in an UPDATE ... SET statement when the target table already has a row-level BEFORE UPDATE trigger that automatically updates that same column. Maintaining the modification timestamp in both the data-modifying routine and a BEFORE UPDATE trigger violates the Don't Repeat Yourself (DRY) principle and creates architectural ambiguity:
- Redundant and overwritten assignments: Because a
BEFORE UPDATEtrigger executes after theSETexpressions of theUPDATEstatement are evaluated, an unconditional assignment in the trigger (e.g.,NEW.updated_at := CURRENT_TIMESTAMP(0);) silently overwrites whatever timestamp the routine explicitly computed and passed in theSETclause. - Inconsistent timestamp semantics: If the routine uses one timestamp function (e.g.,
statement_timestamp()) while the trigger uses another (e.g.,CURRENT_TIMESTAMPorclock_timestamp()), developers reading the routine code are misled about the actual value persisted in the table.
#812. Routines that can be invoked with a variable number of arguments
INFORMATION_SCHEMA+system catalog base tablesFind routines with a VARIADIC parameter. These are routines that take as input an undefined number of arguments where the argument that is an undefined number are all of the same type and are the last input arguments.
#813. Routines that use old syntax for limiting rows
INFORMATION_SCHEMA+system catalog base tablesThis query identifies PL/pgSQL and SQL routines with no SQL-standard bodies that use the non-standard LIMIT clause for row limitation. It flags these routines because the official, cross-platform SQL standard specifies FETCH FIRST n ROWS ONLY for this purpose. Adhering to the standard improves code portability and maintainability. To ensure relevance, the query intelligently excludes routines that are part of installed extensions, focusing only on user-defined code.
#814. Routines with a redundant ORDER BY clause in single-row locking queries (FOR [NO KEY] UPDATE or FOR [KEY] SHARE)
INFORMATION_SCHEMA+system catalog base tablesThis query identifies user-defined SQL and PL/pgSQL routines (functions, procedures, and trigger functions) that execute a SELECT or PERFORM query with a row-level locking clause (FOR UPDATE, FOR NO KEY UPDATE, FOR SHARE, or FOR KEY SHARE) and include an unnecessary ORDER BY clause when the query can lock at most a single row.
While deterministic row ordering via ORDER BY is essential for preventing deadlocks when a query locks multiple rows, adding ORDER BY to a locking query is completely redundant in two cases:
- Single-row primary or unique key lookups: When the
WHEREclause constrains all columns of aPRIMARY KEYorUNIQUEindex using simple equality (=) withoutOR,IN (...),= ANY (...), range operators, orJOINs, the query is mathematically guaranteed to return at most one row (0 or 1 row). A single row has only one possible lock acquisition order and cannot cause an intra-query ordering deadlock; addingORDER BYmerely clutters the code and can prevent optimizer shortcuts or introduce unnecessary sort overhead if the sort column differs from the lookup index. EXISTS (SELECT ... ORDER BY ... FOR ...)subqueries withoutLIMIT/FETCH: AnEXISTSsubquery halts execution and locks only the very first matching row. Including anORDER BYclause inside anEXISTSsubquery without a row-limiting clause forces PostgreSQL to fetch and sort all candidate rows before locking and testing the first row.
#815. Routines with BOOLEAN return type that do not have a good name
INFORMATION_SCHEMA+system catalog base tablesThis query audits the naming conventions of routines that return a BOOLEAN data type. It identifies routines that do not adhere to the recommended semantic prefix convention (i.e., starting with is_, has_, can_, or on_). A function name should represent a state or a question (e.g., has_rights) rather than an action (e.g., check_rights). Enforcing this standard makes the routine's purpose and return value immediately obvious from its name.
#816. Routines with INSERT statements that are sensitive towards the order of columns
INFORMATION_SCHEMA+system catalog base tablesINSERT statements shouldn't be sensitive towards the order of columns. If one changes the order of columns in a table then these statements must be rewritten. Otherwise the code will not work or works incorrectly.
#817. Routines with non-deterministic side effects and static return values
INFORMATION_SCHEMA+system catalog base tablesThis query identifies SQL routines that exhibit a dangerous combination of state-changing side effects (DML) and a static return value (either a constant literal or an unmodified input parameter). The function's name and signature often imply that the return value is the result of its operations (e.g., a new balance, a generated ID). However, the static return value contradicts this, creating a semantic disconnect between the routine's name and its contract. This is a significant design flaw that can lead to subtle but critical bugs, as the calling code may act on a return value that does not accurately reflect the database state after the routine's execution.
#818. Routines without an action
INFORMATION_SCHEMA+system catalog base tablesFind routines that body does not contain any action.
#819. Routines with partial row locking (FOR SHARE/UPDATE OF) in multi-table JOIN queries
INFORMATION_SCHEMA+system catalog base tablesThis query identifies user-defined routines containing SELECT queries that join multiple tables (FROM (table_a JOIN table_b ...)), but restrict row-level locking to only a single table alias via FOR SHARE OF <single_table> or FOR UPDATE OF <single_table>.
When a query joins multiple tables (for example, a supertype/subtype relationship such as isik and tootaja) and filters rows using attributes from one table (such as WHERE lower(i.e_post) = ...) while locking only the other table (FOR SHARE OF t), the unlocked table remains vulnerable to concurrent modifications. Under PostgreSQL's default READ COMMITTED isolation level, a concurrent transaction can modify the unlocked table's attributes (such as changing the email address or deactivating the person record) before the current transaction completes, leading to race conditions and data inconsistencies.
#820. Routines with SQL-standard body that update a table without updating its modification time column
INFORMATION_SCHEMA+system catalog base tablesThis query identifies user-defined routines with an SQL-standard body (BEGIN ATOMIC ... END) that execute an UPDATE statement on a base table containing a modification timestamp or date column, where neither the routine itself nor any row-level BEFORE UPDATE trigger on the target table updates that modification time column. Failing to maintain the modification timestamp during row updates compromises audit trails, data lineage tracking, and synchronization or caching mechanisms that depend on accurate last-modified timestamps.
| # | Name(sorted ascending, activate to sort descending) | Goal(activate to sort ascending) | Type(activate to sort ascending) | Data source(activate to sort ascending) | Last update(activate to sort ascending) | License | Actions |
|---|---|---|---|---|---|---|---|
| 801 | Regular expression with possibly a LIKE pattern | Find expressions that use a regular expression with a like predicate pattern. | Problem detection | INFORMATION_SCHEMA+system catalog base tables | MIT (opens in new tab) | View | |
| 802 | Roles with revoked PUBLIC execute privileges on routines | This query identifies database roles that have explicitly revoked the default EXECUTE privilege from PUBLIC for newly created routines. By default, PostgreSQL automatically grants the EXECUTE privilege to PUBLIC (all users) whenever a new function or procedure is created. Revoking this default behavior is a critical security best practice, ensuring that access to potentially sensitive database logic must be explicitly granted. | General | system catalog base tables only | MIT (opens in new tab) | View | |
| 803 | Routine body has keywords that are not in uppercase | Keywords in uppercase improve readability. | Problem detection | INFORMATION_SCHEMA+system catalog base tables | MIT (opens in new tab) | View | |
| 804 | Routine body only in uppercase | Uppercase means screaming and having code entirely in uppercase makes its reading more difficult. On the other hand, it would be a good idea to have keywords in uppercase. Find routines that body contains a SQL data manipulation statement (which shouldn't be entirely in uppercase) but still the body is completely in uppercase. | Problem detection | INFORMATION_SCHEMA+system catalog base tables | MIT (opens in new tab) | View | |
| 805 | Routine body with ordering the query result based on positional references | Find routines where the query result is sorted based on the column number in the SELECT clause. Such query is sensitive towards changing the order of columns in the SELECT clause, i.e., if one changes the order of columns in the SELECT clause, then one must change the numbers in the ORDER BY clause as well, otherwise the query will produce undesired order of rows. | Problem detection | INFORMATION_SCHEMA+system catalog base tables | MIT (opens in new tab) | View | |
| 806 | Routine for reading data uses another routine to read some data | This query analyzes the call graph of user-defined routines to identify nested data retrieval patterns. It flags routines that are operationally read-only (performing no DML) but rely on invoking other routines to access additional data. This indicates a layered architecture where data access logic is encapsulated and chained. Identifying these routines is essential for performance profiling, as the total cost of execution is distributed across the call stack rather than being contained within a single procedure body. | Problem detection | INFORMATION_SCHEMA+system catalog base tables | MIT (opens in new tab) | View | |
| 807 | Routines acquiring multi-row locks (FOR [NO KEY] UPDATE or FOR [KEY] SHARE) without an ORDER BY clause | This query identifies user-defined SQL and PL/pgSQL routines (functions, procedures, and trigger functions) that execute a SELECT or PERFORM query with a row-level locking clause (FOR UPDATE, FOR NO KEY UPDATE, FOR SHARE, or FOR KEY SHARE) capable of locking multiple rows (or using LIMIT / FETCH FIRST ... ROWS), but omit an ORDER BY clause.When a query locks multiple rows without a deterministic ORDER BY clause, PostgreSQL acquires row locks in the order tuples are visited by the execution plan (such as physical heap order or index scan order). Because tuple positions and access paths can vary across concurrent transactions, two transactions locking overlapping sets of rows can acquire locks in opposite orders, resulting in a deadlock. Although FOR SHARE locks are mutually compatible with other FOR SHARE locks, they conflict with write locks (UPDATE, DELETE, FOR [NO KEY] UPDATE); therefore, a multi-row FOR SHARE query can deadlock with a concurrent transaction that modifies the same rows unless both transactions acquire locks in a consistent order (e.g., ascending primary key order). Furthermore, using LIMIT or FETCH FIRST/NEXT with a locking clause without ORDER BY locks an arbitrary subset of rows. Queries that lock at most a single row (such as equality lookups on a PRIMARY KEY or UNIQUE index, or EXISTS (...) subqueries) cannot deadlock due to intra-query row ordering and are excluded. |
Problem detection | INFORMATION_SCHEMA+system catalog base tables | MIT (opens in new tab) | View | |
| 808 | Routines declaring local variables with hardcoded length-constrained character types instead of %TYPE or TEXT | This query identifies user-defined PL/pgSQL routines that declare local variables in their DECLARE block using hardcoded length-constrained character types (VARCHAR(n), CHARACTER VARYING(n), CHAR(n), or CHARACTER(n)). Hardcoding character length limits on local variables in procedural code is a maintainability anti-pattern for two main reasons:
|
Problem detection | INFORMATION_SCHEMA+system catalog base tables | MIT (opens in new tab) | View | |
| 809 | Routines executing a separate INSERT followed by an UPDATE with a redundant EXISTS check on the inserted table instead of using a data-modifying CTE (INSERT ... RETURNING) | This query identifies user-defined routines that execute a standalone INSERT INTO <table> statement and immediately follow it with an UPDATE statement that verifies the existence of the newly inserted row via EXISTS (SELECT ... FROM <same_table>), rather than chaining the operations atomically within a single statement using a data-modifying Common Table Expression (WITH ... AS (INSERT INTO ... RETURNING ...) UPDATE ...). Splitting coupled data modifications into a standalone INSERT followed by an UPDATE ... WHERE EXISTS (...) on the target table introduces both performance overhead and potential logic flaws:
|
Problem detection | INFORMATION_SCHEMA+system catalog base tables | MIT (opens in new tab) | View | |
| 810 | Routines lacking explicit locking (MVCC risks) | This query identifies user-defined routines that may be susceptible to concurrency anomalies due to a lack of explicit locking. PostgreSQL utilizes Multi-Version Concurrency Control (MVCC), where SELECT statements do not block data modification operations. Consequently, routines that read data to inform subsequent modifications without acquiring row-level locks (e.g., FOR UPDATE, FOR SHARE) or using isolation levels higher than READ COMMITTED are prone to race conditions. This query flags such routines for review to ensure transactional integrity is maintained. | Problem detection | INFORMATION_SCHEMA+system catalog base tables | MIT (opens in new tab) | View | |
| 811 | Routines redundantly updating a modification time column that is already maintained by a BEFORE UPDATE trigger | This query identifies user-defined routines (functions and procedures) that explicitly assign a value to a table's modification timestamp column in an UPDATE ... SET statement when the target table already has a row-level BEFORE UPDATE trigger that automatically updates that same column. Maintaining the modification timestamp in both the data-modifying routine and a BEFORE UPDATE trigger violates the Don't Repeat Yourself (DRY) principle and creates architectural ambiguity:
|
Problem detection | INFORMATION_SCHEMA+system catalog base tables | MIT (opens in new tab) | View | |
| 812 | Routines that can be invoked with a variable number of arguments | Find routines with a VARIADIC parameter. These are routines that take as input an undefined number of arguments where the argument that is an undefined number are all of the same type and are the last input arguments. | General | INFORMATION_SCHEMA+system catalog base tables | MIT (opens in new tab) | View | |
| 813 | Routines that use old syntax for limiting rows | This query identifies PL/pgSQL and SQL routines with no SQL-standard bodies that use the non-standard LIMIT clause for row limitation. It flags these routines because the official, cross-platform SQL standard specifies FETCH FIRST n ROWS ONLY for this purpose. Adhering to the standard improves code portability and maintainability. To ensure relevance, the query intelligently excludes routines that are part of installed extensions, focusing only on user-defined code. | Problem detection | INFORMATION_SCHEMA+system catalog base tables | MIT (opens in new tab) | View | |
| 814 | Routines with a redundant ORDER BY clause in single-row locking queries (FOR [NO KEY] UPDATE or FOR [KEY] SHARE) | This query identifies user-defined SQL and PL/pgSQL routines (functions, procedures, and trigger functions) that execute a SELECT or PERFORM query with a row-level locking clause (FOR UPDATE, FOR NO KEY UPDATE, FOR SHARE, or FOR KEY SHARE) and include an unnecessary ORDER BY clause when the query can lock at most a single row.While deterministic row ordering via ORDER BY is essential for preventing deadlocks when a query locks multiple rows, adding ORDER BY to a locking query is completely redundant in two cases:
|
Problem detection | INFORMATION_SCHEMA+system catalog base tables | MIT (opens in new tab) | View | |
| 815 | Routines with BOOLEAN return type that do not have a good name | This query audits the naming conventions of routines that return a BOOLEAN data type. It identifies routines that do not adhere to the recommended semantic prefix convention (i.e., starting with is_, has_, can_, or on_). A function name should represent a state or a question (e.g., has_rights) rather than an action (e.g., check_rights). Enforcing this standard makes the routine's purpose and return value immediately obvious from its name. | Problem detection | INFORMATION_SCHEMA+system catalog base tables | MIT (opens in new tab) | View | |
| 816 | Routines with INSERT statements that are sensitive towards the order of columns | INSERT statements shouldn't be sensitive towards the order of columns. If one changes the order of columns in a table then these statements must be rewritten. Otherwise the code will not work or works incorrectly. | Problem detection | INFORMATION_SCHEMA+system catalog base tables | MIT (opens in new tab) | View | |
| 817 | Routines with non-deterministic side effects and static return values | This query identifies SQL routines that exhibit a dangerous combination of state-changing side effects (DML) and a static return value (either a constant literal or an unmodified input parameter). The function's name and signature often imply that the return value is the result of its operations (e.g., a new balance, a generated ID). However, the static return value contradicts this, creating a semantic disconnect between the routine's name and its contract. This is a significant design flaw that can lead to subtle but critical bugs, as the calling code may act on a return value that does not accurately reflect the database state after the routine's execution. | Problem detection | INFORMATION_SCHEMA+system catalog base tables | MIT (opens in new tab) | View | |
| 818 | Routines without an action | Find routines that body does not contain any action. | Problem detection | INFORMATION_SCHEMA+system catalog base tables | MIT (opens in new tab) | View | |
| 819 | Routines with partial row locking (FOR SHARE/UPDATE OF) in multi-table JOIN queries | This query identifies user-defined routines containing SELECT queries that join multiple tables (FROM (table_a JOIN table_b ...)), but restrict row-level locking to only a single table alias via FOR SHARE OF <single_table> or FOR UPDATE OF <single_table>.
|
Problem detection | INFORMATION_SCHEMA+system catalog base tables | MIT (opens in new tab) | View | |
| 820 | Routines with SQL-standard body that update a table without updating its modification time column | This query identifies user-defined routines with an SQL-standard body (BEGIN ATOMIC ... END) that execute an UPDATE statement on a base table containing a modification timestamp or date column, where neither the routine itself nor any row-level BEFORE UPDATE trigger on the target table updates that modification time column. Failing to maintain the modification timestamp during row updates compromises audit trails, data lineage tracking, and synchronization or caching mechanisms that depend on accurate last-modified timestamps. |
Problem detection | INFORMATION_SCHEMA+system catalog base tables | MIT (opens in new tab) | View |