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。
先決條件
開始之前,請確認您已下載並安裝適當的工具和資源。
- 安裝在您電腦上的 Visual Studio Code。
- 在本機安裝的 PostgreSQL 資料庫,或託管於建立 Azure HorizonDB 叢集。
- Visual Studio Code 中安裝的 PostgreSQL 擴充功能。
- GitHub Copilot 擴展碼。
- 已安裝 GitHub Copilot Chat 擴充功能。
- 用來連線到雲端裝載資料庫的 Azure 帳戶(選擇性)。
安裝 GitHub Copilot 和 GitHub Copilot 聊天延伸模組
如果您尚未在 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
請遵循下列步驟,開始使用 GitHub Copilot 搭配 PostgreSQL Visual Studio Code 擴充功能。
- 在物件總管中右鍵點擊 PostgreSQL 資料庫,選擇 Chat with this database。 這方法適用於任何連線——本地 PostgreSQL 或 Azure HorizonDB。
- 若有提示,請選擇 允許 以啟用 GitHub Copilot 存取資料庫連線上下文。
- 當 Copilot 聊天介面開啟時,開始提問,並使用
@pgsql前綴指定你想與 PostgreSQL 資料庫互動。
嘗試提示,例如:
@pgsql tell me about the tables in the HR schema
Copilot 會以結構描述表格的詳細描述進行回應。
連接正確的 HorizonDB 端點
由於 Azure HorizonDB 叢集會暴露兩個端點,你開始聊天的連線會決定 Copilot 產生的 SQL 會針對哪個端點執行:
- 讀取/寫入端點 - 當你想要 Copilot 草擬、修改或執行會變更資料或結構描述的陳述式,或必須讀取最新已提交的狀態時,請使用此連線。
- 讀者端點 - 當您想讓 Copilot 草擬並執行唯讀查詢,並透過 HA 複本的負載平衡水平擴充獲得效益時,請使用此連線(例如探索性分析或報表提示)。
如果您在連線至讀取端點時讓 Copilot 產生寫入陳述式,請先切換目前的連線至讀寫端點,再核准執行。
使用讀寫功能
適用於 Visual Studio Code 中 PostgreSQL 擴充功能的 GitHub Copilot 整合支援讀取和寫入作業。 你可以直接從編輯器查詢資料、修改結構,並透過 AI 驅動的建議,考量即時連線情境,更新紀錄。
Note
GitHub Copilot Chat 的 PostgreSQL 整合可以對你的資料庫做變更。 使用此功能時請謹慎,尤其是在測試和生產環境中。 在執行 SQL 程式代碼之前,請務必先檢閱產生的 SQL 程式代碼,並考慮先在安全的環境中進行測試。 在 Azure HorizonDB 上,另請確保寫入作業是以 讀寫端點 為目標——讀取端點為唯讀。
試試更進階的提示:
@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
然後科皮洛特要求確認:
@pgsql Yes, I confirm
使用快顯功能表選項
你可以在編輯器中選擇 SQL 程式碼,然後右鍵點擊 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 來建立 HorizonDB 的 AI 功能——pgvector、pg_diskann(具備進階過濾功能),以及由 azure_ai 支援的 AI 功能,或是你自己的 Microsoft Foundry 部署。
@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 叢集的讀取端點時,您可以要求 Copilot 草擬可善用負載平衡 HA 複本的唯讀工作負載:
@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索引)。 - 關閉任何開啟的連線,以避免不必要的資源使用量。
意見反應與支援
針對 Bug、功能要求和問題,請使用 Visual Studio Code 中的內建意見反應工具。 你可以透過 Visual Studio Code 說明選單或 PGSQL 指令面板完成此回饋。
-
說明功能表
- 移至 [說明] > [報告問題]
-
命令選擇區
- 使用
Ctrl + Shift + P開啟命令選擇區並執行:PGSQL: Report Issue
- 使用