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

PL/pgSQL调用graphile_worker.add_job报无查询结果目标错误排查

问题产生原因

这个错误和PostGraphile、你编写的email.ts Graphile Worker任务逻辑无关,是PL/pgSQL的基础语法规则限制:

  • PL/pgSQL块中使用SELECT语句调用有返回值的函数时,必须通过INTO子句指定变量接收查询结果,否则就会抛出query has no destination for result data错误。
  • graphile_worker.add_job()本身是有返回值的函数(执行后会返回新创建任务的自增ID),你直接写select graphile_worker.add_job(...)没有指定结果接收目标,就触发了这个报错。移除这行调用后没有不符合语法的语句,错误自然消失。
修复方法

如果你不需要使用add_job返回的任务ID,直接把调用前的SELECT替换成PL/pgSQL提供的PERFORM关键字即可——PERFORM专门用于执行不需要保留返回值的函数调用,会自动丢弃返回结果,不会要求配置结果存储目标。
修正后的函数代码参考:

create or replace function public.function()
returns public.xyz as $$
begin
if //condition// then
  if //condition// then
    //some logic
    return xyz
  else
    //logic
    -- 把select替换为perform即可
    perform graphile_worker.add_job('email', json_build_object('subject', subject, 'email', email));
    return null
  end if;
   return null;
 end if;
end;
$$ language plpgsql strict security definer;

如果你后续需要用到新建任务的ID做后续逻辑,也可以先声明对应类型的变量,通过SELECT ... INTO 变量名的方式接收返回值再处理,示例:

create or replace function public.function()
returns public.xyz as $$
-- 声明变量存储job id
declare
  new_job_id bigint;
begin
if //condition// then
  if //condition// then
    //some logic
    return xyz
  else
    //logic
    select graphile_worker.add_job('email', json_build_object('subject', subject, 'email', email)) into new_job_id;
    -- 后续可以使用new_job_id做其他逻辑
    return null
  end if;
   return null;
 end if;
end;
$$ language plpgsql strict security definer;

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.28 20:24:28