Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f8lzF-0001d6-VC for pgsql-general@arkaria.postgresql.org; Wed, 18 Apr 2018 12:14:58 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1f8lzE-0007hH-M0 for pgsql-general@arkaria.postgresql.org; Wed, 18 Apr 2018 12:14:56 +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.89) (envelope-from ) id 1f8lzE-0007h6-8n for pgsql-general@lists.postgresql.org; Wed, 18 Apr 2018 12:14:56 +0000 Received: from mail-yb0-x22d.google.com ([2607:f8b0:4002:c09::22d]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1f8lz9-0007o2-Dd for pgsql-general@postgresql.org; Wed, 18 Apr 2018 12:14:54 +0000 Received: by mail-yb0-x22d.google.com with SMTP id z16-v6so486749ybk.13 for ; Wed, 18 Apr 2018 05:14:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=to:from:subject:message-id:date:user-agent:mime-version :content-transfer-encoding:content-language; bh=VHnfvdW7XnlAAyxSOB9OBZ6g7tpGF1ZMoeVahvs+ZOw=; b=Upuwtd6Z8tHMpnw3mg8KfocuQIaiKGorZZJ2Wxg+7bqCESXi2Ca3bPQ2IFN4UbITW7 ccaWSnXHXpjhTpjG/1PAcj3r614r7tWSPeJfg759x9pDoPUF/hPhKq63cuyWqzrKtUts DpGCUJwLmgPUi6Ki3UHWABY80RqanOQJJ/c+T3UVwSxS8K/xNx/d6qVNP3JB5cQw5PN2 zej5VFk6vSpplXSI+92KkH0yfQTGx1AYeZCvcyMELaiGysSccrctNNIWls7Uvr6+lkL6 ZdSMMkhwrZhAQIjuGTBmAKUFGXl8prp0L1N3WliAquGzBloodQqnHvc3caRqwhTUvN4r ZyMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:from:subject:message-id:date:user-agent :mime-version:content-transfer-encoding:content-language; bh=VHnfvdW7XnlAAyxSOB9OBZ6g7tpGF1ZMoeVahvs+ZOw=; b=Cib5s93HmZkPJI1zvync0j5K1mWYBjiNbFFWOol5nq+jUZ+XQhd31nBDSQQln0kp+n 8Z5ac/cX0oPW8oSUe48u56KH0dFwMoTUhSlPla7FbqViupYB/hvnVEw8XMiGHoVtU7FF YdW1N9X2D+BrdiILDoALHepiG/We4yUkYjXFUfDkKlLsTK84OwanJkCt8yc+AyrkXmy0 eGh6KQWYvPdmUYwpITW2TH7Nj8ahC+oVDGdNZWrqZz41aHfZ3ZGCMBLtyOeY6dxuyLVc 0+hBxmx+EcjkpKrLtd8j5eCSvnWfajULXFx123FEkbEQOcs8rxHx9aXFd0tK/O5/CWNk Mtkw== X-Gm-Message-State: ALQs6tD5UWcHbEeypVIEAUpf/NtdgWOYqRqkNEOco6bsb6y2bkuUxgfr z1QPvlXBoXAfrTUSEMVrbBKigtLN X-Google-Smtp-Source: AIpwx48lGIZzwVoXQKUt7V9kvU/SwGrS7SMPehTKdAN3BMK53tRDT4Vp+nitzwiavXD5MbNHw86Oyg== X-Received: by 2002:a25:9247:: with SMTP id e7-v6mr866503ybo.400.1524053689601; Wed, 18 Apr 2018 05:14:49 -0700 (PDT) Received: from [192.168.1.194] ([193.93.78.27]) by smtp.gmail.com with ESMTPSA id l1-v6sm376627ywh.62.2018.04.18.05.14.48 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 18 Apr 2018 05:14:49 -0700 (PDT) To: Postgres General From: Vitaliy Garnashevich Subject: Tricking the optimizer Message-ID: <612dae0f-16b3-e5f8-7458-1ec38aea163c@gmail.com> Date: Wed, 18 Apr 2018 15:15:50 +0300 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.7.0 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hi, In order to implement some security features in our application, we sometimes append additional WHERE filters to our queries, so the filer in the end looks like: SELECT ... FROM ... WHERE securityFilter1 AND securityFilter2 AND securityFilter3 In the EXPLAIN the filters look something like: (((SubPlan 1) = 'foo') AND ((SubPlan 2) = 'bar') AND ((SubPlan 3) = 'baz')) There are no applicable indexes and each filter clause looks opaque to the optimizer, so the optimizer is using some generic rules and assumes that each AND clause would reduce the total number of returned rows by some factor. The problem is that this is not usually the case, and usually the clauses would not filter out any rows at all. This leads to poor plans, when such query is a part of another bigger query. Is there any trick to craft the query in such a way, so that to make the optimizer believe that the filters would not remove any rows, and all rows will likely be returned by the query? Regards, Vitaliy