Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rndXL-00EGEY-Tv for pgsql-general@arkaria.postgresql.org; Fri, 22 Mar 2024 11:58:16 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1rndXJ-002Yt6-VQ for pgsql-general@arkaria.postgresql.org; Fri, 22 Mar 2024 11:58:14 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rndJP-002WL2-5R for pgsql-general@lists.postgresql.org; Fri, 22 Mar 2024 11:43:51 +0000 Received: from mout-y-209.mailbox.org ([91.198.250.237]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rndJK-005q1D-VT for pgsql-general@lists.postgresql.org; Fri, 22 Mar 2024 11:43:50 +0000 Received: from smtp2.mailbox.org (smtp2.mailbox.org [10.196.197.2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-y-209.mailbox.org (Postfix) with ESMTPS id 4V1L7f3VB1z9vW0 for ; Fri, 22 Mar 2024 12:43:42 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mailbox.org; s=mail20150812; t=1711107822; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=Dfj0tcuF+pS9uyNkSIkr7w5h5bwYLXTsAXtX+R3qOnM=; b=m8qGmKquCJVKgCRDixSfhaq0P3rTToJ3ivR0k3OEOjEdwyT1a8vGZRa8gp6tSUeWw3g6OF WtDikn2OUz4VLZCDF8sCQHJayZrGq6kkEday1p4cPOgAZXQXDmIW23/aPOkLc6KfLYNChW PjK57IS21cZxnA96R6Nmqt0rPRLiIGqjeoYC2mARR9cvXsTqkyQeN1xLlfgu2NT5+SNxSu JGXvRAaSi4XfkROMS3NhhFk5SsgTeGpvteqILnEtPW9FFlcrrsonT4+9raAGxVntmPYMOO a+YcpnoL4CCYFY+sORGGJ7st8XWP1NJo1VwDPpm7UFapGTKW8FfmBpN5978OtA== Message-ID: <90ee725f-d003-4838-97ec-e7e4149ac75b@mailbox.org> Date: Fri, 22 Mar 2024 12:43:40 +0100 MIME-Version: 1.0 From: ushi Subject: PostgreSQL as advanced job queuing system To: pgsql-general@lists.postgresql.org Content-Language: de-DE Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-MBO-RS-ID: 4cb6b3e06430788a831 X-MBO-RS-META: pyrxa74x8emgddwfbx1tto9ran3umjed List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hello List, i am playing with the idea to implement a job queuing system using PostgreSQL. To meet requirements the system needs to offer some advanced features compared to "classic" queuing systems: - users can create new queues at any time - queues have priorities - priorities of queues can change at any time - jobs in queues with the highest priority should be processed first A simple schema could look like this: create table queues ( id integer primary key, priority integer not null default 0 ); create table jobs ( id serial primary key, queue_id integer not null references queues(id) ); Right now i am stuck writing an efficient query for dequeuing. This is what i got so far: insert into queues (id, priority) values (1, 0), (2, 1), (3, 1); insert into jobs (queue_id) select 1 from generate_series(1, 1000000); insert into jobs (queue_id) select 2 from generate_series(1, 1000000); insert into jobs (queue_id) select 3 from generate_series(1, 1000000); Here is a simple dequeuing query that is super efficient, but ignores priority: select * from jobs limit 1 for update of jobs skip locked; -- id | queue_id -- ----+---------- -- 1 | 1 -- (1 row) -- -- Time: 2.641 ms This is my naive query obeying priority. This is very slow and i could not get it any faster: select * from jobs join queues on queues.id = jobs.queue_id order by priority desc limit 1 for update of jobs skip locked; -- id | queue_id | id | priority -- ---------+----------+----+---------- -- 1000001 | 2 | 2 | 1 -- (1 row) -- -- Time: 1116.631 ms (00:01.117) Here is the query plan: -- QUERY PLAN -- ----------------------------------------------------------------------------------------------------------------------------------------- -- Limit (cost=66225.61..66225.63 rows=1 width=28) (actual time=1333.139..1333.142 rows=1 loops=1) -- -> LockRows (cost=66225.61..103725.61 rows=3000000 width=28) (actual time=1333.137..1333.139 rows=1 loops=1) -- -> Sort (cost=66225.61..73725.61 rows=3000000 width=28) (actual time=1333.110..1333.112 rows=1 loops=1) -- Sort Key: queues.priority DESC -- Sort Method: external merge Disk: 111568kB -- -> Hash Join (cost=60.85..51225.61 rows=3000000 width=28) (actual time=0.064..660.317 rows=3000000 loops=1) -- Hash Cond: (jobs.queue_id = queues.id) -- -> Seq Scan on jobs (cost=0.00..43275.00 rows=3000000 width=14) (actual time=0.027..253.868 rows=3000000 loops=1) -- -> Hash (cost=32.60..32.60 rows=2260 width=14) (actual time=0.021..0.022 rows=3 loops=1) -- Buckets: 4096 Batches: 1 Memory Usage: 33kB -- -> Seq Scan on queues (cost=0.00..32.60 rows=2260 width=14) (actual time=0.011..0.013 rows=3 loops=1) -- Planning Time: 0.430 ms -- Execution Time: 1347.448 ms -- (13 rows) Any ideas for a more efficient implementation? Thank you for your time, ushi