Schema Cache 问题
CloudBase PostgreSQL 的 HTTP API 基于 PostgREST 协议。PostgREST 会缓存数据库 schema 信息,因此修改表结构、函数或关系后,HTTP API 可能短时间内仍使用旧 schema。
常见现象
- 新增列后,HTTP API 返回字段中没有新列。
- 调用 RPC 提示函数不存在。
- 修改函数参数后,HTTP API 仍按旧参数校验。
- 新增外键关系后,关联查询不可用。
先确认 SQL 层是否生效
在 DMC 中查询表结构:
select column_name, data_type
from information_schema.columns
where table_schema = 'public'
and table_name = 'todos'
order by ordinal_position;
查询函数:
select routine_name, data_type
from information_schema.routines
where routine_schema = 'public';
如果 SQL 层也不可见,说明表结构或函数创建本身未成功。
处理方式
如果 SQL 层已经生效但 HTTP API 未更新,可等待 schema cache 自动刷新,或根据控制台提供的入口手动刷新 API schema。
对于上线流程,建议先完成表结构和函数变更,再等待或刷新 schema,最后发布依赖新字段的前端或服务端代码。
RPC 函数注意事项
PostgREST 对函数签名较敏感。修改函数参数时,建议使用 create or replace function 明确参数名称和类型。
create or replace function public.search_todos(keyword text)
returns setof public.todos as $$
select *
from public.todos
where title ilike '%' || keyword || '%';
$$ LANGUAGE SQL STABLE;
调用示例请参考 调用 RPC。
预防建议
- 表结构变更和 API 发布分步骤进行。
- 避免在同一发布时间窗口内频繁改函数签名。
- 对依赖 HTTP API 的字段新增兼容逻辑。
- 在测试环境验证 schema 刷新后再发布生产环境。