You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Supabase新增invitations字段后触发statement timeout错误咨询

问题描述

原本调用Supabase RPC函数的代码运行正常:

const [users, diaries, comments, likes, step] = await Promise.all([
  await supabase.rpc("get_users"),
  await supabase.rpc("get_period_diaries", { mondaysecs: monday, sundaysecs: sunday }),
  await supabase.rpc("get_period_comments_month_diary_three", { mondaysecs: monday, sundaysecs: sunday }),
  await supabase.rpc("get_period_likes_month_diary_three", { mondaysecs: monday, sundaysecs: sunday }),
  await supabase.rpc("get_period_steps", { weekdays: weekDays })
]);

添加invitations字段并调用get_period_invitations函数后,代码变为:

const [users, diaries, comments, likes, steps, invitations] = await Promise.all([
  await supabase.rpc("get_users"),
  await supabase.rpc("get_period_diaries", { mondaysecs: monday, sundaysecs: sunday }),
  await supabase.rpc("get_period_comments_month_diary_three", { mondaysecs: monday, sundaysecs: sunday }),
  await supabase.rpc("get_period_likes_month_diary_three", { mondaysecs: monday, sundaysecs: sunday }),
  await supabase.rpc("get_period_steps", { weekdays: weekDays }),
  await supabase.rpc("get_period_invitations", { mondaysecs: monday, sundaysecs: sunday })
]);

此时抛出错误:

"error": { "code": "57014", "details": null, "hint": null, "message": "canceling statement due to statement timeout" }

其中数据量最大的comments字段返回null,其他字段数据正常。两次查询的comments数据量一致,但仅第二次查询返回null。已知statement_timeout为2分钟,请问是否需要延长该参数?


解决方案分析
  • 是否要延长statement_timeout?
    可以临时延长作为排查手段,但不建议直接作为长期解决方案。超时本质是并发RPC调用导致数据库资源竞争,原本能在2分钟内完成的get_period_comments_month_diary_three因资源被其他请求抢占,执行时间超过阈值。

  • 核心问题排查方向

    • 数据库资源竞争:同时发起6个RPC请求,连接池、CPU或IO资源被分散,大查询(comments对应的RPC)无法获得足够资源完成执行,触发超时。
    • RPC函数优化:重点检查get_period_comments_month_diary_three的SQL逻辑,比如是否存在未加索引的关联查询、全表扫描,或者复杂聚合计算。即使之前能运行,并发请求会放大性能问题。
    • 并发请求调整:可将大查询单独发起,或减少并发请求数量,避免一次性抢占过多资源。
  • 具体优化步骤

    1. 单独测试get_period_comments_month_diary_three的执行时间,如果单独执行也接近2分钟,必须优化该RPC的SQL,比如添加合适索引、简化查询逻辑。
    2. 如果单独执行很快,调整代码拆分并发请求:
      // 先获取大查询数据
      const comments = await supabase.rpc("get_period_comments_month_diary_three", { mondaysecs: monday, sundaysecs: sunday });
      // 并行获取其他小数据量请求
      const [users, diaries, likes, steps, invitations] = await Promise.all([
        supabase.rpc("get_users"),
        supabase.rpc("get_period_diaries", { mondaysecs: monday, sundaysecs: sunday }),
        supabase.rpc("get_period_likes_month_diary_three", { mondaysecs: monday, sundaysecs: sunday }),
        supabase.rpc("get_period_steps", { weekdays: weekDays }),
        supabase.rpc("get_period_invitations", { mondaysecs: monday, sundaysecs: sunday })
      ]);
      
    3. 若必须并发所有请求,可临时在Supabase控制台调整statement_timeout参数(比如延长到3分钟),但这只是临时缓解,长期仍需优化查询或请求方式。

内容的提问来源于stack exchange,提问作者Hyejung

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 13:50:11