動作するが恐ろしく遅いストアド プロシージャがあります。
基本的に私がやりたいことは、単一の更新コマンドでサブクエリから取得した一連の行を更新することです。もう 1 つの注意点は、ステートメントで更新した行を返したいということです。
現在、ループを使用して単一の行を取得して更新し、returning を使用して結果を保存していますが、これは機能しますが、非常に遅いです。
提案?
以下は、現在の作業バージョンとスキーマ作成ステートメントです。
CREATE TABLE "queued_message"
(
id bigserial NOT NULL,
body json NOT NULL,
status character varying(50) NOT NULL,
queue character varying(150) NOT NULL,
last_modified timestamp without time zone NOT NULL,
CONSTRAINT id_pkey PRIMARY KEY (id)
);
CREATE TYPE returned_message as (id bigint, body json, status character varying(50) , queue character varying(150), last_modified timestamp without time zone);
CREATE OR REPLACE FUNCTION get_next_notification_message_batch(desiredQueue character varying(150), numberOfItems integer)
RETURNS SETOF returned_message AS $$
DECLARE result returned_message; messageCount integer := 1;
BEGIN
lock table queued_notification_message in exclusive mode;
LOOP
update queued_notification_message
set
status='IN_PROGRESS', last_modified=now()
where
id in (
select
id
from
queued_notification_message
where
status='SUBMITTED' and queue=desiredQueue
limit 1
)
returning * into result;
RETURN NEXT result;
messageCount := messageCount+1;
EXIT WHEN messageCount > numberOfItems;
END LOOP;
END;$$LANGUAGE plpgsql;