Visual Studio Code用 PostgreSQL 拡張機能には、GitHub Copilot統合が含まれています。AI 支援型の開発により、データベース ワークフローが強化されます。 PostgreSQL データベースに接続すると、Copilotはライブ接続からコンテキスト情報にアクセスします。 このアクセスにより、@pgsql Copilot Chat参加要素は、正確でスキーマに対応した SQL クエリと分析情報を生成し、開発を合理化し、Visual Studio Code内でのコンテキスト切り替えを最小限に抑えることができます。
@pgsql参加要素は、拡張機能が接続できる任意のデータベースに対して動作します。次に示します。
- Azure HorizonDB クラスター。
HorizonDB クラスターに接続すると、Copilotは、データベースにインストールされている HorizonDB 固有の拡張機能 (pgvector、pg_diskann、azure_ai、age など) を認識し、それらを利用するプロンプトと SQL を生成できます。
Prerequisites
開始する前に、適切なツールとリソースがダウンロードおよびインストールされていることを確認します。
- コンピューターにインストールされている Visual Studio Code。
- ローカルにインストールされた PostgreSQL データベース、または Azure HorizonDB クラスターの作成 でホストされている PostgreSQL データベース。
- Visual Studio Code にインストールされている PostgreSQL 拡張機能。
- GitHub Copilot拡張機能。
- GitHub Copilot Chat 拡張機能 がインストールされました。
- クラウドでホストされているデータベースに接続するための Azure アカウント (省略可能)。
GitHub Copilot と GitHub Copilot Chat 拡張機能をインストールする
Visual Studio Code に GitHub Copilot 拡張機能がまだインストールされていない場合:
- Visual Studio Code で [拡張機能 ] アイコンを選択し、 GitHub Copilot を検索して、[インストール] を選択 します。
- GitHub Copilot チャット拡張機能は、GitHub Copilot と共に自動的にインストールされます。
Visual Studio Code で GitHub にサインインする
- GitHub アカウントとアクティブなGitHub Copilot サブスクリプションがあることを確認します。
- Visual Studio Codeで、
Account アイコンを選択し、GitHub を選択してGitHub Copilotを使用します。
GitHub Copilotの使い始め方
手順に従って、PostgreSQL Visual Studio Code 拡張機能で GitHub Copilot の使用を開始します。
- オブジェクト エクスプローラーで PostgreSQL データベースを右クリックし、 このデータベースでチャットを選択します。 これは、ローカル PostgreSQL または Azure HorizonDB のすべての接続に対して機能します。
- メッセージが表示されたら、[ 許可 ] を選択して GitHub Copilot がデータベース接続コンテキストにアクセスできるようにします。
- Copilot チャット インターフェイスが開いたら、
@pgsqlプレフィックスを使用して PostgreSQL データベースと対話することを指定して質問を開始します。
次のようなプロンプトを試してください。
@pgsql tell me about the tables in the HR schema
Copilot は、スキーマのテーブルの詳細な説明で応答します。
適切な HorizonDB エンドポイントに接続する
Azure HorizonDB クラスターには 2 つのエンドポイントがあるため、チャットを開始した接続によって、Copilot が生成した SQL がどちらのエンドポイントで実行されるかが決まります。
- 読み取り/書き込みエンドポイント - データやスキーマを変更するステートメント、または最新のコミット済み状態を読み取る必要があるステートメントを Copilot に作成、変更、または実行させる場合は、この接続を使用します。
- Reader エンドポイント - Copilot が、HA レプリカ間での負荷分散によるスケールアウトの恩恵を受ける 読み取り専用 クエリ(たとえば、探索的分析やレポート用のプロンプト)を作成して実行する場合は、この接続を使用します。
リーダー エンドポイントに接続しているときにCopilot書き込みステートメントが生成される場合は、実行を承認する前に、アクティブな接続を読み取り/書き込みエンドポイントに切り替えます。
読み取りと書き込みの機能を使用する
Visual Studio Code での PostgreSQL 拡張機能のGitHub Copilot統合では、読み取り操作と書き込み操作の両方がサポートされます。 ライブ接続コンテキストを考慮した AI を利用した提案を使用して、エディターからデータのクエリ、スキーマの変更、レコードの更新を直接行うことができます。
Note
PostgreSQL の GitHub Copilot Chat統合により、データベースに変更を加えることができます。 この機能は、特にステージング環境と運用環境では注意して使用してください。 生成された SQL コードを実行する前に必ず確認し、最初に安全な環境でテストすることを検討してください。 HorizonDB Azureでは、書き込み操作が read/write エンドポイント をターゲットにしていることを確認します。リーダー エンドポイントは読み取り専用です。
より高度なプロンプトを試してみてください。
@pgsql convert the hr.employees table to use a JSONB column for the address field
Copilot は SQL の提案で応答し、変更を加えるアクセス許可を求める場合があります。
実行を承認するには:
@pgsql Yes, please make the JSONB column for me
その後、Copilot は確認を求めます。
@pgsql Yes, I confirm
コンテキスト メニュー オプションを使用する
エディターで SQL コードを選択し、右クリックして、 Explain Query、 Rewrite Query、 Analyze Query Performance などの GitHub Copilot コンテキスト メニュー オプションにアクセスできます。 HorizonDB では、説明と書き換えの提案も、HorizonDB 固有のインデックス ( pg_diskann ベクター インデックスなど) が存在する場合に考慮されます。
その他のアイデアとプロンプトレシピ
次のセクションでは、データベースコンテキストと開発環境に合わせて試したり変更したりできる概念プロンプトを示します。
クエリ最適化
これらのプロンプトを使用して、特定のクエリ最適化の課題に対処する方法について Copilot をガイドします。
I'm working on optimizing my database for high-concurrency workloads. The table is called transactions with millions of records, and I'm experiencing deadlocks under a heavy load. Help me optimize my table schema and queries.
I need help writing a query. The data is stored in the orders table, which uses the columns customer_id, order_date, and total_price. I also need to include a rolling 3-month average of customer spending using a window function.
I'm getting this error: 'ERROR: column "orders.total_price" must appear in the GROUP BY clause or be used in an aggregate function.'
パフォーマンスの最適化
これらのプロンプトを使用して、特定のパフォーマンス最適化の課題に対処するために Copilot をガイドします。
Provide the Explain Plan for my most recent query, and please explain each step.
Can you run some performance metrics on my database and tell me how it performs?
My orders table has 10 million records, and queries on customer_id and order_date are slow. How can I optimize indexing, partitioning, and schema design for performance?
アプリ開発
これらのプロンプトを使用して、アプリ開発の課題に対処するための Copilot をガイドします。
Generate a FastAPI endpoint to fetch orders from the ecom.orders table with pagination.
Generate an ETL pipeline script to clean and normalize the customer table data.
Generate a FastAPI project with my database using SQLAlchemy.
Azure HorizonDB 上の AI ワークロード
HorizonDB クラスターに接続している場合、Copilot を使用して、pgvector、pg_diskann(Advanced Filtering 対応)、および AI モデル管理(限定プレビュー) またはお客様独自の Microsoft Foundry デプロイを基盤とする azure_ai AI Functions など、HorizonDB の AI 機能を活用できます。
@pgsql Create a products table with a description column and a vector(1536) embedding column. Then write a query that populates the embedding column for any rows where it's NULL using azure_openai.create_embeddings with the default-embedding managed model.
@pgsql Build a pg_diskann index on the products.embedding column using vector_cosine_ops, then write a similarity search that finds the top 10 products with average_rating > 4.5 and price between 100 and 200 - using DiskANN Advanced Filtering so the WHERE clause is evaluated during the vector search.
@pgsql Use azure_ai.extract() to pull product and sentiment out of the review_text column of the product_reviews table, and azure_ai.is_true() to flag reviews that mention shipping issues.
@pgsql I have a Microsoft Foundry deployment of gpt-5 in my own subscription. Show me how to register it with model_registry.model_add as 'gpt-5-byom' and then call azure_ai.generate() with that alias to rewrite the comment_text column of user_comments to be more polite.
読み取りスケールアウト
HorizonDB クラスターの reader エンドポイントに接続すると、負荷分散された HA レプリカを利用する読み取り専用ワークロードのドラフト作成をCopilotに求めることができます。
@pgsql Generate a reporting query against the orders and customers tables that aggregates monthly revenue by region and customer segment. Make sure the query is read-only so it's safe to run against the HorizonDB reader endpoint.
クリーンアップ
スムーズなエクスペリエンスを実現するには、このクイックスタートで作成した一時的なリソースまたは構成をクリーンアップします。 例えば次が挙げられます。
- Visual Studio Code で PostgreSQL データベースから切断します。
- セッション中に作成したすべてのテスト データベース、テーブル、またはインデックス (
pg_diskannインデックスを含む) を削除します。 - 不要なリソースの使用を避けるために、開いている接続を閉じます。
フィードバックとサポート
バグ、機能要求、および問題については、Visual Studio Code の組み込みのフィードバック ツールを使用します。 このフィードバックは、Visual Studio Code の [ヘルプ] メニューまたは PGSQL コマンド パレットから完了できます。
-
[ヘルプ] メニュー
- 問題の報告>ヘルプに移動する
-
コマンド パレット
-
Ctrl + Shift + Pでコマンド パレットを開き、次のコマンドを実行します。PGSQL: Report Issue
-