Skip to content
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

Fix chunk creation on hypertables with non-default statistics #4538

Merged
merged 1 commit into from
Jul 22, 2022
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
14 changes: 12 additions & 2 deletions src/chunk.c
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,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 @@ -914,6 +920,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 @@ -930,8 +942,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;