快速入门
Learn how to use Supabase Queues to add and read messages
本指南是关于如何通过仪表板和官方客户端库与 Supabase 队列进行互动的入门介绍。想了解我们 API 的更多详情,请查看 队列 API 参考。
🌐 This guide is an introduction to interacting with Supabase Queues via the Dashboard and official client library. Check out Queues API Reference for more details on our API.
概念 #
🌐 Concepts
Supabase 队列是一个基于拉取的消息队列,主要由三个部分组成:队列、消息和队列类型。
🌐 Supabase Queues is a pull-based Message Queue consisting of three main components: Queues, Messages, and Queue Types.
拉取式队列 #
🌐 Pull-Based Queue
拉取式队列是一种消息存储和传递系统,消费者在准备处理消息时会主动去获取它们——就像不断刷新网页以显示最新更新一样。我们的拉取式队列以先进先出(FIFO)的方式处理消息,没有优先级区分。
🌐 A pull-based Queue is a Message storage and delivery system where consumers actively fetch Messages when they're ready to process them - similar to constantly refreshing a webpage to display the latest updates. Our pull-based Queues process Messages in a First-In-First-Out (FIFO) manner without priority levels.
信息 #
🌐 Message
队列中的消息是一个 JSON 对象,它会被存储,直到消费者明确地处理并移除它,就像待办事项列表中的任务,直到有人查看并完成它一样。
🌐 A Message in a Queue is a JSON object that is stored until a consumer explicitly processes and removes it, like a task waiting in a to-do list until someone checks and completes it.
队列类型 #
🌐 Queue types
Supabase Queues 提供三种类型的队列:
🌐 Supabase Queues offers three types of Queues:
- 基础队列:一个将消息存储在日志表中的持久队列。
- 无日志队列:一个临时队列,它将消息存储在无日志表中,以获得更好的性能,但可能会导致队列消息丢失。
创建队列 #
🌐 Create Queues
要开始,请在仪表板的“集成”下导航到 Supabase Queues Postgres 模块,并启用 pgmq 扩展。
🌐 To get started, navigate to the Supabase Queues Postgres Module under Integrations in the Dashboard and enable the pgmq extension.
pgmq 扩展在 Postgres 15.6.1.143 或更高版本可用。

在 队列页面 上:
🌐 On the Queues page:
- 点击 创建队列 按钮
- 给你的队列命名
队列名称只能是小写字母,可以使用连字符和下划线。
🌐 Queue names can only be lowercase and hyphens and underscores are permitted.
- 选择你的队列类型
- 我们建议保持行级安全性(RLS)开启。开启后,你就不需要在队列表上设置额外的RLS了。

当你创建一个队列时会发生什么?
每个新的队列都会在 pgmq 模式下创建两个表。这两个表分别是 pgmq.q_<queue_name> 用来存储和处理活跃消息,以及 pgmq.a_<queue_name> 用来存储任何归档消息。
🌐 Every new Queue creates two tables in the pgmq schema. These tables are pgmq.q_<queue_name> to store and process active messages and pgmq.a_<queue_name> to store any archived messages.
“基本队列”会创建 pgmq.q_<queue_name> 和 pgmq.a_<queue_name> 作为日志表。
🌐 A "Basic Queue" creates pgmq.q_<queue_name> and pgmq.a_<queue_name> tables as logged tables.
然而,“无日志队列”会将 pgmq.q_<queue_name> 创建为无日志表,以提高性能,但会牺牲持久性。pgmq.a_<queue_name> 表仍然会创建为有日志的表,因此你的归档消息仍然是安全可靠的。
🌐 However, an "Unlogged Queue" creates pgmq.q_<queue_name> as an unlogged table for better performance while sacrificing durability. The pgmq.a_<queue_name> table is still created as a logged table so your archived messages remain safe and secure.
向客户端消费者公开队列 #
🌐 Expose Queues to client-side consumers
默认情况下,队列不会通过 Supabase 数据 API 暴露,只能通过 Postgres 客户端访问。
🌐 Queues, by default, are not exposed over the Supabase Data API and are only accessible via Postgres clients.
不过,你可以通过启用 Supabase 数据 API 并授予对 Queues API 的权限,让客户端消费者访问你的队列。Queues API 是 pgmq_public 架构中的一组数据库函数,它封装了 pgmq 架构中的数据库函数。
🌐 However, you may grant client-side consumers access to your Queues by enabling the Supabase Data API and granting permissions to the Queues API, which is a collection of database functions in the pgmq_public schema that wraps the database functions in the pgmq schema.
这是为了防止直接访问 pgmq 模式及其表(默认情况下,任何表都没有启用 RLS)和数据库函数。
🌐 This is to prevent direct access to the pgmq schema and its tables (RLS is not enabled by default on any tables) and database functions.
要开始,请导航到仪表板的 Queues > Settings 部分并启用 通过 PostgREST 暴露队列。启用后,Supabase 会创建并暴露一个 pgmq_public 模式,其中包含对 pgmq 数据库函数子集的数据库函数封装。
🌐 To get started, navigate to the Queues > Settings section of the Dashboard and enable Expose Queues via PostgREST. Once enabled, Supabase creates and exposes a pgmq_public schema containing database function wrappers to a subset of pgmq's database functions.
在 pgmq#
🌐 Add an RLS policy on your tables in pgmq schema [#enable-rls-on-your-tables-in-pgmq-schema]
如果你通过 Data API 暴露你的 pgmq 架构,为了安全起见,你必须在所有队列表上启用行级安全(RLS)(pgmq 架构下所有以 q_ 开头的表)
🌐 If you expose your pgmq schema with the Data API, for security purposes, you must enable Row Level Security (RLS) on all Queue tables (all tables in pgmq schema that begin with q_)
通过点击 [添加 RLS 策略] 按钮,在 仪表板中任何队列的概览页面 为你希望客户端消费者交互的队列添加 RLS 策略。
🌐 Add an RLS policy for any Queues you want your client-side consumers to interact with, by clicking the Add RLS Policy button on the overview page of any Queue in the Dashboard.
授予 pgmq_public#
🌐 Grant permissions to pgmq_public database functions
除了在底层队列表上启用 RLS 并编写 RLS 策略之外,你还必须为每个 Data API 角色授予对 pgmq_public 数据库函数的正确权限。
🌐 On top of enabling RLS and writing RLS policies on the underlying Queue tables, you must grant the correct permissions to the pgmq_public database functions for each Data API role.
每个队列 API 数据库功能所需的权限:
🌐 The permissions required for each Queue API database function:
| 操作 | 所需权限 |
|---|---|
send send_batch | Select Insert |
read pop | Select Update |
archive delete | Select Delete |
要管理你的队列权限,请点击 仪表板中任意队列概览页面 上的队列设置齿轮按钮。
🌐 To manage your queue permissions, click on the Queue Settings cog button on the overview page of any Queue in the Dashboard.

然后启用所需的角色权限。
🌐 Then enable the required roles permissions.
| 角色 | 选择 | 插入 | 更新 | 删除 |
|---|---|---|---|---|
| 匿名 | ||||
| 已认证 | 已启用 | 已启用 | 已启用 | 已启用 |
| postgres | 已启用 | 已启用 | 已启用 | 已启用 |
| 服务角色 | 已启用 | 已启用 | 已启用 | 已启用 |
你绝不应该在客户端暴露 postgres 和 service_role 角色。
🌐 You should never expose postgres and service_role roles client-side.
消息入队和出队 #
🌐 Enqueueing and dequeueing messages
一旦你创建了队列,就可以开始入队和出队消息了。
🌐 Once you have created your Queue, you can begin enqueueing and dequeueing Messages.
1import { createClient } from '@supabase/supabase-js'23const supabaseUrl = 'supabaseURL'4const supabaseKey = 'supabaseKey'56const supabase = createClient(supabaseUrl, supabaseKey)78const QueuesTest: React.FC = () => {9 //Add a Message10 const sendToQueue = async () => {11 const result = await supabase.schema('pgmq_public').rpc('send', {12 queue_name: 'foo',13 message: { hello: 'world' },14 sleep_seconds: 30,15 })16 console.log(result)17 }1819 //Dequeue Message20 const popFromQueue = async () => {21 const result = await supabase.schema('pgmq_public').rpc('pop', { queue_name: 'foo' })22 console.log(result)23 }2425 return (26 <div className="p-6">27 <h2 className="text-2xl font-bold mb-4">Queue Test Component</h2>28 <button29 onClick={sendToQueue}30 className="bg-blue-500 text-white px-4 py-2 rounded-sm hover:bg-blue-600 mr-4"31 >32 Add Message33 </button>34 <button35 onClick={popFromQueue}36 className="bg-blue-500 text-white px-4 py-2 rounded-sm hover:bg-blue-600"37 >38 Pop Message39 </button>40 </div>41 )42}4344export default QueuesTest