| Goal | 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:
|
|---|---|
| Notes | Queries the PostgreSQL system catalogs (pg_proc and pg_namespace) to inspect the decompiled source code (pg_get_functiondef) of user-defined functions and procedures (prokind IN ('f', 'p')). The query uses a case-insensitive regular expression with a backreference (\1) to capture the target table name in an INSERT INTO <table> ...; statement and detect whether the immediately following UPDATE statement performs an EXISTS (SELECT ... FROM <same_table>) subquery against the identical table \1. For better readability in a web browser, < characters are escaped and newline characters are replaced with HTML line break (<br>) tags. Routine parameters are included alongside the schema and routine name to uniquely identify overloaded routines. |
| 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 (opens in new tab) |
| Data Source | INFORMATION_SCHEMA+system catalog |
| SQL Query |
|
Fixing Suggestion
Combine the two statements into a single atomic statement using a data-modifying Common Table Expression (
WITH clause) with RETURNING. Capture the key of the inserted row in the CTE (e.g., WITH inserted AS (INSERT INTO child_table ... RETURNING parent_id)) and filter the outer UPDATE statement using the CTE result (UPDATE parent_table SET ... WHERE parent_id IN (SELECT parent_id FROM inserted)). This eliminates the redundant table lookup and ensures that the parent table is updated if and only if a new row was actually inserted. Collections
This query belongs to the following collections:
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 .
| Name | 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 . |
Categories
This query is classified under the following categories:
User-defined routines
Queries of this category provide information about the user-defined routines
| Name | Description |
|---|---|
| User-defined routines | Queries of this category provide information about the user-defined routines |