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 1kPolO-0008AJ-JI for pgsql-hackers@arkaria.postgresql.org; Tue, 06 Oct 2020 15:20:26 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kPolN-0003jJ-I7 for pgsql-hackers@arkaria.postgresql.org; Tue, 06 Oct 2020 15:20:25 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from <9erthalion6@gmail.com>) id 1kPolN-0003jC-4J for pgsql-hackers@lists.postgresql.org; Tue, 06 Oct 2020 15:20:25 +0000 Received: from mail-ej1-x642.google.com ([2a00:1450:4864:20::642]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from <9erthalion6@gmail.com>) id 1kPolG-0008F7-QI for pgsql-hackers@postgresql.org; Tue, 06 Oct 2020 15:20:24 +0000 Received: by mail-ej1-x642.google.com with SMTP id t25so2912538ejd.13 for ; Tue, 06 Oct 2020 08:20:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to; bh=3yqBbj+EihvswDMmn5ywmGJF0h18VsMUkWHlrnyHU8o=; b=QHioW5X0TGkpajyrEudnPDVMt90v0XL3iv31AF4sm2hh4jkeu0FMGB9KE4UIWCSBE9 mccjbSQahpbtKTKxdT3xMNwS5c2YUy7Nk7wfAUaD4GZQzz2dWMBtCx5tURpKvf+Ch3zK s7uWSs1Nwd0mZPI8zUJdafoiRLlEURt5LyV/fyuFN9Xg2ZbCBCqTCd6GcIZlCOMz/sUA EnQZjqSwY4OQHFhFxo+WhCRd7+QmCrW5z+RTj98Y/4q5+FGb0ZepcoB0iQf134KZF+kb 4C8wouw8lKButqAVMYtlA3qW/q9ogTfq3SiCtjBNyO2KYqnJhFMRtETceFWZCc6BLVcW uTQQ== 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; bh=3yqBbj+EihvswDMmn5ywmGJF0h18VsMUkWHlrnyHU8o=; b=SvMXTSG1Y5Lg552SbOUJ0Pq5c92mvuYYOoeIFeSIuq5lR/S77eOEM2Dxt4ZQhhGlmN un9B8VlEEygviVKx1iKvGvpVvfB8Jl/5wuHj92AgRpZ94cvWGuXjE3WRhR5gN3HhqORI ZcHIBS09FSCsnqisCZAQJ3xleVBLuqJdke3aid/GcQWm0ZG/sBeiI53nm3fdbmDe4iZ4 7Z+WAS6lbSyHRE/wO/7Qe2YOfgrALjWD46yJDmDfHrgvv79ICY7VGujlwRJvgA90lotT XNPHY5AN2XSNXsD9AjydGCgUW7fvOXwShHYXJb3l1JHjU8U/DhCl7ie9qoLdxrbGOGIM 79LA== X-Gm-Message-State: AOAM531cqNwynmip/J8ARjykNZxDhtr6PVQw625HRTWKmH7Oi8MN7bd6 U9GKqxX/lIHN5X3SUv0VStzzqEkbkQqUJQ== X-Google-Smtp-Source: ABdhPJwfT9gzUj1ITrxbaRgdicuEBoTWwLMty3aomkX3BIE199a+2Aa0XAJ+eLrbtOnk0M2BxCV+KQ== X-Received: by 2002:a17:906:cf8b:: with SMTP id um11mr6026476ejb.540.1601997617097; Tue, 06 Oct 2020 08:20:17 -0700 (PDT) Received: from localhost (dslb-178-005-232-008.178.005.pools.vodafone-ip.de. [178.5.232.8]) by smtp.gmail.com with ESMTPSA id a19sm2533856edb.84.2020.10.06.08.20.15 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 06 Oct 2020 08:20:16 -0700 (PDT) Date: Tue, 6 Oct 2020 17:20:39 +0200 From: Dmitry Dolgov <9erthalion6@gmail.com> To: Peter Geoghegan Cc: PostgreSQL-development , Jesper Pedersen , David Rowley , Floris Van Nee , Kyotaro Horiguchi , Thomas Munro , Tomas Vondra , Andy Fan , Dilip Kumar Subject: Re: Index Skip Scan (new UniqueKeys) Message-ID: <20201006152039.xs2ul7il52sowhlu@localhost> References: <20200609102247.jdlatmfyeecg52fi@localhost> <20200711161258.5modwkdeszhf55ux@localhost> <20200727102431.mjspwec4yhzeyny2@localhost> <20200815141240.wuyc6ijkmnywywtn@localhost> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk > On Mon, Sep 21, 2020 at 05:59:32PM -0700, Peter Geoghegan wrote: > > * I see the following compiler warning: > > /code/postgresql/patch/build/../source/src/backend/optimizer/path/uniquekeys.c: > In function ‘populate_baserel_uniquekeys’: > /code/postgresql/patch/build/../source/src/backend/optimizer/path/uniquekeys.c:797:13: > warning: ‘expr’ may be used uninitialized in this function > [-Wmaybe-uninitialized] > 797 | else if (!list_member(unique_index->rel->reltarget->exprs, expr)) > | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ This is mostly for UniqueKeys patch, which is attached here only as a dependency, but I'll prepare changes for that. Interesting enough I can't reproduce this warning, but if I understand correctly gcc has some history of spurious uninitialized warnings, so I guess it could be version dependent. > * Perhaps the warning is related to this nearby code that I noticed > Valgrind complains about: > > ==1083468== VALGRINDERROR-BEGIN > ==1083468== Invalid read of size 4 > ==1083468== at 0x59568A: get_exprs_from_uniqueindex (uniquekeys.c:771) > ==1083468== by 0x593C5B: populate_baserel_uniquekeys (uniquekeys.c:140) This also belongs to UniqueKeys patch, but at least I can reproduce this one. My guess is that nkeycolums should be used there, not ncolums, which is visible in index_incuding tests. The same as previous one, will prepare corresponding changes. > * Do we really need the AM-level boolean flag/argument named > "scanstart"? Why not just follow the example of btgettuple(), which > determines whether or not the scan has been initialized based on the > current scan position? > > Just because you set so->currPos.buf to InvalidBuffer doesn't mean you > cannot or should not take the same approach as btgettuple(). And even > if you can't take exactly the same approach, I would still think that > the scan's opaque B-Tree state should remember if it's the first call > to _bt_skip() (rather than some subsequent call) in some other way > (e.g. carrying a "scanstart" bool flag directly). Yes, agree, carrying this flag inside the opaque state would be better. > * Why is it okay to do anything important based on the > _bt_scankey_within_page() return value? > > If the page is empty, then how can we know that it's okay to go to the > next value? I'm concerned that there could be subtle bugs in this > area. VACUUM will usually just delete the empty page. But it won't > always do so, for a variety of reasons that aren't worth going into > now. This could mask bugs in this area. I'm concerned about patterns > like this one from _bt_skip(): > > while (!nextFound) > { > .... > > if (_bt_scankey_within_page(scan, so->skipScanKey, > so->currPos.buf, dir)) > { > ... > } > else > /* > * If startItup could be not found within the current page, > * assume we found something new > */ > nextFound = true; > .... > } > > Why would you assume that "we found something new" here? In general I > just don't understand the design of _bt_skip(). I get the basic idea > of what you're trying to do, but it could really use better comments. Yeah, I'll put more efforts into clear comments. There are two different ways in which _bt_scankey_within_page is being used. The first one is to check if it's possible to skip traversal of the tree from root in case if what we're looking for could be on the current page. In this case an empty page would mean we need to search from the root, so not sure what could be the issue here? The second one (that you've highlighted above) I admit is probably the most questionable part of the patch and open for suggestions how to improve it. It's required for one particular case with a cursor when scan advances forward but reads backward. What could happen here is we found one valid item, but the next one e.g. do not pass scan key conditions, and we end up with the previous item again. I'm not entirely sure how presence of an empty page could change this scenario, could you please show an example? > *The "jump one more time if it's the same as at the beginning" thing > seems scary to me. Maybe you should be doing something with the actual > high key here. Same as for the previous question, can you give a hint what do you mean by "doing something with the actual high key"?