微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PostgREST:使用单个 json 参数请求 volatile RPC 并触发多个触发器需要很长时间才能完成 - 已解决

如何解决PostgREST:使用单个 json 参数请求 volatile RPC 并触发多个触发器需要很长时间才能完成 - 已解决

与通过 psql 调用存储过程相比,我在通过 PostgREST 调用存储过程时遇到了主要的性能问题。两种方法都成功完成,但 PostgREST 需要大约 1 分钟,而 psql 需要 2 秒。此过程会触发许多语句级别和一些行级别触发器。

这个程序更新数据库,另一个更新程序有同样的问题,

从 psql 调用

set role myrole;
start transaction;

WITH pgrst_source AS (WITH pgrst_args AS (SELECT NULL) SELECT "api"."docket_creation"('{"dockets":[{"docket":{},"transactions":[{"transaction":{"batch_id":null,"best_before_date":null,"docket_id":1417,"minimum_price":1.5,"note":"","price":3,"product_code":"TL12","product_cost":1,"product_description":"","product_group_id":null,"product_id":217,"product_name":"12 Tray Large","quantity":1,"vat_rate_id":4,"vat_rate_percentage":0,"return_reason_id":null,"transaction_type_id":2},"transaction_discounts":[{"discount_percentage":5,"discount_type_id":10,"discount_value_per_unit":null,"minimum_quantity":null,"note":""}],"transaction_returns":[],"transaction_commissions":[{"commission_id":null,"commission_percentage":0,"commission_type_id":2,"customer_id":1,"delivery_agent_id":0,"effective_from":null,"effective_to":null,"is_cumulative":true,"owner":2,"product_id":217}]}],"docket_buy_x_get_y_discounts":[]}]}'::json) AS pgrst_scalar)
  
  SELECT
    null::bigint AS total_result_set,pg_catalog.count(_postgrest_t) AS page_total,(json_agg(_postgrest_t.pgrst_scalar)->0)::character varying AS body,coalesce(nullif(current_setting('response.headers',true),''),'[]') AS response_headers
  FROM (SELECT "pgrst_source".* FROM "pgrst_source"    ) _postgrest_t;

或者干脆:

 SELECT "api"."docket_creation"('{"dockets":[{"docket":{},"docket_buy_x_get_y_discounts":[]}]}')

这大约需要 2 秒。

从 web 应用程序调用或通过 curl 调用

curl 'https://<server>/hhdb_api/rpc/docket_creation' \
   -X 'POST' \
   -H 'Connection: keep-alive' \
   -H 'sec-ch-ua: " Not A;Brand";v="99","Chromium";v="90","Google Chrome";v="90"' \
   -H 'Prefer: params=single-object' \
   -H 'sec-ch-ua-mobile: ?0' \
   -H 'Authorization: Bearer $bearer_token' \
   -H 'Content-Type: application/json' \
   -H 'Accept: application/json,text/plain,*/*' \
   -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/90.0.4430.85 Safari/537.36' \
   -H 'DNT: 1' \
   -H 'Origin: https://hh-tincan-dev.srv.handheld.ie' \
   -H 'sec-fetch-site: same-site' \
   -H 'sec-fetch-mode: cors' \
   -H 'Sec-Fetch-Dest: empty' \
   -H 'Referer: https://hh-tincan-dev.srv.handheld.ie/' \
   -H 'Accept-Language: en-GB,en-US;q=0.9,en;q=0.8' \
   --data-raw '{"dockets":[{"docket":{},"docket_id":1428,"docket_buy_x_get_y_discounts":[]}]}' \
   --compressed

这将需要一分钟多的时间。

我希望这两个调用花费相似的时间。

我启用了广泛的日志记录以尝试跟踪这两种方法间的差异似乎是输入嵌套触发器时的延迟 ~

我在 PostgREST 的 GitHub 上提出了这个问题,并提供了一些配置细节:

https://github.com/PostgREST/postgrest/issues/1836

此问题现已解决,详情请参考 PostgREST github issue

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。