Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hsRw2-0001Wd-MU for pgsql-hackers@arkaria.postgresql.org; Tue, 30 Jul 2019 13:12:59 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hsRw1-0005BT-A4 for pgsql-hackers@arkaria.postgresql.org; Tue, 30 Jul 2019 13:12:57 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hsRw0-0005BL-UZ for pgsql-hackers@lists.postgresql.org; Tue, 30 Jul 2019 13:12:57 +0000 Received: from mail-wm1-x344.google.com ([2a00:1450:4864:20::344]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hsRvt-0004fV-Gp for pgsql-hackers@postgresql.org; Tue, 30 Jul 2019 13:12:56 +0000 Received: by mail-wm1-x344.google.com with SMTP id s15so35248171wmj.3 for ; Tue, 30 Jul 2019 06:12:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to :user-agent; bh=8ah1yG1atVsq13inAre517uhrUOrkA/L+Xhac1+pYCc=; b=eOlX2EJAEVtD+bgip+nxpSmynvajAkRMzjPw6gahKAOvvDI8MTcYGDoO0GznHP5nm3 OadHxTT+qxAdsu3N7gZ/iJTMMygQ6KcWebEJMsa6W1+AtOJTKk6K541KMHKiYbE6bYka j7u/oJ8VM/mRqR1yfuS0NR5mZv9gZ3n2aR2Fw9W2lmoD+vbM+fd2M64nIJsDfO0Vv9Or +jh7gQYY1AoIOgj1hksuu+JUx/QjWweomFCnRh2grfGWbEm1XKqUGzoqwBVI4wgVYLJW mKNvL3E01bNAg17Meb2rUd4hpcMKUh1aRcGLNaxJynNMGIggMeliC2qkQERgi6D7z8f1 0dNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to:user-agent; bh=8ah1yG1atVsq13inAre517uhrUOrkA/L+Xhac1+pYCc=; b=eqWZpgTS4GJg7IBerJwS2FmVxayR0MFQJ/EZYW6naZmpSzVgyydiZiiYL/66eldu8p eynhsut0XUDr4A3ZHdl5U+mvjBMmmUek7clEo0pefnwAt7erT7UJuohgc8gigQEh4xGT HmWWy9ADZtp/wrnMr201fh0koI1uWgisQXrXoMrb8ZRnbVWXFvTwJDGtRJhUPi3xJGsw F9rMfYhi5hR7KzuzQJ8WPbMmr11rAnYK+FXb+ylZu0sVcIko9yKnR5ZiK+J7HpHfyGvm IaWqUeOLszdrlVZtyrt42IRkWv4BUc5adxCs4m6nkOS92h6BBjwxWBVLRi5b5QVDwZBk 2HCA== X-Gm-Message-State: APjAAAXUvuPbggTC5O34cgW5ti0ah93wgKkVDb/hr8y3iuyCLKD3hxJ8 BKILxSpTbhxgzpswlzSWYxqf9w== X-Google-Smtp-Source: APXvYqw3E+9Zhme43C7vvHYMNxdXZPqdQw9/JxCaYAD0ZF8J/7XcfYVF4jyWN3audUCaTKaL9MAuKw== X-Received: by 2002:a1c:a7c6:: with SMTP id q189mr106827442wme.146.1564492368476; Tue, 30 Jul 2019 06:12:48 -0700 (PDT) Received: from localhost (static-84-42-175-93.net.upcbroadband.cz. [84.42.175.93]) by smtp.gmail.com with ESMTPSA id y10sm53461996wmj.2.2019.07.30.06.12.47 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Tue, 30 Jul 2019 06:12:47 -0700 (PDT) Date: Tue, 30 Jul 2019 15:12:46 +0200 From: Tomas Vondra To: Konstantin Knizhnik Cc: Ryan Lambert , Thomas Munro , PostgreSQL Hackers , Bruce Momjian , Dimitri Fontaine Subject: Re: Built-in connection pooler Message-ID: <20190730131246.ihphwoy4sywfdhib@development> References: <42c78b0e-e2ad-7920-3b31-37a81c9ba83b@postgrespro.ru> <20190726202437.2svtvcisiujqaoky@development> <0d7a1ca4-0527-c502-28ae-842aa56ca10b@postgrespro.ru> <20190730010240.tktomjoio2efgcx3@development> <22e10415-abd3-c75d-6b70-f8789c7b2db8@postgrespro.ru> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1; format=flowed Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <22e10415-abd3-c75d-6b70-f8789c7b2db8@postgrespro.ru> User-Agent: NeoMutt/20180716-1444-295967-dirty List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Tue, Jul 30, 2019 at 01:01:48PM +0300, Konstantin Knizhnik wrote: > > >On 30.07.2019 4:02, Tomas Vondra wrote: >> >>My idea (sorry if it wasn't too clear) was that we might handle some >>cases more gracefully. >> >>For example, if we only switch between transactions, we don't quite care >>about 'SET LOCAL' (but the current patch does set the tainted flag). The >>same thing applies to GUCs set for a function. >>For prepared statements, we might count the number of statements we >>prepared and deallocated, and treat it as 'not tained' when there are no >>statements. Maybe there's some risk I can't think of. >> >>The same thing applies to temporary tables - if you create and drop a >>temporary table, is there a reason to still treat the session as tained? >> >> > >I already handling temporary tables with transaction scope (created >using "create temp table ... on commit drop" command) - backend is not >marked as tainted in this case. >Thank you for your notice about "set local" command - attached patch >is also handling such GUCs. > Thanks. > >>>To implement prepared statements  we need to store them in session >>>context or at least add some session specific prefix to prepare >>>statement name. >>>Temporary tables also require per-session temporary table space. >>>With GUCs situation is even more complicated - actually most of >>>the time in my PgPro-EE pooler version >>>I have spent in the fight with GUCs (default values, reloading >>>configuration, memory alllocation/deallocation,...). >>>But the "show stopper" are temporary tables: if them are accessed >>>through internal (non-shared buffer), then you can not reschedule >>>session to some other backend. >>>This is why I have now patch with implementation of global >>>temporary tables (a-la Oracle) which has global metadata and are >>>accessed though shared buffers (which also allows to use them >>>in parallel queries). >>> >> >>Yeah, temporary tables are messy. Global temporary tables would be nice, >>not just because of this, but also because of catalog bloat. >> > >Global temp tables solves two problems: >1. catalog bloating >2. parallel query execution. > >Them are not solving problem with using temporary tables at replica. >May be this problem can be solved by implementing special table access >method for temporary tables. >But I am still no sure how useful will be such implementation of >special table access method for temporary tables. >Obviously it requires much more efforts (need to reimplement a lot of >heapam stuff). >But it will allow to eliminate MVCC overhead for temporary tuple and >may be also reduce space by reducing size of tuple header. > Sure. Temporary tables are a hard issue (another place where they cause trouble are 2PC transactions, IIRC), so I think it's perfectly sensible to accept the limitation, handle cases that we can handle and see if we can improve the remaining cases later. > > >> >>>If Postgres backend is able to work only with on database, then >>>you will have to start at least such number of backends as number >>>of databases you have. >>>Situation with users is more obscure - it may be possible to >>>implement multiuser access to the same backend (as it can be done >>>now using "set role"). >>> Yes, that's a direct consequence of the PostgreSQL process model. >> >>I don't think I've said we need anything like that. The way I'd expect >>it to work that when we run out of backend connections, we terminate >>some existing ones (and then fork new backends). > >I afraid that it may eliminate most of positive effect of session >pooling if we will  terminate and launch new backends without any >attempt to bind backends to database and reuse them. > I'm not sure I understand. Surely we'd still reuse connections as much as possible - we'd still keep the per-db/user connection pools, but after running out of backend connections we'd pick a victim in one of the pools, close it and open a new connection. We'd need some logic for picking the 'victim' but that does not seem particularly hard - idle connections first, then connections from "oversized" pools (this is one of the reasons why pgbouncer has min_connection_pool). >> >>>So I am not sure that if we implement sophisticated configurator >>>which allows to specify in some configuration file for each >>>database/role pair maximal/optimal number >>>of workers, then it completely eliminate the problem with multiple >>>session pools. >>> >> >>Why would we need to invent any sophisticated configurator? Why couldn't >>we use some version of what pgbouncer already does, or maybe integrate >>it somehow into pg_hba.conf? > >I didn't think about such possibility. >But I suspect many problems with reusing pgbouncer code and moving it >to Postgres core. > To be clear - I wasn't suggesting to copy any code from pgbouncer. It's far too different (style, ...) compared to core. I'm suggesting to adopt roughly the same configuration approach, i.e. what parameters are allowed for each pool, global limits, etc. I don't know whether we should have a separate configuration file, make it part of pg_hba.conf somehow, or store the configuration in a system catalog. But I'm pretty sure we don't need a "sophisticated configurator". >>I also agree that more monitoring facilities are needed. >>>Just want to get better understanding what kind of information we >>>need to monitor. >>>As far as pooler is done at transaction level, all non-active >>>session are in idle state >>>and state of active sessions can be inspected using pg_stat_activity. >>> >> >>Except when sessions are tainted, for example. And when the transactions >>are long-running, it's still useful to list the connections. >> >Tainted backends are very similar with normal postgres backends. >The only difference is that them are still connected with client >though proxy. >What I wanted to say is that pg_stat_activity will show you >information about all active transactions >even in case of connection polling.  You will no get information about >pended sessions, waiting for >idle backends. But such session do not have any state (transaction is >not started yet). So there is no much useful information >we can show about them except just number of such pended sessions. > I suggest you take a look at metrics used for pgbouncer monitoring. Even when you have pending connection, you can still get useful data about that (e.g. average wait time to get a backend, maximum wait time, ...). Furthermore, how will you know from pg_stat_activity whether a connection is coming through a connection pool or not? Or that it's (not) tainted? Or how many backends are used by all connection pools combined? Because those are questions people will be asking when investigating issues, and so on. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services