Skip to content

[pull] master from postgres:master #973

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions contrib/amcheck/expected/check_gin.out
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,15 @@ SELECT gin_index_check('gin_check_jsonb_idx');

-- cleanup
DROP TABLE gin_check_jsonb;
-- Test GIN multicolumn index
CREATE TABLE "gin_check_multicolumn"(a text[], b text[]);
INSERT INTO gin_check_multicolumn (a,b) values ('{a,c,e}','{b,d,f}');
CREATE INDEX "gin_check_multicolumn_idx" on gin_check_multicolumn USING GIN(a,b);
SELECT gin_index_check('gin_check_multicolumn_idx');
gin_index_check
-----------------

(1 row)

-- cleanup
DROP TABLE gin_check_multicolumn;
1 change: 1 addition & 0 deletions contrib/amcheck/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ tests += {
't/003_cic_2pc.pl',
't/004_verify_nbtree_unique.pl',
't/005_pitr.pl',
't/006_verify_gin.pl',
],
},
}
10 changes: 10 additions & 0 deletions contrib/amcheck/sql/check_gin.sql
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,13 @@ SELECT gin_index_check('gin_check_jsonb_idx');

-- cleanup
DROP TABLE gin_check_jsonb;

-- Test GIN multicolumn index
CREATE TABLE "gin_check_multicolumn"(a text[], b text[]);
INSERT INTO gin_check_multicolumn (a,b) values ('{a,c,e}','{b,d,f}');
CREATE INDEX "gin_check_multicolumn_idx" on gin_check_multicolumn USING GIN(a,b);

SELECT gin_index_check('gin_check_multicolumn_idx');

-- cleanup
DROP TABLE gin_check_multicolumn;
199 changes: 199 additions & 0 deletions contrib/amcheck/t/006_verify_gin.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@

# Copyright (c) 2021-2025, PostgreSQL Global Development Group

use strict;
use warnings FATAL => 'all';

use PostgreSQL::Test::Cluster;
use PostgreSQL::Test::Utils;

use Test::More;

my $node;
my $blksize;

# to get the split fast, we want tuples to be as large as possible, but the same time we don't want them to be toasted.
my $filler_size = 1900;

#
# Test set-up
#
$node = PostgreSQL::Test::Cluster->new('test');
$node->init(no_data_checksums => 1);
$node->append_conf('postgresql.conf', 'autovacuum=off');
$node->start;
$blksize = int($node->safe_psql('postgres', 'SHOW block_size;'));
$node->safe_psql('postgres', q(CREATE EXTENSION amcheck));
$node->safe_psql(
'postgres', q(
CREATE OR REPLACE FUNCTION random_string( INT ) RETURNS text AS $$
SELECT string_agg(substring('0123456789abcdefghijklmnopqrstuvwxyz', ceil(random() * 36)::integer, 1), '') from generate_series(1, $1);
$$ LANGUAGE SQL;));

# Tests
invalid_entry_order_leaf_page_test();
invalid_entry_order_inner_page_test();
invalid_entry_columns_order_test();

sub invalid_entry_order_leaf_page_test
{
my $relname = "test";
my $indexname = "test_gin_idx";

$node->safe_psql(
'postgres', qq(
DROP TABLE IF EXISTS $relname;
CREATE TABLE $relname (a text[]);
INSERT INTO $relname (a) VALUES ('{aaaaa,bbbbb}');
CREATE INDEX $indexname ON $relname USING gin (a);
));
my $relpath = relation_filepath($indexname);

$node->stop;

my $blkno = 1; # root

# produce wrong order by replacing aaaaa with ccccc
string_replace_block(
$relpath,
'aaaaa',
'ccccc',
$blkno
);

$node->start;

my ($result, $stdout, $stderr) = $node->psql('postgres', qq(SELECT gin_index_check('$indexname')));
my $expected = "index \"$indexname\" has wrong tuple order on entry tree page, block 1, offset 2, rightlink 4294967295";
like($stderr, qr/$expected/);
}

sub invalid_entry_order_inner_page_test
{
my $relname = "test";
my $indexname = "test_gin_idx";

# to break the order in the inner page we need at least 3 items (rightmost key in the inner level is not checked for the order)
# so fill table until we have 2 splits
$node->safe_psql(
'postgres', qq(
DROP TABLE IF EXISTS $relname;
CREATE TABLE $relname (a text[]);
INSERT INTO $relname (a) VALUES (('{' || 'pppppppppp' || random_string($filler_size) ||'}')::text[]);
INSERT INTO $relname (a) VALUES (('{' || 'qqqqqqqqqq' || random_string($filler_size) ||'}')::text[]);
INSERT INTO $relname (a) VALUES (('{' || 'rrrrrrrrrr' || random_string($filler_size) ||'}')::text[]);
INSERT INTO $relname (a) VALUES (('{' || 'ssssssssss' || random_string($filler_size) ||'}')::text[]);
INSERT INTO $relname (a) VALUES (('{' || 'tttttttttt' || random_string($filler_size) ||'}')::text[]);
INSERT INTO $relname (a) VALUES (('{' || 'uuuuuuuuuu' || random_string($filler_size) ||'}')::text[]);
INSERT INTO $relname (a) VALUES (('{' || 'vvvvvvvvvv' || random_string($filler_size) ||'}')::text[]);
INSERT INTO $relname (a) VALUES (('{' || 'wwwwwwwwww' || random_string($filler_size) ||'}')::text[]);
CREATE INDEX $indexname ON $relname USING gin (a);
));
my $relpath = relation_filepath($indexname);

$node->stop;

my $blkno = 1; # root

# we have rrrrrrrrr... and tttttttttt... as keys in the root, so produce wrong order by replacing rrrrrrrrrr....
string_replace_block(
$relpath,
'rrrrrrrrrr',
'zzzzzzzzzz',
$blkno
);

$node->start;

my ($result, $stdout, $stderr) = $node->psql('postgres', qq(SELECT gin_index_check('$indexname')));
my $expected = "index \"$indexname\" has wrong tuple order on entry tree page, block 1, offset 2, rightlink 4294967295";
like($stderr, qr/$expected/);
}

sub invalid_entry_columns_order_test
{
my $relname = "test";
my $indexname = "test_gin_idx";

$node->safe_psql(
'postgres', qq(
DROP TABLE IF EXISTS $relname;
CREATE TABLE $relname (a text[],b text[]);
INSERT INTO $relname (a,b) VALUES ('{aaa}','{bbb}');
CREATE INDEX $indexname ON $relname USING gin (a,b);
));
my $relpath = relation_filepath($indexname);

$node->stop;

my $blkno = 1; # root

# mess column numbers
# root items order before: (1,aaa), (2,bbb)
# root items order after: (2,aaa), (1,bbb)
my $attrno_1 = pack('s', 1);
my $attrno_2 = pack('s', 2);

my $find = qr/($attrno_1)(.)(aaa)/s;
my $replace = $attrno_2 . '$2$3';
string_replace_block(
$relpath,
$find,
$replace,
$blkno
);

$find = qr/($attrno_2)(.)(bbb)/s;
$replace = $attrno_1 . '$2$3';
string_replace_block(
$relpath,
$find,
$replace,
$blkno
);

$node->start;

my ($result, $stdout, $stderr) = $node->psql('postgres', qq(SELECT gin_index_check('$indexname')));
my $expected = "index \"$indexname\" has wrong tuple order on entry tree page, block 1, offset 2, rightlink 4294967295";
like($stderr, qr/$expected/);
}

# Returns the filesystem path for the named relation.
sub relation_filepath
{
my ($relname) = @_;

my $pgdata = $node->data_dir;
my $rel = $node->safe_psql('postgres',
qq(SELECT pg_relation_filepath('$relname')));
die "path not found for relation $relname" unless defined $rel;
return "$pgdata/$rel";
}

# substitute pattern 'find' with 'replace' within the block with number 'blkno' in the file 'filename'
sub string_replace_block
{
my ($filename, $find, $replace, $blkno) = @_;

my $fh;
open($fh, '+<', $filename) or BAIL_OUT("open failed: $!");
binmode $fh;

my $offset = $blkno * $blksize;
my $buffer;

sysseek($fh, $offset, 0) or BAIL_OUT("seek failed: $!");
sysread($fh, $buffer, $blksize) or BAIL_OUT("read failed: $!");

$buffer =~ s/$find/'"' . $replace . '"'/gee;

sysseek($fh, $offset, 0) or BAIL_OUT("seek failed: $!");
syswrite($fh, $buffer) or BAIL_OUT("write failed: $!");

close($fh) or BAIL_OUT("close failed: $!");

return;
}

done_testing();
Loading