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 1ghz9R-0007iM-17 for pgsql-general@arkaria.postgresql.org; Fri, 11 Jan 2019 15:55:17 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ghz9O-0004yX-RY for pgsql-general@arkaria.postgresql.org; Fri, 11 Jan 2019 15:55:14 +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 1ghz9N-0004yQ-HT for pgsql-general@lists.postgresql.org; Fri, 11 Jan 2019 15:55:14 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ghz9I-0001CO-Ip for pgsql-general@postgresql.org; Fri, 11 Jan 2019 15:55:13 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id B0F87233C0; Fri, 11 Jan 2019 10:55:06 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Fri, 11 Jan 2019 10:55:06 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm1; bh=a BDo/o5mNAXNnmL/t3wCqMQmAmBnKsaYAP7Pg1zno+w=; b=80ZV9PX68Fc0MFWt1 8C+wORQXItifkYMfk+2caoYtjasbCAlRmVKfExzc5dzxd8ONg8mfif7+6R98UHzO JQ/MMggKLDAsAwTzAYKpNPkOiO3B1/+tBkFlWPg7owEj7B4fJwb+6AYFtxcPYHU1 s9bPBWxvbsIwukU8eaivoaaxb2J8nrmz+22oHIDXmFWMdgHJV7RtUHeAVdSarnRX WR2z/g8WQnq4zbuQNA1ry6dzN0OuQkiBrpHTDDsn89Hmx76IHqr8z6LrnCdssygK W3HjzZqoneEwPM5iprQe7CWSGEejSdf9zzgYuJPyVvuvKHpQX1GxN28hXka/XmN7 vrvbQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; bh=aBDo/o5mNAXNnmL/t3wCqMQmAmBnKsaYAP7Pg1zno +w=; b=YZU3WlY94BFxQbhoHIIhqyTgu5pXMH+8ctuSqGSqui+RTzHSr1jcyAjrW X1yzRnAWB8gUMM1BCnfFbhQkjy9ySXjmIr5Dm3rIo6vOV/I3oxT7J5kDZLlVt1Cx NCMJ0yoaO9dVTl1e7XbxsTATJFtInwYIJPfaCiyn2k7riyAMxz7sb35z6bWMY8x8 GpU0V6/nXeoxtGZFSs1iD64JawP7m8g/WVFX/m7A/vYGaosXo0daQ0/s/QqCPaNt 4WUJqx+v3yfASsI4U+Oxa81NAAOxsBVrdqDtemM11s5uyNaqYh3tFKlosOZp3pLD gUajRoE3txIO4hjbNVbuWeksUV0TQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedtledrfeehgdekfecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfhuthenuceurghilhhouhhtmecufedt tdenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujfgurhepuffvfhfhkffffg ggjggtgfesthekredttdefjeenucfhrhhomheptegurhhirghnucfmlhgrvhgvrhcuoegr ughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtohhmqeenucffohhmrghinhepud dtrddtrddtrddtpdgusgdqfhhiuggulhgvrdgtohhmpddurddvrdefrddtnecukfhppeej uddrvdduvddrudefgedrfeenucfrrghrrghmpehmrghilhhfrhhomheprggurhhirghnrd hklhgrvhgvrhesrghklhgrvhgvrhdrtghomhenucevlhhushhtvghrufhiiigvpedt X-ME-Proxy: Received: from [192.168.1.2] (unknown [71.212.134.3]) by mail.messagingengine.com (Postfix) with ESMTPA id C57FDE4598; Fri, 11 Jan 2019 10:55:05 -0500 (EST) Subject: Re: How to always run UPDATE FROM despite missing records in the source table? To: Alexander Farber , pgsql-general References: From: Adrian Klaver Message-ID: <1f962321-5973-fe09-e52f-182115296555@aklaver.com> Date: Fri, 11 Jan 2019 07:55:05 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.3.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 1/11/19 4:50 AM, Alexander Farber wrote: > Good afternoon > > I have prepared a simplified test case for my question: > https://www.db-fiddle.com/f/22jfWnsvqD8hVeFPXsyLbV/0 > > In PostgreSQL 10.6 there are 2 tables: > > CREATE TABLE users ( >         uid SERIAL PRIMARY KEY, >         created       timestamptz NOT NULL, >         visited       timestamptz NOT NULL, >         ip            inet        NOT NULL, >         lat           double precision, >         lng           double precision >   ); > >   CREATE TABLE geoip ( >         block   inet    PRIMARY KEY, >         lat     double precision, >         lng     double precision > ); > > CREATE INDEX ON geoip USING SPGIST (block); > > which are filled with the following test data: > > INSERT INTO users (created, visited, ip) VALUES >   (now(), now(), '1.2.3.4'::inet), >   (now(), now(), '1.2.3.5'::inet), >   (now(), now(), '1.2.3.6'::inet); > > INSERT INTO geoip (block, lat, lng) VALUES >  ('1.2.3.0/24 ', -33.4940, 143.2104), >  ('10.0.0.0/8 ', 34.6617, 133.9350); > > Then in a stored function I run the following UPDATE command - > > UPDATE users u SET >     visited = now(), >     ip      = '10.10.10.10'::inet, >     lat     = i.lat, >     lng     = i.lng > FROM geoip i > WHERE u.uid = 1 AND '10.10.10.10'::inet <<= i.block; > > (the 1 and the ip address are actually in_uid and in_ip parameters in my > stored function). > > The above query works well and updates all 4 fields in the users table. > > However the following query does not work as intended and does not > update any fields, because there is no matching block in the geoip table > found: > > UPDATE users u SET >     visited = now(),                  -- HOW TO ALWAYS UPDATE THIS FIELD? >     ip      = '20.20.20.20'::inet, -- HOW TO ALWAYS UPDATE THIS FIELD? >     lat     = i.lat, >     lng     = i.lng > FROM geoip i > WHERE u.uid = 2 AND '20.20.20.20'::inet <<= i.block; > > The field visited and ip however should be always updated - regardless > if the block was found or not. > > Kind of LEFT JOIN, but for UPDATE - how to achieve this please? > > The only workaround that I could think of is - > > UPDATE users SET >     visited = now(), >     ip      = '20.20.20.20'::inet, >     lat     = (SELECT lat FROM geoip WHERE '20.20.20.20'::inet <<= block), >     lng     = (SELECT lng FROM geoip WHERE '20.20.20.20'::inet <<= block) > WHERE uid = 2; > > But that would run the same subquery twice (correct?) and my geoip table > is already slow with 3073410 records (and that is why I am trying to > cache its lat and lng values in the users table on each user login event) Why not put a test for the block in the function and then use different UPDATE's depending on the result? Pseudo code: IF ip IN block THEN UPDATE users u SET visited = now(), ip = '10.10.10.10'::inet, lat = i.lat, lng = i.lng FROM geoip i WHERE u.uid = 1; ELSE UPDATE users u SET visited = now(), ip = '20.20.20.20'::inet FROM geoip i WHERE u.uid = 2 ; END IF; > > Regards > Alex -- Adrian Klaver adrian.klaver@aklaver.com