Skip to content

Conversation

@Khoyo
Copy link
Contributor

@Khoyo Khoyo commented Feb 13, 2025

Mostly generated with pgassistant, but can also be done with

WITH fkeys_without_indexes AS
  (SELECT conname AS fk_name,
          conrelid::regclass AS TABLE_NAME,
          a.attname AS COLUMN_NAME,
          n.nspname AS SCHEMA_NAME,
          condeferrable AS is_deferrable,
          confupdtype AS update_action,
          confdeltype AS delete_action,
          conkey
   FROM pg_constraint c
   JOIN pg_namespace n ON n.oid = c.connamespace
   JOIN pg_class r ON r.oid = c.conrelid
   JOIN unnest(c.conkey) AS col_num ON TRUE
   JOIN pg_attribute a ON a.attnum = col_num
   AND a.attrelid = r.oid
   LEFT JOIN pg_index i ON i.indrelid = r.oid
   AND col_num = ANY (i.indkey)
   WHERE c.contype = 'f'
     AND i.indexrelid IS NULL),
     suggested_indexes AS
  (SELECT SCHEMA_NAME,
          TABLE_NAME,
          COLUMN_NAME,
          'CREATE INDEX pga_idx_fk_' || TABLE_NAME || '_' || COLUMN_NAME || ' ON ' || SCHEMA_NAME || '.' || TABLE_NAME || '(' || COLUMN_NAME || ');' || 'ANALYZE ' || SCHEMA_NAME || '.' || TABLE_NAME || ';' AS pga_suggestion
   FROM fkeys_without_indexes)
SELECT SCHEMA_NAME,
       TABLE_NAME,
       COLUMN_NAME,
       pga_suggestion
FROM suggested_indexes
ORDER BY SCHEMA_NAME,
         TABLE_NAME,
         COLUMN_NAME;

Signed-off-by: Younes Khoudli <younes.khoudli@epita.fr>
@Khoyo Khoyo requested a review from a team as a code owner February 13, 2025 09:18
@github-actions github-actions bot added the area:editoast Work on Editoast Service label Feb 13, 2025
@codecov-commenter
Copy link

codecov-commenter commented Feb 13, 2025

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 81.83%. Comparing base (da06215) to head (224c432).
Report is 27 commits behind head on dev.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##              dev   #10792      +/-   ##
==========================================
- Coverage   81.83%   81.83%   -0.01%     
==========================================
  Files        1081     1081              
  Lines      106879   106879              
  Branches      721      721              
==========================================
- Hits        87466    87462       -4     
- Misses      19372    19376       +4     
  Partials       41       41              
Flag Coverage Δ
editoast 74.31% <ø> (-0.02%) ⬇️
front 89.38% <ø> (ø)
gateway 2.18% <ø> (ø)
osrdyne 3.28% <ø> (ø)
railjson_generator 87.50% <ø> (ø)
tests 88.14% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@woshilapin woshilapin left a comment

Choose a reason for hiding this comment

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

Looks legit, didn't tested it.

Copy link
Contributor

@leovalais leovalais left a comment

Choose a reason for hiding this comment

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

I thought Pg would generate indexes for fks at least... LGTM

Any reason why infra objects are left out?

@Khoyo
Copy link
Contributor Author

Khoyo commented Feb 14, 2025

Because of the UNIQUE constraint on (infra_id, obj_id), they already get a btree index on that by default (and since infra_id is the leading column of the tuple, an additional index on infra_id is very redundant)

@Khoyo Khoyo added this pull request to the merge queue Feb 14, 2025
Merged via the queue into dev with commit b38cad9 Feb 14, 2025
27 checks passed
@Khoyo Khoyo deleted the yk/editoast-missing-fkey-index branch February 14, 2025 13:05
@beh74
Copy link

beh74 commented Aug 10, 2025

you should take a look at the new version of pgAssistant here : https://github.com/beh74/pgassistant-community and read this post about missing indexes on FK : https://beh74.github.io/pgassistant-blog/doc/issue_index_fk/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:editoast Work on Editoast Service

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants