Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cKPAB-0004Dj-Dt for pgsql-general@arkaria.postgresql.org; Fri, 23 Dec 2016 12:41:31 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1cKPAB-0007nr-0o for pgsql-general@arkaria.postgresql.org; Fri, 23 Dec 2016 12:41:31 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1cKPAA-0007nk-94 for pgsql-general@postgresql.org; Fri, 23 Dec 2016 12:41:30 +0000 Received: from mail.hjp.at ([212.17.106.138] helo=rorschach.hjp.at) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1cKPA6-0007qW-OS for pgsql-general@postgresql.org; Fri, 23 Dec 2016 12:41:29 +0000 Received: by rorschach.hjp.at (Postfix, from userid 1000) id 27914A42; Fri, 23 Dec 2016 13:41:24 +0100 (CET) Date: Fri, 23 Dec 2016 13:41:24 +0100 From: "Peter J. Holzer" To: pgsql-general@postgresql.org Subject: Re: Is is safe to use SPI in multiple threads? Message-ID: <20161223124124.GB9067@hjp.at> References: MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="TRYliJ5NKNqkz5bu" Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.23 (2014-03-12) X-Pg-Spam-Score: -5.0 (-----) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-general Precedence: bulk Sender: pgsql-general-owner@postgresql.org --TRYliJ5NKNqkz5bu Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On 2016-12-09 16:52:05 +0800, Qiu Xiafei wrote: > I'm new to PG and want to implement my domain-specific system based on PG= =2E I > wish to arrange my data as several tables in database and translate my DS= L into > SQL statements for query. Since one DSL statement may be mapped to severa= l SQL > statements, it's better to push the DSL server as close to the PG server = as > possible. I found PG's backgroud worker meet my needs. I can setup a back= ground > worker bounded to PG server and listen to a port for network requests.=A0 >=20 > But I encounter a problem that the Server Programing Interfaces are not T= HREAD > SAFE. There are some global variables defined like:=A0SPI_processed, > SPI_tuptable, etc. This limit to my DSL server to work in single thread m= ode > which is quite inefficient. I had a similar requirement. I solved it by moving the application logic out of the stored procedures. All the stored procedure does is an RPC call (I use =D8MQ for that) to a server process and send the result back to the client. The server process converts the request into multiple SQL queries which can be processed in parallel. The downside is of course that the communication overhead is much higher (A minimum of 4 network messages per request). That's not a problem in my case, but you mileage may vary. The advantages in my opinion are: * A standalone server process is easier to test and debug than a bunch of stored procedures. * I can easily scale out if necessary: Currently my database and server process run on the same machine, but I could distribute them over several machines with (almost) no change in logic. hp --=20 _ | Peter J. Holzer | A coding theorist is someone who doesn't |_|_) | | think Alice is crazy. | | | hjp@hjp.at | -- John Gordon __/ | http://www.hjp.at/ | http://downlode.org/Etext/alicebob.html --TRYliJ5NKNqkz5bu Content-Type: application/pgp-signature; name="signature.asc" Content-Description: Digital signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1 iQIcBAEBAgAGBQJYXRt0AAoJEPIOSFES/ihdv6sP/2Nxui/QJjknzt/iYiyuXHAi AWP4WmfEtk0RvT85ZmYskDGib/h/TONEP6V2tgs7PwTKi0dd2YJIwXbiW9zCLpLt mWTHpkHHtIO93ut4t92/4f6AQFXwgCczpIVIL4hJckHvswYKe5HoVZ+j6ACwlYHv SSMebDiQHZMllklAEO0WYU7IEbD5HPT5gWBEODBVCURPqxAk5DHRgdTnuQdg8e1p KOzWyTZx4QKGNJEvjWbWHX0xn0bgbF/bcta/CB6ZEr7nt+irisZXJHcz4sqJJ3Ce VYAmEaHqKK3WfZxo7T+6e536Lrs65usKdTP4Xv9cPiHFTFxikrg0LQJA/kRdt2Nt uH7xts28WeWAxCszEYgpmkX2V1XdwFR6cgoHM5hMYMrygE6CQcmq6I5oXAmBHNZ+ 9lR5UKyJccMYAhiW4+TVxCDxfBRtaBXQtPXxqRajAAQdhYzaxTJOrscmmDd90qem sfJPbrxel9r70k5gpb82lmeEkz/+pQCsd4u0pnfiwbngr9V/f2nDkECwpw1JdVm4 ZftIvLlnBUs47BQlaQHTcbpVPl3HYv9r6w5M9n7ugGoEnyBMa/W5g8B45j9nyLEz jn+zg7BqHEGfcf5Ld3LnHOGG5q0sFwRTbXrCyN63ZuLVXggP2Lhl/fTWiAHnJxd3 L/6hKJunshFhkCm1ls+2 =KJOf -----END PGP SIGNATURE----- --TRYliJ5NKNqkz5bu--