Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitfef2bcd

Browse files
committed
pageinspect: Add page_checksum function
Author: Tomas Vondra <tomas.vondra@2ndquadrant.com>Reviewed-by: Ashutosh Sharma <ashu.coek88@gmail.com>
1 parent64ae420 commitfef2bcd

File tree

5 files changed

+98
-2
lines changed

5 files changed

+98
-2
lines changed

‎contrib/pageinspect/expected/page.out

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ SELECT pagesize, version FROM page_header(get_raw_page('test1', 0));
4949
8192 | 4
5050
(1 row)
5151

52+
SELECT page_checksum(get_raw_page('test1', 0), 0) IS NOT NULL AS silly_checksum_test;
53+
silly_checksum_test
54+
---------------------
55+
t
56+
(1 row)
57+
5258
SELECT tuple_data_split('test1'::regclass, t_data, t_infomask, t_infomask2, t_bits)
5359
FROM heap_page_items(get_raw_page('test1', 0));
5460
tuple_data_split

‎contrib/pageinspect/pageinspect--1.5--1.6.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,11 @@ CREATE FUNCTION hash_metapage_info(IN page bytea,
7575
OUT mapp int8[])
7676
AS'MODULE_PATHNAME','hash_metapage_info'
7777
LANGUAGE C STRICT PARALLEL SAFE;
78+
79+
--
80+
-- page_checksum()
81+
--
82+
CREATEFUNCTIONpage_checksum(IN pagebytea,IN blkno int4)
83+
RETURNSsmallint
84+
AS'MODULE_PATHNAME','page_checksum'
85+
LANGUAGE C STRICT PARALLEL SAFE;

‎contrib/pageinspect/rawpage.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include"funcapi.h"
2525
#include"miscadmin.h"
2626
#include"storage/bufmgr.h"
27+
#include"storage/checksum.h"
2728
#include"utils/builtins.h"
2829
#include"utils/pg_lsn.h"
2930
#include"utils/rel.h"
@@ -280,3 +281,39 @@ page_header(PG_FUNCTION_ARGS)
280281

281282
PG_RETURN_DATUM(result);
282283
}
284+
285+
/*
286+
* page_checksum
287+
*
288+
* Compute checksum of a raw page
289+
*/
290+
291+
PG_FUNCTION_INFO_V1(page_checksum);
292+
293+
Datum
294+
page_checksum(PG_FUNCTION_ARGS)
295+
{
296+
bytea*raw_page=PG_GETARG_BYTEA_P(0);
297+
uint32blkno=PG_GETARG_INT32(1);
298+
intraw_page_size;
299+
PageHeaderpage;
300+
301+
if (!superuser())
302+
ereport(ERROR,
303+
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
304+
(errmsg("must be superuser to use raw page functions"))));
305+
306+
raw_page_size=VARSIZE(raw_page)-VARHDRSZ;
307+
308+
/*
309+
* Check that the supplied page is of the right size.
310+
*/
311+
if (raw_page_size!=BLCKSZ)
312+
ereport(ERROR,
313+
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
314+
errmsg("incorrect size of input page (%d bytes)",raw_page_size)));
315+
316+
page= (PageHeader)VARDATA(raw_page);
317+
318+
PG_RETURN_INT16(pg_checksum_page((char*)page,blkno));
319+
}

‎contrib/pageinspect/sql/page.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ SELECT get_raw_page('test1', 0) = get_raw_page('test1', 'main', 0);
2424

2525
SELECT pagesize, versionFROM page_header(get_raw_page('test1',0));
2626

27+
SELECT page_checksum(get_raw_page('test1',0),0)IS NOT NULLAS silly_checksum_test;
28+
2729
SELECT tuple_data_split('test1'::regclass, t_data, t_infomask, t_infomask2, t_bits)
2830
FROM heap_page_items(get_raw_page('test1',0));
2931

‎doc/src/sgml/pageinspect.sgml

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,55 @@
7373
test=# SELECT * FROM page_header(get_raw_page('pg_class', 0));
7474
lsn | checksum | flags | lower | upper | special | pagesize | version | prune_xid
7575
-----------+----------+--------+-------+-------+---------+----------+---------+-----------
76-
0/24A1B50 |1 | 1 | 232 | 368 | 8192 | 8192 | 4 | 0
76+
0/24A1B50 |0 | 1 | 232 | 368 | 8192 | 8192 | 4 | 0
7777
</screen>
7878
The returned columns correspond to the fields in the
7979
<structname>PageHeaderData</> struct.
8080
See <filename>src/include/storage/bufpage.h</> for details.
81-
</para>
81+
</para>
82+
83+
<para>
84+
The <structfield>checksum</structfield> field is the checksum stored in
85+
the page, which might be incorrect if the page is somehow corrupted. If
86+
data checksums are not enabled for this instance, then the value stored
87+
is meaningless.
88+
</para>
89+
</listitem>
90+
</varlistentry>
91+
92+
<varlistentry>
93+
<term>
94+
<function>page_checksum(page bytea, blkno int4) returns smallint</function>
95+
<indexterm>
96+
<primary>page_checksum</primary>
97+
</indexterm>
98+
</term>
99+
100+
<listitem>
101+
<para>
102+
<function>page_checksum</function> computes the checksum for the page, as if
103+
it was located at the given block.
104+
</para>
105+
106+
<para>
107+
A page image obtained with <function>get_raw_page</function> should be
108+
passed as argument. For example:
109+
<screen>
110+
test=# SELECT page_checksum(get_raw_page('pg_class', 0), 0);
111+
page_checksum
112+
---------------
113+
13443
114+
</screen>
115+
Note that the checksum depends on the block number, so matching block
116+
numbers should be passed (except when doing esoteric debugging).
117+
</para>
118+
119+
<para>
120+
The checksum computed with this function can be compared with
121+
the <structfield>checksum</structfield> result field of the
122+
function <function>page_header</function>. If data checksums are
123+
enabled for this instance, then the two values should be equal.
124+
</para>
82125
</listitem>
83126
</varlistentry>
84127

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp