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 1gi01t-0003nW-9U for pgsql-general@arkaria.postgresql.org; Fri, 11 Jan 2019 16:51:33 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gi01r-0006Xq-Gm for pgsql-general@arkaria.postgresql.org; Fri, 11 Jan 2019 16:51:31 +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 1gi01r-0006Xj-84 for pgsql-general@lists.postgresql.org; Fri, 11 Jan 2019 16:51:31 +0000 Received: from lungold.riddles.org.uk ([82.68.208.19]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gi01o-0003U8-RH for pgsql-general@postgresql.org; Fri, 11 Jan 2019 16:51:30 +0000 Received: from [192.168.127.1] (port=22257 helo=caithnard.riddles.org.uk) by lungold.riddles.org.uk with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.88 (FreeBSD)) (envelope-from ) id 1gi01m-000Jlv-Sa; Fri, 11 Jan 2019 16:51:26 +0000 Received: from [127.0.0.1] (port=19968 helo=caithnard.riddles.org.uk) by caithnard.riddles.org.uk with esmtp (Exim 4.89 (FreeBSD)) (envelope-from ) id 1gi01m-0004AI-0J; Fri, 11 Jan 2019 16:51:26 +0000 From: Andrew Gierth To: Alexander Farber Cc: pgsql-general Subject: Re: How to always run UPDATE FROM despite missing records in the source table? In-Reply-To: (Alexander Farber's message of "Fri, 11 Jan 2019 13:50:38 +0100") Message-ID: <87k1jbno3w.fsf@news-spur.riddles.org.uk> References: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (berkeley-unix) Date: Fri, 11 Jan 2019 16:51:25 +0000 MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk >>>>> "Alexander" == Alexander Farber writes: Alexander> However the following query does not work as intended and Alexander> does not update any fields, because there is no matching Alexander> block in the geoip table found: Alexander> UPDATE users u SET Alexander> visited = now(), -- HOW TO ALWAYS UPDATE THIS FIELD? Alexander> ip = '20.20.20.20'::inet, -- HOW TO ALWAYS UPDATE THIS FIELD? Alexander> lat = i.lat, Alexander> lng = i.lng Alexander> FROM geoip i Alexander> WHERE u.uid = 2 AND '20.20.20.20'::inet <<= i.block; Alexander> The field visited and ip however should be always updated - Alexander> regardless if the block was found or not. Alexander> Kind of LEFT JOIN, but for UPDATE - how to achieve this please? It can be done like this (this assumes you want to preserve the previous values of u.lat/u.lng if the block was not found; if you want to set them to null instead, then remove the coalesce() calls): UPDATE users u SET visited = now(), ip = v.ip, lat = coalesce(i.lat, u.lat), lng = coalesce(i.lng, u.lng) FROM (VALUES ('20.20.20.20'::inet)) v(ip) LEFT JOIN geoip i ON (v.ip <<= i.block) WHERE u.uid = 2; Alexander> But that would run the same subquery twice (correct?) and my Alexander> geoip table is already slow with 3073410 records Slow even with a gist or spgist index? what does the explain analyze look like? (You could also try using the ip4r module; I've not done any serious benchmarking to see if it's faster than the built-in index types, though it has some theoretical advantages due to not being restricted to CIDR ranges. In pg versions before the built-in inet type got a gist index method, ip4r was _the_ way to do ip block lookups for geoip etc.) -- Andrew (irc:RhodiumToad)