postgresql/src/backend/commands
Michael Paquier a556549d7e Improve VACUUM and ANALYZE by avoiding early lock queue
A caller of VACUUM can perform early lookup obtention which can cause
other sessions to block on the request done, causing potentially DOS
attacks as even a non-privileged user can attempt a vacuum fill of a
critical catalog table to block even all incoming connection attempts.

Contrary to TRUNCATE, a client could attempt a system-wide VACUUM after
building the list of relations to VACUUM, which can cause vacuum_rel()
or analyze_rel() to try to lock the relation but the operation would
just block.  When the client specifies a list of relations and the
relation needs to be skipped, ownership checks are done when building
the list of relations to work on, preventing a later lock attempt.

vacuum_rel() already had the sanity checks needed, except that those
were applied too late.  This commit refactors the code so as relation
skips are checked beforehand, making it safer to avoid too early locks,
for both manual VACUUM with and without a list of relations specified.

An isolation test is added emulating the fact that early locks do not
happen anymore, issuing a WARNING message earlier if the user calling
VACUUM is not a relation owner.

When a partitioned table is listed in a manual VACUUM or ANALYZE
command, its full list of partitions is fetched, all partitions get
added to the list to work on, and then each one of them is processed one
by one, with ownership checks happening at the later phase of
vacuum_rel() or analyze_rel().  Trying to do early ownership checks for
each partition is proving to be tedious as this would result in deadlock
risks with lock upgrades, and skipping all partitions if the listed
partitioned table is not owned would result in a behavior change
compared to how Postgres 10 has implemented vacuum for partitioned
tables.  The original problem reported related to early lock queue for
critical relations is fixed anyway, so priority is given to avoiding a
backward-incompatible behavior.

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/20180812222142.GA6097@paquier.xyz
2018-08-27 09:11:12 +09:00
..
aggregatecmds.c Improve spelling of new FINALFUNC_MODIFY aggregate attribute. 2018-05-21 11:41:42 -04:00
alter.c Post-feature-freeze pgindent run. 2018-04-26 14:47:16 -04:00
amcmds.c Update copyright for 2018 2018-01-02 23:30:12 -05:00
analyze.c Improve VACUUM and ANALYZE by avoiding early lock queue 2018-08-27 09:11:12 +09:00
async.c Update copyright for 2018 2018-01-02 23:30:12 -05:00
cluster.c Refactor cluster_rel() to handle more options 2018-07-24 11:37:32 +09:00
collationcmds.c Merge catalog/pg_foo_fn.h headers back into pg_foo.h headers. 2018-04-08 14:35:29 -04:00
comment.c Update copyright for 2018 2018-01-02 23:30:12 -05:00
constraint.c Update copyright for 2018 2018-01-02 23:30:12 -05:00
conversioncmds.c Merge catalog/pg_foo_fn.h headers back into pg_foo.h headers. 2018-04-08 14:35:29 -04:00
copy.c Allow multi-inserts during COPY into a partitioned table 2018-08-01 10:23:09 +02:00
createas.c Update copyright for 2018 2018-01-02 23:30:12 -05:00
dbcommands.c Rename TransactionChain functions 2018-03-16 13:18:06 -04:00
define.c Update copyright for 2018 2018-01-02 23:30:12 -05:00
discard.c Rename TransactionChain functions 2018-03-16 13:18:06 -04:00
dropcmds.c Fix more wrong paths in header comments 2018-07-11 17:57:04 +03:00
event_trigger.c Post-feature-freeze pgindent run. 2018-04-26 14:47:16 -04:00
explain.c Fix misc typos, mostly in comments. 2018-07-18 16:17:32 +03:00
extension.c Replace AclObjectKind with ObjectType 2018-01-19 14:01:15 -05:00
foreigncmds.c Don't record FDW user mappings as members of extensions. 2018-08-07 16:32:50 -04:00
functioncmds.c Prohibit transaction commands in security definer procedures 2018-07-13 10:41:32 +02:00
indexcmds.c Restrict access to reindex of shared catalogs for non-privileged users 2018-08-09 09:40:15 +02:00
lockcmds.c Post-feature-freeze pgindent run. 2018-04-26 14:47:16 -04:00
Makefile Implement multivariate n-distinct coefficients 2017-03-24 14:06:10 -03:00
matview.c Indexes with INCLUDE columns and their support in B-tree 2018-04-07 23:00:39 +03:00
opclasscmds.c doc: Update uses of the word "procedure" 2018-08-22 14:44:49 +02:00
operatorcmds.c Change PROCEDURE to FUNCTION in CREATE OPERATOR syntax 2018-08-22 14:44:49 +02:00
policy.c Post-feature-freeze pgindent run. 2018-04-26 14:47:16 -04:00
portalcmds.c Post-feature-freeze pgindent run. 2018-04-26 14:47:16 -04:00
prepare.c Revert MERGE patch 2018-04-12 11:22:56 +01:00
proclang.c Merge catalog/pg_foo_fn.h headers back into pg_foo.h headers. 2018-04-08 14:35:29 -04:00
publicationcmds.c Merge catalog/pg_foo_fn.h headers back into pg_foo.h headers. 2018-04-08 14:35:29 -04:00
schemacmds.c Replace AclObjectKind with ObjectType 2018-01-19 14:01:15 -05:00
seclabel.c Update copyright for 2018 2018-01-02 23:30:12 -05:00
sequence.c Combine options for RangeVarGetRelidExtended() into a flags argument. 2018-03-30 17:05:16 -07:00
statscmds.c Post-feature-freeze pgindent run. 2018-04-26 14:47:16 -04:00
subscriptioncmds.c Split the SetSubscriptionRelState function into two 2018-04-06 10:00:26 -04:00
tablecmds.c Fix set of NLS translation issues 2018-08-21 15:17:13 +09:00
tablespace.c Refactor dir/file permissions 2018-04-07 17:45:39 -04:00
trigger.c Allow using the updated tuple while moving it to a different partition. 2018-07-12 12:51:39 +05:30
tsearchcmds.c Avoid unnecessary use of pg_strcasecmp for already-downcased identifiers. 2018-01-26 18:25:14 -05:00
typecmds.c Fix a couple minor typos 2018-04-20 19:04:54 -04:00
user.c Replace AclObjectKind with ObjectType 2018-01-19 14:01:15 -05:00
vacuum.c Improve VACUUM and ANALYZE by avoiding early lock queue 2018-08-27 09:11:12 +09:00
vacuumlazy.c Further cleanup of client dependencies on src/include/catalog headers. 2018-04-09 14:39:58 -04:00
variable.c Update copyright for 2018 2018-01-02 23:30:12 -05:00
view.c Fix set of NLS translation issues 2018-08-21 15:17:13 +09:00