Skip to content

Commit

Permalink
fix: PG index cardinality estimate (#1869)
Browse files Browse the repository at this point in the history
Fixes the cardinality when using the PostgreSQL adapter to estimate the
number of rows indexed, instead of the number of columns in the index.
  • Loading branch information
thiagotnunes authored Sep 16, 2024
1 parent 39af6e6 commit 29d5fa5
Showing 1 changed file with 2 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ public ImmutableMap<String, ImmutableMap<String, SourceColumnType>> discoverTabl

/**
* Discover the indexes of tables to migrate. You can try this in <a
* href="https://www.db-fiddle.com/f/kTanXYXoM2VgCjSf6NZHjD/1">db-fiddle</a>.
* href="https://www.db-fiddle.com/f/kTanXYXoM2VgCjSf6NZHjD/2">db-fiddle</a>.
*
* @param dataSource Provider for JDBC connection.
* @param sourceSchemaReference Source database name and (optionally namespace)
Expand All @@ -286,13 +286,7 @@ public ImmutableMap<String, ImmutableList<SourceColumnIndexInfo>> discoverTableI
+ " ixs.indexname AS index_name,"
+ " ix.indisunique AS is_unique,"
+ " ix.indisprimary AS is_primary,"
// PG < 11 does not have the indnkeyatts column (total key columns in index), since
// the INCLUDE SQL feature was not available then. We use the indnatts column
// instead (total columns in index).
+ " COALESCE("
+ " (row_to_json(ix)->>'indnkeyatts')::int8,"
+ " (row_to_json(ix)->>'indnatts')::int8"
+ " ) AS cardinality,"
+ " c.reltuples AS cardinality,"
+ " a.attnum AS ordinal_position,"
+ " t.typname AS type_name,"
+ " information_schema._pg_char_max_length(a.atttypid, a.atttypmod) AS type_length,"
Expand Down

0 comments on commit 29d5fa5

Please sign in to comment.