Case insensitive uniqueness

From CarlosCaPe/octorato · View interactive version

Case insensitive uniqueness is an AI agent skill from the GitHub repository CarlosCaPe/octorato. Case-Insensitive Uniqueness (Functional Indexes)

How to use this skill

Skill file

Case-Insensitive Uniqueness (Functional Indexes)

Source: PostgreSQL Best Practices

-- Audit Finding #6 and Backlog #3

What

Creating a case-insensitive unique constraint on text columns (typically email)

using a functional B-tree index on lower(column) instead of the citext

extension.

Why

Email addresses Alice@Example.com and alice@example.com are the same

mailbox but different PostgreSQL text values. Without case-insensitive

uniqueness:

The decision to use lower() instead of citext:

How

Create the functional unique index

CREATE UNIQUE INDEX CONCURRENTLY IF NOT EXISTS uq_users__email_lower
    ON public."Users" (lower("Email"));

Query using the same expression

-- MUST use lower() in the query for the index to be used
SELECT "UserId", "Email"
FROM public."Users"
WHERE lower("Email") = lower('Alice@Example.com');

Idempotent pattern for scripts

DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM pg_indexes
        WHERE schemaname = 'public'
          AND indexname = 'uq_users__email_lower'
    ) THEN
        -- Must be outside DO block (CONCURRENTLY restriction)
        RAISE NOTICE '[--] Index uq_users__email_lower needs creation';
    ELSE
        RAISE NOTICE '[OK] Index uq_users__email_lower already exists';
    END IF;
END $$;

-- Outside DO block:
CREATE UNIQUE INDEX CONCURRENTLY IF NOT EXISTS uq_users__email_lower
    ON public."Users" (lower("Email"));

Check for existing duplicates BEFORE creating the unique index

-- This will fail if duplicates exist
SELECT lower("Email"), COUNT(*)
FROM public."Users"
GROUP BY lower("Email")
HAVING COUNT(*) > 1;

If duplicates are found, resolve them before creating the unique index.

lower() vs citext Comparison

| Aspect | lower() + B-tree | citext |

|--------|-------------------|----------|

| Extension needed | No | Yes (CREATE EXTENSION citext) |

| Column type change | No | Yes (ALTER COLUMN TYPE citext) |

| Query requirement | Must use lower() in WHERE | Transparent (implicit) |

| Index type | Functional B-tree | Standard B-tree |

| Collation awareness | ASCII only | Full locale support |

| Maintenance | Low | Low |

Our standard: Use lower() unless multiple tables need case-insensitive

behavior AND the lower() pattern creates significant code repetition.

When to Use

Where We Used It

Related Skills

References

-- Audit Finding #6 and Backlog #3

-- "Prevent case-variant duplicates in email identifiers"

Gotchas

by the planner -- WHERE "Email" = 'alice@...' will NOT use the index

duplicate key error -- always scan for duplicates first

eszett ss vs SS), consider citext or ICU collation

otherwise different-case values can be stored even though lookups match


Category: DDL | Origin: ,

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.