X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 56DFA3A4275 for ; Thu, 13 Jan 2005 08:44:16 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 94150-03 for ; Thu, 13 Jan 2005 08:44:03 +0000 (GMT) Received: from mx-00.sil.at (mx-00.sil.at [62.116.68.196]) by svr1.postgresql.org (Postfix) with ESMTP id 9D94B3A5CE6 for ; Thu, 13 Jan 2005 08:44:02 +0000 (GMT) Received: (qmail-ldap/ctrl 4339 invoked from network); 13 Jan 2005 08:44:00 -0000 Received: from unknown (HELO jupiter.sonorys.at) ([213.235.243.42]) (envelope-sender ) by mx-00.sil.at (qmail-ldap-1.03) with SMTP for ; 13 Jan 2005 08:44:00 -0000 Received: from neptun.sonorys.at (neptun.sonorys.at [172.24.1.17]) by jupiter.sonorys.at (8.11.7+Sun/8.11.7) with ESMTP id j0D8iAs14270 for ; Thu, 13 Jan 2005 09:44:10 +0100 (MET) Received: by neptun.sonorys.at with Internet Mail Service (5.5.2653.19) id ; Thu, 13 Jan 2005 09:44:02 +0100 Message-ID: From: =?iso-8859-1?Q?K=D6PFERL_Robert?= To: Cc: "'pgsql-sql@postgresql.org'" Subject: Re: Implementing queue semantics (novice) Date: Thu, 13 Jan 2005 09:43:58 +0100 MIME-Version: 1.0 X-Mailer: Internet Mail Service (5.5.2653.19) Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable X-Virus-Scanned: by amavisd-new at hub.org X-Archive-Number: 200501/74 X-Sequence-Number: 19862 Thank you for this rather detailed example. I already learned something and omitted a fault. There should be enogh = to implement such a Queue. :-) > -----Original Message----- > From: Andrew Hammond [mailto:ahammond@ca.afilias.info] > Sent: Mittwoch, 12. J=E4nner 2005 17:19 > To: K=D6PFERL Robert > Cc: 'pgsql-sql@postgresql.org' > Subject: Re: [SQL] Implementing queue semantics (novice) >=20 >=20 > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 >=20 > The name for what you're looking to build is a concurrent batch > processing system. Here's a basic one. >=20 > - -- adding processes >=20 > BEGIN; >=20 > INSERT INTO queue (queue_id, processing_pid, processing_start, > ~ processing_status, foreign_id) > VALUES (DEFAULT, NULL, NULL, > ~ (SELECT queue_status_id FROM queue_status WHERE name =3D = 'pending'), > ~ foreign_id); >=20 > COMMIT; >=20 >=20 > - -- removing processes >=20 > BEGIN; >=20 > SELECT queue_id, foreign_id FROM queue > WHERE processing_status =3D (SELECT queue_status_id FROM queue_status > ~ WHERE name =3D 'pending') > ORDER BY queue_id LIMIT 1 > FOR UPDATE; >=20 > UPDATE queue > SET processing_pid =3D ?, > ~ processing_start =3D now(), > ~ processing_status =3D (SELECT queue_status_id FROM queue_status = WHERE > ~ name =3D 'active') > WHERE id =3D ?; >=20 > COMMIT; >=20 > - -- client code does whatever it's going to do here >=20 > BEGIN; >=20 > SELECT 1 FROM queue > WHERE queue_id =3D ? AND processing_pid =3D ? > FOR UPDATE; >=20 > - -- confirm that it exists >=20 > DELETE FROM queue WHERE queue_id =3D ? >=20 > INSERT INTO queue_history (queue_id, processing_pid, = processing_start, > ~ processing_complete, processing_status, foreign_id) > VALUES (queue_id, processing_pid, processing_start, now(), > ~ (SELECT queue_status_id FROM queue_status WHERE name =3D 'done'), > ~ foreign_id); >=20 > COMMIT; >=20 > - -- a seperate process reaps orphaned entries should processing = fail. >=20 > BEGIN; > SELECT queue_id, processing_pid FROM queue > WHERE now() - processing_start > 'some reasonable interval'::interval > AND processing_status =3D (SELECT queue_status_id FROM=20 > queue_status WHERE > ~ name =3D 'active' FOR UPDATE; >=20 > - -- for each entry, check to see if the PID is still running >=20 > UPDATE queue > SET > ~ processing_pid =3D NULL, > ~ processing_start =3D NULL, > ~ processing_status =3D (SELECT id FROM queue_status WHERE name=20 > =3D 'pending') > WHERE id =3D ?; >=20 > COMMIT; >=20 > There are more complicated approaches available. If you plan to have > multiple machines processing, you probably want to add a=20 > processing_node > entry too. >=20 >=20 > K=D6PFERL Robert wrote: > | Hi, > | > | since I am new to writing stored procedures I'd like to ask=20 > first bevore I > | do a mistake. > | > | I want to implement some kind of queue (fifo). There are n=20 > users/processes > | that add new records to a table and there are m consumers=20 > that take out > | these records and process them. > | It's however possible for a consumer to die or loose=20 > connection while > | records must not be unprocessed. They may rather be processed = twice. > | > | This seems to me as a rather common problem. But also with > atomicy-holes to > | fall into. > | How is this commonly implemented? > | > | > | I can imagine an 'add' and a 'get' function together with=20 > one aditional > | 'processed' timestamp-column? > | > | > | > | Thanks for helping me do the right. > | > | ---------------------------(end of=20 > broadcast)--------------------------- > | TIP 4: Don't 'kill -9' the postmaster >=20 >=20 > - -- > Andrew Hammond 416-673-4138 ahammond@ca.afilias.info > Database Administrator, Afilias Canada Corp. > CB83 2838 4B67 D40F D086 3568 81FC E7E5 27AF 4A9A > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.2.5 (GNU/Linux) >=20 > iD8DBQFB5U3kgfzn5SevSpoRAoesAKCAZkr61I5knCw9tIr8rlO0xri7YACgifrn > N01nXZY8UKmIlTnGkngHKUo=3D > =3DUXRk > -----END PGP SIGNATURE----- >=20