Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 770D563220C for ; Thu, 10 Dec 2009 19:20:41 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 51496-05 for ; Thu, 10 Dec 2009 23:20:33 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mail1.sea5.speakeasy.net (mail1.sea5.speakeasy.net [69.17.117.3]) by mail.postgresql.org (Postfix) with ESMTP id B52BF634F28 for ; Thu, 10 Dec 2009 19:20:33 -0400 (AST) Received: (qmail 25816 invoked from network); 10 Dec 2009 23:20:31 -0000 Received: from 25-76-42-72.skybeam.com (HELO [192.168.2.16]) (scott_ribe@killerbytes.com@[72.42.76.25]) (envelope-sender ) by mail1.sea5.speakeasy.net (qmail-ldap-1.03) with DES-CBC3-SHA encrypted SMTP for ; 10 Dec 2009 23:20:31 -0000 User-Agent: Microsoft-Entourage/12.20.0.090605 Date: Thu, 10 Dec 2009 16:20:28 -0700 Subject: Re: Cheapest way to poll for notifications? From: Scott Ribe To: Craig Ringer CC: PG-General Mailing List Message-ID: Thread-Topic: [GENERAL] Cheapest way to poll for notifications? Thread-Index: Acp571vuJG8t7SAmekmjYAdjpY0IXg== In-Reply-To: <4B211E6F.1070700@postnewspapers.com.au> Mime-version: 1.0 Content-type: text/plain; charset="US-ASCII" Content-transfer-encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.599 tagged_above=-10 required=5 tests=BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 200912/457 X-Sequence-Number: 156677 > Right? Right. The way I do it, very roughly: Pqconnectdb(...) PQexec( mDbConn, "listen notify" ); int dbsock = PQsocket( mDbConn ); mKq = kqueue(); struct kevent kev[1], kevs[1]; EV_SET( &kev[0], dbsock, EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0, 0 ); kevent( mKq, kev, 1, NULL, 0, NULL ); while( true ) { bool needsnotify = false; int evtcnt = kevent( mKq, NULL, 0, kevs, 1, NULL ); if( evtcnt == 1 && kevs[i].filter == EVFILT_READ && kevs[i].ident == dbsock ) { while( true ) { PQconsumeInput( mDbConn ); PGnotify * notify = PQnotifies( mDbConn ); if( notify ) { free( notify ); needsnotify = true; } else break; } } if( needsnotify ) // go off & query the db & handle updates here } Of course you could also use select to wait on the socket. -- Scott Ribe scott_ribe@killerbytes.com http://www.killerbytes.com/ (303) 722-0567 voice