Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pQmjR-0004Nk-E9 for pgsql-hackers@arkaria.postgresql.org; Sat, 11 Feb 2023 10:03:45 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pQmjP-0006v5-74 for pgsql-hackers@arkaria.postgresql.org; Sat, 11 Feb 2023 10:03:43 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pQmjO-0006uw-S7 for pgsql-hackers@lists.postgresql.org; Sat, 11 Feb 2023 10:03:42 +0000 Received: from mail-ej1-x633.google.com ([2a00:1450:4864:20::633]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pQmjM-0006cA-7C for pgsql-hackers@postgresql.org; Sat, 11 Feb 2023 10:03:42 +0000 Received: by mail-ej1-x633.google.com with SMTP id sa10so21387633ejc.9 for ; Sat, 11 Feb 2023 02:03:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=mBCB+emYr9x4lEvHRXLVCoazq8drnum8z3+/mICMwyg=; b=R75BsGrt7V8HI/5nNl+Qu3aVZFlVa3DAregg7x/9QSn5X4SaJjelp8DS72UUuAzyv/ 3BGpLsxBKWTo7UiZQs+SxmqYS8rI4AfHO50tVSS4PRLw+hHgwzJHnulbMZFMenrAdmLA 5XMzEDPQ3KjWuqjh3FSjJNHvIHSnqNw58nE5+MM+PksOKXFxCG5A+h+CmpRDlCYoWMWZ zr+D6nEYszcUtGQHU/koM3tA8ShQ2fRFsxHG+WwDSPnPB6Z5IPGFxmOdGMcqBgJc4WFq ZU82QnmLP9RPUghnFJY6t/IO9dvTVbjCp8j8qoFYKnOP4nBH3X3m9npIeCn20/ZAI6xa 0Y5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=mBCB+emYr9x4lEvHRXLVCoazq8drnum8z3+/mICMwyg=; b=WUQPUScNwvWNWDCBH8PuPPdv+LVIFx5scMYSotjCGNRuvERHmEKjIvXQmamAJXE6wi 5INbN8b+xV2FQJBuw8eC+rgRVscq1DKGHdtmFOXUL6a05ZdpNAq2VQx05/BJiD0+ZH9z Hq9qFaBc7gBkS15PVgWCaw3j8aKJWC48YKN7NCDWOkrh9o3+PlyvA4whgkhY5h7ri80o 1QwWtnu6wA9yCyVGzDwVbG1E+GiLCsJQooOrGkVpimtCfNzT02xNuv5RwaSSdI5YglgQ nBpXwYRuZZatItPGjRDaxJkStEymnJbCXUwKAew8brd+4e8Ac3Nr5iGVnCBczoXNY8kI jCOQ== X-Gm-Message-State: AO0yUKX8Mk9Mqab66ajzEaTzdN8Xqb/lRst4oEOVSkD54DkObC+NHCUQ 7oouQ4MzkQUr2ll3LOMo+0U= X-Google-Smtp-Source: AK7set9ornZ9F9sx5qiYJjoQzVFkY+bCLRtIohrdSiG/441BVRvhJ9uzzyiBrkLhQ1W5NzSBgW4MRQ== X-Received: by 2002:a17:907:720b:b0:8a5:8620:575 with SMTP id dr11-20020a170907720b00b008a586200575mr24934677ejc.3.1676109818205; Sat, 11 Feb 2023 02:03:38 -0800 (PST) Received: from [172.16.209.129] ([165.225.27.1]) by smtp.gmail.com with ESMTPSA id q13-20020a170906388d00b0088744fc7084sm3636405ejd.38.2023.02.11.02.03.37 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 11 Feb 2023 02:03:37 -0800 (PST) Message-ID: Date: Sat, 11 Feb 2023 11:03:36 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.7.1 Subject: Re: pg_stat_statements and "IN" conditions Content-Language: en-US To: Dmitry Dolgov <9erthalion6@gmail.com>, Sergei Kornilov Cc: Michael Paquier , Alvaro Herrera , Marcos Pegoraro , vignesh C , Robert Haas , Zhihong Yu , David Steele , PostgreSQL-development , Greg Stark , Pavel Trukhanov , Tom Lane References: <20230129133219.tpuzqbyqw3ojztbd@erthalion.local> <20230202140727.g7tw7qjo43g54wcb@alvherre.pgsql> <20230202150554.dwxhvw23epcskppb@ddolgov.remote.csb> <20230204170841.xfgxoo7ymwbvu7yx@erthalion.local> <20230205113346.gmbf5ycnukuhnmqa@erthalion.local> <2420105.1675612952@sss.pgh.pa.us> <20230205195600.e3bhwszzm5kdyyxl@erthalion.local> <966631675800892@znolar4klagewshm.sas.yp-c.yandex.net> <20230209150238.rdtwruh5i34hdag5@ddolgov.remote.csb> From: David Geier In-Reply-To: <20230209150238.rdtwruh5i34hdag5@ddolgov.remote.csb> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, On 2/9/23 16:02, Dmitry Dolgov wrote: >> Unfortunately, rebase is needed again due to recent changes in queryjumblefuncs ( 9ba37b2cb6a174b37fc51d0649ef73e56eae27fc ) I reviewed the last patch applied to some commit from Feb. 4th. >> It seems a little strange to me that with const_merge_threshold = 1, such a test case gives the same result as with const_merge_threshold = 2 >> >> select pg_stat_statements_reset(); >> set const_merge_threshold to 1; >> select * from test where i in (1,2,3); >> select * from test where i in (1,2); >> select * from test where i in (1); >> select query, calls from pg_stat_statements order by query; >> >> query | calls >> -------------------------------------+------- >> select * from test where i in (...) | 2 >> select * from test where i in ($1) | 1 >> >> Probably const_merge_threshold = 1 should produce only "i in (...)"? > Well, it's not intentional, probably I need to be more careful with > off-by-one. Although I agree to a certain extent with Peter questioning Please add tests for all the corner cases. At least for (1) IN only contains a single element and (2) const_merge_threshold = 1. Beyond that: - There's a comment about find_const_walker(). I cannot find that function anywhere. What am I missing? - What about renaming IsConstList() to IsMergableConstList(). - Don't you intend to use the NUMERIC data column in SELECT * FROM test_merge_numeric WHERE id IN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)? Otherwise, the test is identical to previous test cases and you're not checking for what happens with NUMERICs which are wrapped in FuncExpr because of the implicit coercion. - Don't we want to extend IsConstList() to allow a list of all implicitly coerced constants? It's inconsistent that otherwise e.g. NUMERICs don't work. - Typo in /* The firsts merged constant */ (first not firsts) - Prepared statements are not supported as they contain INs with Param instead of Const nodes. While less likely, I've seen applications that use prepared statements in conjunction with queries generated through a UI which ended up with tons of prepared queries with different number of elements in the IN clause. Not necessarily something that must go into this patch but maybe worth thinking about. - The setting name const_merge_threshold is not very telling without knowing the context. While being a little longer, what about jumble_const_merge_threshold or queryid_const_merge_threshold or similar? - Why do we actually only want to merge constants? Why don't we ignore the type of element in the IN and merge whatever is there? Is this because the original jumbling logic as of now only has support for constants? - Ideally we would even remove duplicates. That would even improve cardinality estimation but I guess we don't want to spend the cycles on doing so in the planner? - Why did you change palloc() to palloc0() for clocations array? The length is initialized to 0 and FWICS RecordConstLocation() initializes all members. Seems to me like we don't have to spend these cycles. - Can't the logic at the end of IsConstList() not be simplified to:         foreach(temp, elements)             if (!IsA(lfirst(temp), Const))                 return false;         // All elements are of type Const         *firstConst = linitial_node(Const, elements);         *lastConst = llast_node(Const, elements);         return true; -- David Geier (ServiceNow)