Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id B59BC1337B7E for ; Mon, 15 Nov 2010 17:28:45 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 36152-08 for ; Mon, 15 Nov 2010 21:28:38 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from exprod7og113.obsmtp.com (exprod7og113.obsmtp.com [64.18.2.179]) by mail.postgresql.org (Postfix) with SMTP id 7EEE01337B2F for ; Mon, 15 Nov 2010 17:28:37 -0400 (AST) Received: from source ([209.85.215.43]) by exprod7ob113.postini.com ([64.18.6.12]) with SMTP ID DSNKTOGmBK3QjY4yfM1aBhifDFrU+Jk+73u7@postini.com; Mon, 15 Nov 2010 13:28:38 PST Received: by ewy20 with SMTP id 20so3364319ewy.16 for ; Mon, 15 Nov 2010 13:28:34 -0800 (PST) Received: by 10.14.47.78 with SMTP id s54mr3987046eeb.20.1289856512141; Mon, 15 Nov 2010 13:28:32 -0800 (PST) Received: from [192.168.1.183] (dsl-hkibrasgw2-ff7ac300-240.dhcp.inet.fi [88.195.122.240]) by mx.google.com with ESMTPS id v56sm377858eeh.2.2010.11.15.13.28.30 (version=TLSv1/SSLv3 cipher=RC4-MD5); Mon, 15 Nov 2010 13:28:31 -0800 (PST) Message-ID: <4CE1A5FF.8000500@enterprisedb.com> Date: Mon, 15 Nov 2010 23:28:31 +0200 From: Heikki Linnakangas User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; fi-FI; rv:1.9.1.15) Gecko/20101030 Icedove/3.0.10 MIME-Version: 1.0 To: PostgreSQL Bugs CC: Teodor Sigaev , Oleg Bartunov Subject: Bug in concurrency control in temporary GiST indexes Content-Type: multipart/mixed; boundary="------------070608070004000400080404" X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-4.2 tagged_above=-5 required=5 tests=BAYES_00=-1.9, RCVD_IN_DNSWL_MED=-2.3 X-Spam-Level: X-Archive-Number: 201011/107 X-Sequence-Number: 28687 This is a multi-part message in MIME format. --------------070608070004000400080404 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit While hacking on the GiST insertion algorithm, I noticed a bug with temporary GiST indexes. The scan algorithm uses LSNs to detect a concurrent page split, but for a temporary index, we just use a constant LSN on the assumption that there can't be anyone else modifying the index concurrently. But that assumption is not true. While a temporary index can't be modified by other backends, it's entirely possible to insert new tuples to the index within the same backend while a scan is in progress. Here's a test case, using btree_gist. If you modify it to use a non-temporary table, it works fine, but with a temporary table the 2nd SELECT misses some rows, when new rows are inserted in the middle of the scan. Operations on temporary tables are not WAL-logged, so we don't have LSNs to use, but we just need a monotonically increasing sequence of numbers for this purpose. Attached patch provides a function to generate such fake LSNs. -- Heikki Linnakangas EnterpriseDB http://www.enterprisedb.com --------------070608070004000400080404 Content-Type: text/x-sql; name="tempgist.sql" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="tempgist.sql" -- Initialize a temporary table with 1000 rows and a gist index. CREATE TEMPORARY TABLE tt (a integer); CREATE INDEX i_tt ON tt USING gist (a); INSERT INTO tt SELECT generate_series(1,1000); -- Fetch n tuples from "tt", and write them to table called "resultset" CREATE OR REPLACE FUNCTION fetchfunc(n int) RETURNS bool AS $$ declare r record; c refcursor; begin c:= 'foocur'; FOR i IN 1..n LOOP FETCH FROM c INTO r; IF r IS NULL THEN RETURN true; END IF; INSERT INTO resultset VALUES (r.a); END LOOP; RETURN false; end; $$ LANGUAGE plpgsql; -- ensure that we use a plain index scan. set enable_bitmapscan=off; set enable_seqscan=off; CREATE TEMPORARY TABLE resultset (a integer); -- Fetch all rows from tt into resultset, with no inserts in the middle BEGIN; DECLARE foocur CURSOR FOR SELECT a FROM tt WHERE a > 100; SELECT fetchfunc(100); SELECT fetchfunc(1000); COMMIT; SELECT COUNT(*) FROM resultset; -- Display result truncate resultset; -- Do the same, but insert new tuples in the middle of the scan. The result -- should be the same. BEGIN; DECLARE foocur CURSOR FOR SELECT a FROM tt WHERE a > 100; SELECT fetchfunc(100); INSERT INTO tt SELECT a% 10 FROM generate_series(1,1000) a; SELECT fetchfunc(1000); COMMIT; SELECT COUNT(*) FROM resultset; -- Display result --------------070608070004000400080404 Content-Type: text/x-diff; name="fix-temp-gist-1.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="fix-temp-gist-1.patch" diff --git a/src/backend/access/gist/gist.c b/src/backend/access/gist/gist.c index 3054f98..8c2dbc9 100644 --- a/src/backend/access/gist/gist.c +++ b/src/backend/access/gist/gist.c @@ -22,8 +22,6 @@ #include "storage/indexfsm.h" #include "utils/memutils.h" -const XLogRecPtr XLogRecPtrForTemp = {1, 1}; - /* Working state for gistbuild and its callback */ typedef struct { @@ -132,7 +130,7 @@ gistbuild(PG_FUNCTION_ARGS) PageSetTLI(page, ThisTimeLineID); } else - PageSetLSN(page, XLogRecPtrForTemp); + PageSetLSN(page, GetXLogRecPtrForTemp()); UnlockReleaseBuffer(buffer); @@ -423,7 +421,7 @@ gistplacetopage(GISTInsertState *state, GISTSTATE *giststate) { for (ptr = dist; ptr; ptr = ptr->next) { - PageSetLSN(ptr->page, XLogRecPtrForTemp); + PageSetLSN(ptr->page, GetXLogRecPtrForTemp()); } } @@ -491,7 +489,7 @@ gistplacetopage(GISTInsertState *state, GISTSTATE *giststate) PageSetTLI(state->stack->page, ThisTimeLineID); } else - PageSetLSN(state->stack->page, XLogRecPtrForTemp); + PageSetLSN(state->stack->page, GetXLogRecPtrForTemp()); if (state->stack->blkno == GIST_ROOT_BLKNO) state->needInsertComplete = false; @@ -1027,7 +1025,7 @@ gistnewroot(Relation r, Buffer buffer, IndexTuple *itup, int len, ItemPointer ke PageSetTLI(page, ThisTimeLineID); } else - PageSetLSN(page, XLogRecPtrForTemp); + PageSetLSN(page, GetXLogRecPtrForTemp()); END_CRIT_SECTION(); } diff --git a/src/backend/access/gist/gistutil.c b/src/backend/access/gist/gistutil.c index 28ab575..9de08f9 100644 --- a/src/backend/access/gist/gistutil.c +++ b/src/backend/access/gist/gistutil.c @@ -677,3 +677,22 @@ gistoptions(PG_FUNCTION_ARGS) PG_RETURN_BYTEA_P(result); PG_RETURN_NULL(); } + +/* + * Temporary GiST indexes are not WAL-logged, but we need LSNs to detect + * concurrent page splits anyway. GetXLogRecPtrForTemp() provides a fake + * monotonically increasing sequence of LSNs for that purpose. + */ +XLogRecPtr +GetXLogRecPtrForTemp(void) +{ + static XLogRecPtr counter = {0, 1}; + + counter.xrecoff++; + if (counter.xrecoff == 0) + { + counter.xlogid++; + counter.xrecoff++; + } + return counter; +} diff --git a/src/backend/access/gist/gistvacuum.c b/src/backend/access/gist/gistvacuum.c index 0ff5ba8..dbe9406 100644 --- a/src/backend/access/gist/gistvacuum.c +++ b/src/backend/access/gist/gistvacuum.c @@ -268,7 +268,7 @@ gistbulkdelete(PG_FUNCTION_ARGS) pfree(rdata); } else - PageSetLSN(page, XLogRecPtrForTemp); + PageSetLSN(page, GetXLogRecPtrForTemp()); END_CRIT_SECTION(); } diff --git a/src/include/access/gist_private.h b/src/include/access/gist_private.h index 34cc5d5..1f5fb7c 100644 --- a/src/include/access/gist_private.h +++ b/src/include/access/gist_private.h @@ -326,6 +326,8 @@ extern void gistMakeUnionKey(GISTSTATE *giststate, int attno, GISTENTRY *entry2, bool isnull2, Datum *dst, bool *dstisnull); +extern XLogRecPtr GetXLogRecPtrForTemp(void); + /* gistvacuum.c */ extern Datum gistbulkdelete(PG_FUNCTION_ARGS); extern Datum gistvacuumcleanup(PG_FUNCTION_ARGS); --------------070608070004000400080404--