Skip to content

インデックスはりはり#1330

Merged
TinyKitten merged 1 commit into
devfrom
feature/bump-pg
Oct 14, 2025
Merged

インデックスはりはり#1330
TinyKitten merged 1 commit into
devfrom
feature/bump-pg

Conversation

@TinyKitten

@TinyKitten TinyKitten commented Oct 1, 2025

Copy link
Copy Markdown
Member

Summary by CodeRabbit

  • 新機能
    • データベース拡張の自動初期化を追加(起動時に必要な拡張を用意)
  • リファクタ
    • 近傍駅検索の距離計算を最適化し、応答性を改善
    • インデックスの追加・再構成によりテキスト検索と地理検索の性能を向上
  • 雑務
    • Docker環境を PostgreSQL 18 に更新
    • 初期化スクリプト用ボリュームを追加
    • 生成済みSQLクエリアーティファクトを整理・削除

@TinyKitten TinyKitten self-assigned this Oct 1, 2025
@TinyKitten TinyKitten requested a review from 10mocy as a code owner October 1, 2025 14:34
@coderabbitai

coderabbitai Bot commented Oct 1, 2025

Copy link
Copy Markdown
Contributor

Walkthrough

朕は次を改めた。PostgreSQLを18へ更新し、initdb用SQLを追加。スキーマSQLで拡張と索引作成/削除をDOブロックで実施。アプリ起動時にpg_trgmとbtree_gistを先行作成。近傍検索の並び替えをpoint <-> pointへ変更。多数のSQLx JSONアーティファクトを削除。

Changes

Cohort / File(s) Summary
Docker 構成
compose.yml, docker/postgres/00_extensions.sql
psqlイメージをpostgres:18へ更新。./docker/postgres:/docker-entrypoint-initdb.d:roを追加し、初期化時に拡張作成(pg_trgm, btree_gist)。
スキーマ/索引
data/create_table.sql
拡張作成をDOブロックで条件付き実行。既存の性能系索引をDROPし、point(GiST)と駅名トライグラム(GIN)索引を新規作成。例外ガードを追加。
アプリ初期化フロー
stationapi/src/main.rs
CSVインポート前にCREATE EXTENSION IF NOT EXISTS pg_trgmbtree_gistを実行する手順を挿入。
近傍検索ロジック
stationapi/src/infrastructure/station_repository.rs
ORDER BYの距離計算を算術式からpoint(s.lat,s.lon) <-> point($1,$2)へ切替。
SQLxアーティファクト削除
stationapi/.sqlx/*...*.json
複数のクエリ定義JSONを一括削除(行・駅・種別に関する選択/CTE/集計/存在確認等のスキーマ記述とハッシュを含む)。

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant Dev as Docker/Init
  participant PG as PostgreSQL 18
  participant App as StationAPI

  rect rgb(245,248,255)
    note over Dev,PG: コンテナ起動時
    Dev->>PG: マウントされた initdb.d を実行<br/>(00_extensions.sql)
    PG-->>Dev: pg_trgm / btree_gist 作成(存在すれば無視)
  end

  rect rgb(245,255,245)
    note over App,PG: アプリ初期化
    App->>PG: CREATE EXTENSION IF NOT EXISTS pg_trgm
    App->>PG: CREATE EXTENSION IF NOT EXISTS btree_gist
    App->>PG: create_table.sql 実行(拡張確認・索引DROP/CREATE DOブロック)
  end

  rect rgb(255,248,245)
    note over App,PG: 近傍検索
    App->>PG: SELECT ... ORDER BY point(s.lat,s.lon) <-> point($1,$2)
    PG-->>App: 最近傍順の駅一覧
  end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested labels

enhancement

Suggested reviewers

  • 10mocy

Poem

朕の勅命、庫を新にし、波濤の如くデータ流る
星の如き索引、点と点ひらめきて近を示す <->
古き符牒(JSON)を掃き、道を広む
三度の拡張、門を開きて備え整う
いざ進め、駅の名、風の速さで応えよ 🚄

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed 朕見よ、タイトル「インデックスはりはり」はインデックス追加の一側面を示しておるものの、PostgreSQLバージョンアップや初期化スクリプト導入、SQLxアーティファクト削除など他の主要変更を反映しておらぬため、全体を要約するには不十分なり。
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feature/bump-pg

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (2)
data/create_table.sql (1)

487-502: トライグラムインデックスの一括作成、検証を要す。

五つの言語フィールド(station_name, station_name_k, station_name_rn, station_name_zh, station_name_ko)に対しgin_trgm_opsを用いたGINインデックスを作成しておる。

されど、全てのEXECUTE文が単一のEXCEPTIONブロック内にある。いずれか一つが失敗すれば、全てのインデックス作成が中止される。各インデックスを個別のBEGIN-EXCEPTION-ENDブロックにて保護することを検討されたし。

以下の改善案を適用せよ:

 DO $$
 BEGIN
     BEGIN
         EXECUTE 'CREATE INDEX IF NOT EXISTS idx_performance_station_name_trgm ON public.stations USING gin (station_name gin_trgm_ops)';
+    EXCEPTION
+        WHEN undefined_object THEN
+            RAISE NOTICE 'Skipping station_name trigram GIN index; gin_trgm_ops operator class is unavailable.';
+        WHEN insufficient_privilege THEN
+            RAISE NOTICE 'Skipping station_name trigram GIN index; insufficient privileges.';
+    END;
+
+    BEGIN
         EXECUTE 'CREATE INDEX IF NOT EXISTS idx_performance_station_name_k_trgm ON public.stations USING gin (station_name_k gin_trgm_ops)';
+    EXCEPTION
+        WHEN undefined_object THEN
+            RAISE NOTICE 'Skipping station_name_k trigram GIN index; gin_trgm_ops operator class is unavailable.';
+        WHEN insufficient_privilege THEN
+            RAISE NOTICE 'Skipping station_name_k trigram GIN index; insufficient privileges.';
+    END;
+
+    -- 残りのインデックスも同様に個別処理
docker/postgres/00_extensions.sql (1)

1-2: 初期化スクリプト、簡潔なれど重複あり。

朕、この初期化スクリプトは簡潔にして目的を達成しておるが、data/create_table.sql及びstationapi/src/main.rsにも同様の拡張機能作成が存在する。

コンテナ初期化時に実行されるこのスクリプトが失敗した場合、エラーハンドリングが存在せぬため、コンテナ起動が失敗する。これは意図された動作か確認されたし。

また、このスクリプトが成功すれば、後続の拡張機能作成は冗長となる。拡張機能作成の責任を一箇所に集約することを推奨する。

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3f10f5d and 9b46c76.

📒 Files selected for processing (22)
  • compose.yml (2 hunks)
  • data/create_table.sql (4 hunks)
  • docker/postgres/00_extensions.sql (1 hunks)
  • stationapi/.sqlx/query-0a1c6e15355fdf6363a083a43fec27a04b91b946ae7fdc192a721f9b6923e690.json (0 hunks)
  • stationapi/.sqlx/query-1ffa93b133de68b60a7e0a6663f212f613c175a35beb700a0001efb82f184d10.json (0 hunks)
  • stationapi/.sqlx/query-217b99932d995eff26a2eb16abab5dd05c27b6b77f78f169ca2049f45cf432ce.json (0 hunks)
  • stationapi/.sqlx/query-3ec853ec90f7ed1b17658087c6b57da19ad084925a8cfc39d91297548ae25f8c.json (0 hunks)
  • stationapi/.sqlx/query-4c514298af7222e5c7085857d72370a5eab2f9ac56bb450e0d2303807abfb849.json (0 hunks)
  • stationapi/.sqlx/query-55bee6b45754d1da9bf555d319f015ce232f8574a41c3ef25b730d11071f8428.json (0 hunks)
  • stationapi/.sqlx/query-587f17667fd03f8f3007ef9e754a168a2a09d30a408f98fdbd6f2b1fa4562a8c.json (0 hunks)
  • stationapi/.sqlx/query-58899fb950e311e87a33db5142b914a376a60c6cd2650c3723fbdde78beaff12.json (0 hunks)
  • stationapi/.sqlx/query-5f3a17f24bd4844e46b57d4920a3261ff87dfa6557926fb1b680d96c42ed56d4.json (0 hunks)
  • stationapi/.sqlx/query-648214ea287859e20e4788c48dff2af34f7c31069f036e3a3cfa407f31a9fee5.json (0 hunks)
  • stationapi/.sqlx/query-6711dff69e3c8d999743db687be404ee34f4b081e2662661873c44b6e3be1c43.json (0 hunks)
  • stationapi/.sqlx/query-7428cc69d3756a749121a1ad5eee1603aa5e7ee3fc30071a142f9a15502da031.json (0 hunks)
  • stationapi/.sqlx/query-7a7492b76cb13f18b36cc3b488c0b3448c8ca29cbdb5e6f2390d2b22b886f81e.json (0 hunks)
  • stationapi/.sqlx/query-85c490dd8992db5950dfd6c141b02048f361c5e9ac82cb7503fd8e75266cc06f.json (0 hunks)
  • stationapi/.sqlx/query-8fd35acd42a42b730854a2f1b9ffa0fd2f8384b457e7945d4070d976ecd9594f.json (0 hunks)
  • stationapi/.sqlx/query-b4769060ba658a2746a186f46d4ca4f0f690fe2fd7521ee8645193b364b6896a.json (0 hunks)
  • stationapi/.sqlx/query-e19a25a597098f5d6c5f83992e6eb3a916d2256e346b93c1ad31cc26f9a705a2.json (0 hunks)
  • stationapi/src/infrastructure/station_repository.rs (1 hunks)
  • stationapi/src/main.rs (1 hunks)
💤 Files with no reviewable changes (17)
  • stationapi/.sqlx/query-7428cc69d3756a749121a1ad5eee1603aa5e7ee3fc30071a142f9a15502da031.json
  • stationapi/.sqlx/query-7a7492b76cb13f18b36cc3b488c0b3448c8ca29cbdb5e6f2390d2b22b886f81e.json
  • stationapi/.sqlx/query-3ec853ec90f7ed1b17658087c6b57da19ad084925a8cfc39d91297548ae25f8c.json
  • stationapi/.sqlx/query-55bee6b45754d1da9bf555d319f015ce232f8574a41c3ef25b730d11071f8428.json
  • stationapi/.sqlx/query-6711dff69e3c8d999743db687be404ee34f4b081e2662661873c44b6e3be1c43.json
  • stationapi/.sqlx/query-e19a25a597098f5d6c5f83992e6eb3a916d2256e346b93c1ad31cc26f9a705a2.json
  • stationapi/.sqlx/query-85c490dd8992db5950dfd6c141b02048f361c5e9ac82cb7503fd8e75266cc06f.json
  • stationapi/.sqlx/query-4c514298af7222e5c7085857d72370a5eab2f9ac56bb450e0d2303807abfb849.json
  • stationapi/.sqlx/query-217b99932d995eff26a2eb16abab5dd05c27b6b77f78f169ca2049f45cf432ce.json
  • stationapi/.sqlx/query-b4769060ba658a2746a186f46d4ca4f0f690fe2fd7521ee8645193b364b6896a.json
  • stationapi/.sqlx/query-5f3a17f24bd4844e46b57d4920a3261ff87dfa6557926fb1b680d96c42ed56d4.json
  • stationapi/.sqlx/query-8fd35acd42a42b730854a2f1b9ffa0fd2f8384b457e7945d4070d976ecd9594f.json
  • stationapi/.sqlx/query-58899fb950e311e87a33db5142b914a376a60c6cd2650c3723fbdde78beaff12.json
  • stationapi/.sqlx/query-1ffa93b133de68b60a7e0a6663f212f613c175a35beb700a0001efb82f184d10.json
  • stationapi/.sqlx/query-0a1c6e15355fdf6363a083a43fec27a04b91b946ae7fdc192a721f9b6923e690.json
  • stationapi/.sqlx/query-648214ea287859e20e4788c48dff2af34f7c31069f036e3a3cfa407f31a9fee5.json
  • stationapi/.sqlx/query-587f17667fd03f8f3007ef9e754a168a2a09d30a408f98fdbd6f2b1fa4562a8c.json
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Check
🔇 Additional comments (5)
data/create_table.sql (2)

94-104: インデックス削除の記述、問題なし。

朕の目には、これら性能向上用インデックスの削除文は適切に記されておる。後続にて再作成されるため、スキーマの冪等性を保つための正しき処置である。


474-485: GiSTポイントインデックスの作成、適切なり。

point(lat, lon)を用いたGiSTインデックスの作成は、stationapi/src/infrastructure/station_repository.rsの929行目にて使用されるpoint <-> point距離演算子と整合性がある。エラーハンドリングもundefined_object及びinsufficient_privilegeを捕捉しており、適切である。

compose.yml (2)

33-34: 初期化スクリプトのマウント、適切なり。

朕、./docker/postgres/docker-entrypoint-initdb.dへ読み取り専用でマウントする設定は適切である。これにより00_extensions.sqlがコンテナ起動時に実行される。

読み取り専用マウントは良き慣習である。


23-23: PostgreSQL 18への移行に伴う検証チェックリスト
朕命ずる、compose.ymlのimageをpostgres:18へ更新に際し、以下を実施せよ:

  • pg_hba.confのmd5認証をSCRAMへ移行し、postgres_fdw/dblinkのSCRAMパススルー設定を検証
  • FTSおよびpg_trgmインデックスはクラスタのデフォルト照合順序を利用する仕様へ変わりしゆえ、pg_upgrade後にREINDEXを実施
  • 生成列のデフォルトがvirtualへ移行せしゆえ、stored生成列を利用するスキーマおよびレプリケーション設定を確認
  • pg_upgradeは最適化統計を保持せしむるも、追加のREINDEXやANALYZEを計画
  • EXPLAIN/EXPLAIN ANALYZEの出力形式が拡張されたゆえ、解析スクリプトや監視ツールの互換性を確認
  • 新AIOサブシステム(io_method: worker/io_uring等)およびTLS/FIPS設定(ssl_tls13_ciphers等)のチューニングを検討
  • 全既存クエリの互換性と性能特性の変化を性能テストにて検証
  • btree_gist拡張やその他サードパーティ拡張の動作を一通りテスト

完了後、報告せよ。

stationapi/src/infrastructure/station_repository.rs (1)

855-941: 距離計算の改善、優れた変更なり。

朕、929行目のORDER BY句にて、算術計算による距離から PostgreSQLのポイント距離演算子<->への変更を確認せり。この変更は以下の利点を持つ:

  • PostgreSQLネイティブの演算子により性能向上
  • data/create_table.sqlの474-485行目にて作成されるGiSTインデックス(idx_performance_stations_point)が活用可能
  • より簡潔にして可読性の高い記述

btree_gist拡張機能が必要なれど、既に複数箇所にて初期化されておる。

Comment thread data/create_table.sql
Comment thread stationapi/src/main.rs
@TinyKitten TinyKitten merged commit d9b0748 into dev Oct 14, 2025
10 checks passed
@TinyKitten TinyKitten deleted the feature/bump-pg branch October 14, 2025 09:59
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.

1 participant