Goal 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:

  • 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., from VARCHAR(7) to VARCHAR(15)), routines with a hardcoded VARCHAR(7) variable will silently remain outdated and fail at runtime with a value 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.

Type Problem detection Each row in the result could represent a flaw in the design
Reliability High Few or no false-positive results
License MIT (opens in new tab)
Data Source INFORMATION_SCHEMA+system catalog
SQL Query
SELECT 
    n.nspname AS routine_schema,
    p.proname AS routine_name,
    pg_get_function_identity_arguments(p.oid) AS parameters,
    CASE 
        WHEN p.prokind = 'f' THEN 'FUNCTION'
        WHEN p.prokind = 'p' THEN 'PROCEDURE'
        WHEN p.prokind = 'w' THEN 'WINDOW FUNCTION' 
    END AS routine_type,
    regexp_replace(p.prosrc, '[\r\n]', '<br>', 'g') AS routine_src
FROM pg_catalog.pg_proc p
INNER JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace
WHERE p.prokind <> 'a'
  AND n.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 p.prosrc ~* '\yDECLARE\y(?:(?!\yBEGIN\y)[\s\S])*?\y(varchar|character[[:space:]]+varying|char|character)[[:space:]]*\([[:space:]]*[0-9]+[[:space:]]*\)'
ORDER BY routine_schema, routine_name, parameters;

Fixing Suggestion

If a local variable holds the value of a specific table column, declare it using an anchored type reference (e.g., v_code schema_name.table_name.column_name%TYPE;) so that the variable automatically adapts to future column type changes. For general-purpose string variables not tied to a single column, use the unbounded TEXT data type.

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 .

NameDescription
Find problems automaticallyQueries, 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:

Triggers and rules

Queries of this category provide information about triggers and rules in a database.

User-defined routines

Queries of this category provide information about the user-defined routines

NameDescription
Triggers and rulesQueries of this category provide information about triggers and rules in a database.
User-defined routinesQueries of this category provide information about the user-defined routines