Invalid result from hash_page_items function

Started by Masahiko Sawadaalmost 8 years ago3 messages
#1Masahiko Sawada
sawada.mshk@gmail.com
1 attachment(s)

Hi,

While researching hash index, I found that hash_page_items could
return an invalid result as follows.

postgres(1:1056)=# create table hash_test (c int);
CREATE TABLE
postgres(1:1056)=# insert into hash_test select generate_series(1,50) % 5;
INSERT 0 50
postgres(1:1056)=# create index hash_idx on hash_test using hash (c);
CREATE INDEX
postgres(1:1056)=# create extension pageinspect ;
CREATE EXTENSION
postgres(1:1056)=# select * from hash_page_items(get_raw_page('hash_idx',
4));
itemoffset | ctid | data
------------+--------------------+------------
1 | (0,49) | 3283889963
2 | (2139062143,32639) | 2139062143
3 | (2139062143,32639) | 2139062143
4 | (2139062143,32639) | 2139062143
5 | (2139062143,32639) | 2139062143
6 | (2139062143,32639) | 2139062143
7 | (2139062143,32639) | 2139062143
8 | (2139062143,32639) | 2139062143
9 | (2139062143,32639) | 2139062143
10 | (2139062143,32639) | 2139062143
(10 rows)

This appears at PostgreSQL 10 and current HEAD. The cause of this
seems that hash_page_items allocates the memory space for the page
before switching memory context. AFAICS there is no similar problem in
pageinspect contrib module. Attached patch fixes it.

Regards,

--
Masahiko Sawada
NIPPON TELEGRAPH AND TELEPHONE CORPORATION
NTT Open Source Software Center

Attachments:

fix_hash_page_items.patchapplication/octet-stream; name=fix_hash_page_items.patchDownload
diff --git a/contrib/pageinspect/hashfuncs.c b/contrib/pageinspect/hashfuncs.c
index 3d0e3f9..99b61b8 100644
--- a/contrib/pageinspect/hashfuncs.c
+++ b/contrib/pageinspect/hashfuncs.c
@@ -313,10 +313,10 @@ hash_page_items(PG_FUNCTION_ARGS)
 
 		fctx = SRF_FIRSTCALL_INIT();
 
-		page = verify_hash_page(raw_page, LH_BUCKET_PAGE | LH_OVERFLOW_PAGE);
-
 		mctx = MemoryContextSwitchTo(fctx->multi_call_memory_ctx);
 
+		page = verify_hash_page(raw_page, LH_BUCKET_PAGE | LH_OVERFLOW_PAGE);
+
 		uargs = palloc(sizeof(struct user_args));
 
 		uargs->page = page;
#2Robert Haas
robertmhaas@gmail.com
In reply to: Masahiko Sawada (#1)
Re: Invalid result from hash_page_items function

On Thu, Jan 25, 2018 at 4:50 PM, Masahiko Sawada <sawada.mshk@gmail.com> wrote:

This appears at PostgreSQL 10 and current HEAD. The cause of this
seems that hash_page_items allocates the memory space for the page
before switching memory context. AFAICS there is no similar problem in
pageinspect contrib module. Attached patch fixes it.

Committed and back-patched.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

#3Masahiko Sawada
sawada.mshk@gmail.com
In reply to: Robert Haas (#2)
Re: Invalid result from hash_page_items function

On Sat, Jan 27, 2018 at 12:04 AM, Robert Haas <robertmhaas@gmail.com> wrote:

On Thu, Jan 25, 2018 at 4:50 PM, Masahiko Sawada <sawada.mshk@gmail.com> wrote:

This appears at PostgreSQL 10 and current HEAD. The cause of this
seems that hash_page_items allocates the memory space for the page
before switching memory context. AFAICS there is no similar problem in
pageinspect contrib module. Attached patch fixes it.

Committed and back-patched.

Thank you!

Regards,

--
Masahiko Sawada
NIPPON TELEGRAPH AND TELEPHONE CORPORATION
NTT Open Source Software Center