Skip to content

Conversation

NikolayS
Copy link
Owner

Summary

Fixes the "operator is not unique" error in the i3 (foreign keys with missing or bad indexes) check by adding explicit type casting to the key_cols parameter.

Problem

Users reported encountering this error when running the i3 check:

ERROR:  operator is not unique: smallint[] pg_catalog.@> smallint[]
HINT:  Could not choose a best candidate operator. You might need to add explicit type casts.

This occurred on line 66 of i3_non_indexed_fks.sql where the @> operator was being used with operator(pg_catalog.@>) syntax to avoid conflicts with the intarray extension (fixed in PR #35), but PostgreSQL still couldn't resolve which operator to use due to type ambiguity.

Solution

Added explicit ::int2[] cast to the key_cols parameter to match the left side of the comparison, ensuring PostgreSQL knows exactly which @> operator to use:

and (indkey::int2[])[0:(array_length(key_cols,1) -1)] operator(pg_catalog.@>) key_cols::int2[]

Both sides are now explicitly int2[] (smallint array), eliminating the ambiguity.

Fixes #62

Add explicit int2[] cast to key_cols parameter to resolve "operator
is not unique" error when using the @> operator with pg_catalog
schema qualification.

Fixes #62
Add test coverage for i3_non_indexed_fks.sql query to catch potential
operator ambiguity issues. The test:
- Installs intarray extension which can cause operator ambiguity
- Creates parent and child tables with foreign keys
- Verifies i3 query executes without errors
- Checks that foreign keys without indexes are detected

Also update all psql commands in CI to use PAGER=cat per PostgresAI
command execution rules to ensure non-interactive behavior.

Related to #62
Temporarily remove the ::int2[] cast on key_cols parameter to verify
that the new CI test correctly catches the operator ambiguity error.

This commit is expected to fail CI tests.

Related to #62
# Create extensions (pg_stat_statements may not work without shared_preload_libraries)
psql -h localhost -U postgres -d test -c 'CREATE EXTENSION IF NOT EXISTS pg_stat_statements;' || echo "Warning: pg_stat_statements extension not available"
psql -h localhost -U postgres -d test -c 'CREATE EXTENSION IF NOT EXISTS pgstattuple;'
psql -h localhost -U postgres -d test -c 'CREATE EXTENSION IF NOT EXISTS intarray;'
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

follow SQL style guide

Add explicit `::int2[]` cast to `key_cols` parameter in the i3 foreign
key check query. This resolves "operator is not unique" errors that can
occur in certain PostgreSQL environments (such as Google CloudSQL).

The error occurs when PostgreSQL cannot determine which @> operator to
use between smallint[] operands. By explicitly casting both sides to
int2[], we eliminate any ambiguity.

While standard PostgreSQL installations (including those with intarray
extension) may not exhibit this error, the explicit cast is a best
practice that ensures consistent behavior across all environments.

Fixes #62
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

"operator is not unique" on i3 check
1 participant