Skip to content

Commit

Permalink
Fix chunk creation on hypertables with non-default statistics
Browse files Browse the repository at this point in the history
When triggering chunk creation on a hypertable with non-default
statistics targets by a user different from the hypertable owner
the chunk creation will fail with a permission error. This patch
changes the chunk table creation to run the attribute modification
as the table owner.

Fixes #4474
  • Loading branch information
svenklemm committed Jul 22, 2022
1 parent ec9249f commit 949e098
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/chunk.c
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,12 @@ ts_chunk_create_table(const Chunk *chunk, const Hypertable *ht, const char *tabl
*/
create_toast_table(&stmt.base, objaddr.objectId);

/*
* Some options require being table owner to set for example statistics
* so we have to set them before restoring security context
*/
set_attoptions(rel, objaddr.objectId);

if (uid != saved_uid)
SetUserIdAndSecContext(saved_uid, sec_ctx);
}
Expand All @@ -913,6 +919,12 @@ ts_chunk_create_table(const Chunk *chunk, const Hypertable *ht, const char *tabl
/* Create the foreign table catalog information */
CreateForeignTable(&stmt, objaddr.objectId);

/*
* Some options require being table owner to set for example statistics
* so we have to set them before restoring security context
*/
set_attoptions(rel, objaddr.objectId);

/*
* Need to restore security context to execute remote commands as the
* original user
Expand All @@ -929,8 +941,6 @@ ts_chunk_create_table(const Chunk *chunk, const Hypertable *ht, const char *tabl
else
elog(ERROR, "invalid relkind \"%c\" when creating chunk", chunk->relkind);

set_attoptions(rel, objaddr.objectId);

table_close(rel, AccessShareLock);

return objaddr.objectId;
Expand Down
20 changes: 20 additions & 0 deletions test/expected/alter.out
Original file line number Diff line number Diff line change
Expand Up @@ -769,3 +769,23 @@ CREATE VIEW v1 AS SELECT random();
ALTER VIEW v1 SET (autovacuum_enabled = false);
ERROR: unrecognized parameter "autovacuum_enabled"
\set ON_ERROR_STOP 1
-- issue 4474
-- test hypertable with non-default statistics target
-- and chunk creation triggered by non-owner
CREATE ROLE role_4474;
CREATE TABLE i4474(time timestamptz NOT NULL);
SELECT table_name FROM public.create_hypertable( 'i4474', 'time');
table_name
------------
i4474
(1 row)

GRANT SELECT, INSERT on i4474 TO role_4474;
-- create chunk as owner
INSERT INTO i4474 SELECT '2020-01-01';
-- set statistics
ALTER TABLE i4474 ALTER COLUMN time SET statistics 10;
-- create chunk as non-owner
SET ROLE role_4474;
INSERT INTO i4474 SELECT '2021-01-01';
RESET ROLE;
19 changes: 19 additions & 0 deletions test/sql/alter.sql
Original file line number Diff line number Diff line change
Expand Up @@ -428,3 +428,22 @@ CREATE VIEW v1 AS SELECT random();
ALTER VIEW v1 SET (autovacuum_enabled = false);
\set ON_ERROR_STOP 1

-- issue 4474
-- test hypertable with non-default statistics target
-- and chunk creation triggered by non-owner
CREATE ROLE role_4474;
CREATE TABLE i4474(time timestamptz NOT NULL);
SELECT table_name FROM public.create_hypertable( 'i4474', 'time');
GRANT SELECT, INSERT on i4474 TO role_4474;

-- create chunk as owner
INSERT INTO i4474 SELECT '2020-01-01';

-- set statistics
ALTER TABLE i4474 ALTER COLUMN time SET statistics 10;

-- create chunk as non-owner
SET ROLE role_4474;
INSERT INTO i4474 SELECT '2021-01-01';
RESET ROLE;

0 comments on commit 949e098

Please sign in to comment.