Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: postgresql-cfbot/postgresql
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: cf/5583~1
Choose a base ref
...
head repository: postgresql-cfbot/postgresql
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: cf/5583
Choose a head ref
  • 2 commits
  • 2 files changed
  • 2 contributors

Commits on May 5, 2025

  1. pg_prewarm: For indexes, check privileges on table.

    nathan-bossart authored and Commitfest Bot committed May 5, 2025
    Copy the full SHA
    908d61c View commit details
  2. [CF 5583] v2 - Improve ACL checks in pg_prewarm for indexes

    This branch was automatically generated by a robot using patches from an
    email thread registered at:
    
    https://commitfest.postgresql.org/patch/5583
    
    The branch will be overwritten each time a new patch version is posted to
    the thread, and also periodically to check for bitrot caused by changes
    on the master branch.
    
    Patch(es): https://www.postgresql.org/message-id/Z8y9RTT-vU6oVI_Y@nathan
    Author(s): Ayush Vatsa
    Commitfest Bot committed May 5, 2025
    Copy the full SHA
    8407320 View commit details
Showing with 39 additions and 3 deletions.
  1. +11 −2 contrib/pg_prewarm/pg_prewarm.c
  2. +28 −1 contrib/pg_prewarm/t/001_basic.pl
13 changes: 11 additions & 2 deletions contrib/pg_prewarm/pg_prewarm.c
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@
#include <unistd.h>

#include "access/relation.h"
#include "catalog/index.h"
#include "fmgr.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -58,6 +59,7 @@ Datum
pg_prewarm(PG_FUNCTION_ARGS)
{
Oid relOid;
Oid permOid;
text *forkName;
text *type;
int64 first_block;
@@ -106,9 +108,16 @@ pg_prewarm(PG_FUNCTION_ARGS)
forkString = text_to_cstring(forkName);
forkNumber = forkname_to_number(forkString);

/* Open relation and check privileges. */
/*
* Open relation and check privileges. Indexes don't have their own
* privileges, so we check privileges on the table instead in that case.
*/
rel = relation_open(relOid, AccessShareLock);
aclresult = pg_class_aclcheck(relOid, GetUserId(), ACL_SELECT);
if (rel->rd_rel->relkind == RELKIND_INDEX)
permOid = IndexGetRelation(relOid, false);
else
permOid = relOid;
aclresult = pg_class_aclcheck(permOid, GetUserId(), ACL_SELECT);
if (aclresult != ACLCHECK_OK)
aclcheck_error(aclresult, get_relkind_objtype(rel->rd_rel->relkind), get_rel_name(relOid));

29 changes: 28 additions & 1 deletion contrib/pg_prewarm/t/001_basic.pl
Original file line number Diff line number Diff line change
@@ -23,7 +23,9 @@
$node->safe_psql("postgres",
"CREATE EXTENSION pg_prewarm;\n"
. "CREATE TABLE test(c1 int);\n"
. "INSERT INTO test SELECT generate_series(1, 100);");
. "INSERT INTO test SELECT generate_series(1, 100);\n"
. "CREATE INDEX test_idx ON test(c1);\n"
. "CREATE ROLE test_user LOGIN;");

# test read mode
my $result =
@@ -42,6 +44,31 @@
or $stderr =~ qr/prefetch is not supported by this build/),
'prefetch mode succeeded');

# test_user should be unable to prewarm table/index without privileges
($cmdret, $stdout, $stderr) =
$node->psql(
"postgres", "SELECT pg_prewarm('test');",
extra_params => [ '--username' => 'test_user' ]);
ok($stderr =~ /permission denied for table test/, 'pg_prewarm failed as expected');
($cmdret, $stdout, $stderr) =
$node->psql(
"postgres", "SELECT pg_prewarm('test_idx');",
extra_params => [ '--username' => 'test_user' ]);
ok($stderr =~ /permission denied for index test_idx/, 'pg_prewarm failed as expected');

# test_user should be able to prewarm table/index with privileges
$node->safe_psql("postgres", "GRANT SELECT ON test TO test_user;");
$result =
$node->safe_psql(
"postgres", "SELECT pg_prewarm('test');",
extra_params => [ '--username' => 'test_user' ]);
like($result, qr/^[1-9][0-9]*$/, 'pg_prewarm succeeded as expected');
$result =
$node->safe_psql(
"postgres", "SELECT pg_prewarm('test_idx');",
extra_params => [ '--username' => 'test_user' ]);
like($result, qr/^[1-9][0-9]*$/, 'pg_prewarm succeeded as expected');

# test autoprewarm_dump_now()
$result = $node->safe_psql("postgres", "SELECT autoprewarm_dump_now();");
like($result, qr/^[1-9][0-9]*$/, 'autoprewarm_dump_now succeeded');