mirror of
https://github.com/postgres/postgres.git
synced 2025-07-15 19:21:59 +03:00
Improve TRUNCATE by avoiding early lock queue
A caller of TRUNCATE could previously queue for an access exclusive lock
on a relation it may not have permission to truncate, potentially
interfering with users authorized to work on it. This can be very
intrusive depending on the lock attempted to be taken. For example,
pg_authid could be blocked, preventing any authentication attempt to
happen on a PostgreSQL instance.
This commit fixes the case of TRUNCATE so as RangeVarGetRelidExtended is
used with a callback doing the necessary ACL checks at an earlier stage,
avoiding lock queuing issues, so as an immediate failure happens for
unprivileged users instead of waiting on a lock that would not be
taken.
This is rather similar to the type of work done in cbe24a6
for CLUSTER,
and the code of TRUNCATE is this time refactored so as there is no
user-facing changes. As the commit for CLUSTER, no back-patch is done.
Reported-by: Lloyd Albin, Jeremy Schneider
Author: Michael Paquier
Reviewed by: Nathan Bossart, Kyotaro Horiguchi
Discussion: https://postgr.es/m/152512087100.19803.12733865831237526317@wrigleys.postgresql.org
Discussion: https://postgr.es/m/20180806165816.GA19883@paquier.xyz
This commit is contained in:
99
src/test/isolation/expected/truncate-conflict.out
Normal file
99
src/test/isolation/expected/truncate-conflict.out
Normal file
@ -0,0 +1,99 @@
|
||||
Parsed test spec with 2 sessions
|
||||
|
||||
starting permutation: s1_begin s1_tab_lookup s2_auth s2_truncate s1_commit s2_reset
|
||||
step s1_begin: BEGIN;
|
||||
step s1_tab_lookup: SELECT count(*) >= 0 FROM truncate_tab;
|
||||
?column?
|
||||
|
||||
t
|
||||
step s2_auth: SET ROLE regress_truncate_conflict;
|
||||
step s2_truncate: TRUNCATE truncate_tab;
|
||||
ERROR: permission denied for table truncate_tab
|
||||
step s1_commit: COMMIT;
|
||||
step s2_reset: RESET ROLE;
|
||||
|
||||
starting permutation: s1_begin s2_auth s2_truncate s1_tab_lookup s1_commit s2_reset
|
||||
step s1_begin: BEGIN;
|
||||
step s2_auth: SET ROLE regress_truncate_conflict;
|
||||
step s2_truncate: TRUNCATE truncate_tab;
|
||||
ERROR: permission denied for table truncate_tab
|
||||
step s1_tab_lookup: SELECT count(*) >= 0 FROM truncate_tab;
|
||||
?column?
|
||||
|
||||
t
|
||||
step s1_commit: COMMIT;
|
||||
step s2_reset: RESET ROLE;
|
||||
|
||||
starting permutation: s1_begin s2_auth s1_tab_lookup s2_truncate s1_commit s2_reset
|
||||
step s1_begin: BEGIN;
|
||||
step s2_auth: SET ROLE regress_truncate_conflict;
|
||||
step s1_tab_lookup: SELECT count(*) >= 0 FROM truncate_tab;
|
||||
?column?
|
||||
|
||||
t
|
||||
step s2_truncate: TRUNCATE truncate_tab;
|
||||
ERROR: permission denied for table truncate_tab
|
||||
step s1_commit: COMMIT;
|
||||
step s2_reset: RESET ROLE;
|
||||
|
||||
starting permutation: s2_auth s2_truncate s1_begin s1_tab_lookup s1_commit s2_reset
|
||||
step s2_auth: SET ROLE regress_truncate_conflict;
|
||||
step s2_truncate: TRUNCATE truncate_tab;
|
||||
ERROR: permission denied for table truncate_tab
|
||||
step s1_begin: BEGIN;
|
||||
step s1_tab_lookup: SELECT count(*) >= 0 FROM truncate_tab;
|
||||
?column?
|
||||
|
||||
t
|
||||
step s1_commit: COMMIT;
|
||||
step s2_reset: RESET ROLE;
|
||||
|
||||
starting permutation: s1_begin s1_tab_lookup s2_grant s2_auth s2_truncate s1_commit s2_reset
|
||||
step s1_begin: BEGIN;
|
||||
step s1_tab_lookup: SELECT count(*) >= 0 FROM truncate_tab;
|
||||
?column?
|
||||
|
||||
t
|
||||
step s2_grant: GRANT TRUNCATE ON truncate_tab TO regress_truncate_conflict;
|
||||
step s2_auth: SET ROLE regress_truncate_conflict;
|
||||
step s2_truncate: TRUNCATE truncate_tab; <waiting ...>
|
||||
step s1_commit: COMMIT;
|
||||
step s2_truncate: <... completed>
|
||||
step s2_reset: RESET ROLE;
|
||||
|
||||
starting permutation: s1_begin s2_grant s2_auth s2_truncate s1_tab_lookup s1_commit s2_reset
|
||||
step s1_begin: BEGIN;
|
||||
step s2_grant: GRANT TRUNCATE ON truncate_tab TO regress_truncate_conflict;
|
||||
step s2_auth: SET ROLE regress_truncate_conflict;
|
||||
step s2_truncate: TRUNCATE truncate_tab;
|
||||
step s1_tab_lookup: SELECT count(*) >= 0 FROM truncate_tab;
|
||||
?column?
|
||||
|
||||
t
|
||||
step s1_commit: COMMIT;
|
||||
step s2_reset: RESET ROLE;
|
||||
|
||||
starting permutation: s1_begin s2_grant s2_auth s1_tab_lookup s2_truncate s1_commit s2_reset
|
||||
step s1_begin: BEGIN;
|
||||
step s2_grant: GRANT TRUNCATE ON truncate_tab TO regress_truncate_conflict;
|
||||
step s2_auth: SET ROLE regress_truncate_conflict;
|
||||
step s1_tab_lookup: SELECT count(*) >= 0 FROM truncate_tab;
|
||||
?column?
|
||||
|
||||
t
|
||||
step s2_truncate: TRUNCATE truncate_tab; <waiting ...>
|
||||
step s1_commit: COMMIT;
|
||||
step s2_truncate: <... completed>
|
||||
step s2_reset: RESET ROLE;
|
||||
|
||||
starting permutation: s2_grant s2_auth s2_truncate s1_begin s1_tab_lookup s1_commit s2_reset
|
||||
step s2_grant: GRANT TRUNCATE ON truncate_tab TO regress_truncate_conflict;
|
||||
step s2_auth: SET ROLE regress_truncate_conflict;
|
||||
step s2_truncate: TRUNCATE truncate_tab;
|
||||
step s1_begin: BEGIN;
|
||||
step s1_tab_lookup: SELECT count(*) >= 0 FROM truncate_tab;
|
||||
?column?
|
||||
|
||||
t
|
||||
step s1_commit: COMMIT;
|
||||
step s2_reset: RESET ROLE;
|
@ -76,3 +76,4 @@ test: partition-key-update-2
|
||||
test: partition-key-update-3
|
||||
test: partition-key-update-4
|
||||
test: plpgsql-toast
|
||||
test: truncate-conflict
|
||||
|
38
src/test/isolation/specs/truncate-conflict.spec
Normal file
38
src/test/isolation/specs/truncate-conflict.spec
Normal file
@ -0,0 +1,38 @@
|
||||
# Tests for locking conflicts with TRUNCATE commands.
|
||||
|
||||
setup
|
||||
{
|
||||
CREATE ROLE regress_truncate_conflict;
|
||||
CREATE TABLE truncate_tab (a int);
|
||||
}
|
||||
|
||||
teardown
|
||||
{
|
||||
DROP TABLE truncate_tab;
|
||||
DROP ROLE regress_truncate_conflict;
|
||||
}
|
||||
|
||||
session "s1"
|
||||
step "s1_begin" { BEGIN; }
|
||||
step "s1_tab_lookup" { SELECT count(*) >= 0 FROM truncate_tab; }
|
||||
step "s1_commit" { COMMIT; }
|
||||
|
||||
session "s2"
|
||||
step "s2_grant" { GRANT TRUNCATE ON truncate_tab TO regress_truncate_conflict; }
|
||||
step "s2_auth" { SET ROLE regress_truncate_conflict; }
|
||||
step "s2_truncate" { TRUNCATE truncate_tab; }
|
||||
step "s2_reset" { RESET ROLE; }
|
||||
|
||||
# The role doesn't have privileges to truncate the table, so TRUNCATE should
|
||||
# immediately fail without waiting for a lock.
|
||||
permutation "s1_begin" "s1_tab_lookup" "s2_auth" "s2_truncate" "s1_commit" "s2_reset"
|
||||
permutation "s1_begin" "s2_auth" "s2_truncate" "s1_tab_lookup" "s1_commit" "s2_reset"
|
||||
permutation "s1_begin" "s2_auth" "s1_tab_lookup" "s2_truncate" "s1_commit" "s2_reset"
|
||||
permutation "s2_auth" "s2_truncate" "s1_begin" "s1_tab_lookup" "s1_commit" "s2_reset"
|
||||
|
||||
# The role has privileges to truncate the table, TRUNCATE will block if
|
||||
# another session holds a lock on the table and succeed in all cases.
|
||||
permutation "s1_begin" "s1_tab_lookup" "s2_grant" "s2_auth" "s2_truncate" "s1_commit" "s2_reset"
|
||||
permutation "s1_begin" "s2_grant" "s2_auth" "s2_truncate" "s1_tab_lookup" "s1_commit" "s2_reset"
|
||||
permutation "s1_begin" "s2_grant" "s2_auth" "s1_tab_lookup" "s2_truncate" "s1_commit" "s2_reset"
|
||||
permutation "s2_grant" "s2_auth" "s2_truncate" "s1_begin" "s1_tab_lookup" "s1_commit" "s2_reset"
|
Reference in New Issue
Block a user