pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Bossart, Nathan <bossartn@amazon.com>
To: Michael Paquier <michael@paquier.xyz>
To: Andres Freund <andres@anarazel.de>
Cc: Masahiko Sawada <sawada.mshk@gmail.com>
Cc: Robert Haas <robertmhaas@gmail.com>
Cc: Lyes Ameddah <lyes.amd@gmail.com>
Cc: Tomas Vondra <tomas.vondra@2ndquadrant.com>
Cc: pgsql-hackers@postgresql.org <pgsql-hackers@postgresql.org>
Cc: pgsql-bugs@postgresql.org <pgsql-bugs@postgresql.org>
Subject: Re: BUG #14941: Vacuum crashes
Date: Thu, 5 Apr 2018 20:29:38 +0000
Message-ID: <239EE2DC-9FB3-4CB2-92AC-94E035F8BCB1@amazon.com> (raw)
In-Reply-To: <20180403051146.GA12320@paquier.xyz>
References: <4B232BD7-A97D-4461-81E6-28A49764B60E@amazon.com>
	<20180110025331.GE336@paquier.xyz>
	<CBEE19BB-6C70-424C-95E5-F8ABC7AA2185@amazon.com>
	<20180110231442.GA22983@paquier.xyz>
	<20180304001252.5sxn3glccnu5qxjm@alap3.anarazel.de>
	<7327B413-1A57-477F-A6A0-6FD80CB5482D@amazon.com>
	<20180307050345.GA3095@paquier.xyz>
	<3834EFB8-2A90-41C7-A7C8-8B956150479D@amazon.com>
	<4D1115BF-ED38-44E2-B799-C7F9E421A3B8@amazon.com>
	<20180331001946.uie7elezusxes7qw@alap3.anarazel.de>
	<20180403051146.GA12320@paquier.xyz>

On 3/30/18, 7:19 PM, "Andres Freund" <andres@anarazel.de> wrote:
> Could you post a rebased version of the patch, with an incremental
> addition of what you propose in a separate patch?

Here is a new patch set.  I have attempted to add skip-locked
functionality everywhere it is needed, including VACUUM FULL.

On 4/3/18, 12:12 AM, "Michael Paquier" <michael@paquier.xyz> wrote:
> On Fri, Mar 30, 2018 at 05:19:46PM -0700, Andres Freund wrote:
>> On 2018-03-30 18:39:01 +0000, Bossart, Nathan wrote:
>>> I think the most straightforward approach for fixing this is to add
>>> skip-locked functionality in find_all_inheritors(),
>>> find_inheritance_children(), and vac_open_indexes(), but I am curious
>>> what others think.
>
> For vac_open_indexes() it may make the most sense to introduce a new
> try_open_index which wraps on top of try_relation_open with checks on
> the opened relation's relkind. 

I looked into this, but I noticed that the analogous
try_relation_open() implements missing-ok for relation_open().  So, I
thought it might be confusing to have try_index_open() be a skip-
locked version of index_open().

The new tests are now passing as expected, but I am still doing some
manual testing.  Since there is quite a bit of added complexity in
this new patch set, I will certainly not be surprised if this gets
moved to the next commitfest.

Nathan



Attachments: download all (12 files, .tar.gz)

v7-0001-Add-skip_locked-argument-to-find_inheritance_chil.patch (application/octet-stream, 8.5K) download From 2e8936a89e9bbc8bd25e611617b435212402bc2b Mon Sep 17 00:00:00 2001 From: Nathan Bossart <bossartn@amazon.com> Date: Thu, 5 Apr 2018 16:45:49 +0000 Subject: [PATCH v7 01/12] Add skip_locked argument to find_inheritance_children(). --- src/backend/catalog/partition.c | 2 +- src/backend/catalog/pg_inherits.c | 26 +++++++++++++++++++++++--- src/backend/commands/lockcmds.c | 2 +- src/backend/commands/tablecmds.c | 16 ++++++++-------- src/backend/commands/trigger.c | 2 +- src/include/catalog/pg_inherits_fn.h | 3 ++- 6 files changed, 36 insertions(+), 15 deletions(-) diff --git a/src/backend/catalog/partition.c b/src/backend/catalog/partition.c index 39ee773d93..42453c5007 100644 --- a/src/backend/catalog/partition.c +++ b/src/backend/catalog/partition.c @@ -232,7 +232,7 @@ RelationBuildPartitionDesc(Relation rel) PartitionRangeBound **rbounds = NULL; /* Get partition oids from pg_inherits */ - inhoids = find_inheritance_children(RelationGetRelid(rel), NoLock); + inhoids = find_inheritance_children(RelationGetRelid(rel), NoLock, false, NULL); /* Collect bound spec nodes in a list */ i = 0; diff --git a/src/backend/catalog/pg_inherits.c b/src/backend/catalog/pg_inherits.c index 5a5beb9273..17f8b58fba 100644 --- a/src/backend/catalog/pg_inherits.c +++ b/src/backend/catalog/pg_inherits.c @@ -52,9 +52,14 @@ typedef struct SeenRelsEntry * given rel; caller should already have locked it). If lockmode is NoLock * then no locks are acquired, but caller must beware of race conditions * against possible DROPs of child relations. + * + * If skip_locked is true, any child relations that cannot be locked immediately + * without waiting are not added to the returned OID list, and skipped will be + * set to true if it is provided. Otherwise, skipped will be set to false if it + * is provided. */ List * -find_inheritance_children(Oid parentrelId, LOCKMODE lockmode) +find_inheritance_children(Oid parentrelId, LOCKMODE lockmode, bool skip_locked, bool *skipped) { List *list = NIL; Relation relation; @@ -66,13 +71,19 @@ find_inheritance_children(Oid parentrelId, LOCKMODE lockmode) int maxoids, numoids, i; + bool did_skip = false; /* * Can skip the scan if pg_class shows the relation has never had a * subclass. */ if (!has_subclass(parentrelId)) + { + if (skipped != NULL) + *skipped = did_skip; + return NIL; + } /* * Scan pg_inherits and build a working array of subclass OIDs. @@ -125,7 +136,13 @@ find_inheritance_children(Oid parentrelId, LOCKMODE lockmode) if (lockmode != NoLock) { /* Get the lock to synchronize against concurrent drop */ - LockRelationOid(inhrelid, lockmode); + if (!skip_locked) + LockRelationOid(inhrelid, lockmode); + else if (!ConditionalLockRelationOid(inhrelid, lockmode)) + { + did_skip = true; + continue; + } /* * Now that we have the lock, double-check to see if the relation @@ -146,6 +163,9 @@ find_inheritance_children(Oid parentrelId, LOCKMODE lockmode) pfree(oidarr); + if (skipped != NULL) + *skipped = did_skip; + return list; } @@ -200,7 +220,7 @@ find_all_inheritors(Oid parentrelId, LOCKMODE lockmode, List **numparents) ListCell *lc; /* Get the direct children of this rel */ - currentchildren = find_inheritance_children(currentrel, lockmode); + currentchildren = find_inheritance_children(currentrel, lockmode, false, NULL); /* * Add to the queue only those children not already seen. This avoids diff --git a/src/backend/commands/lockcmds.c b/src/backend/commands/lockcmds.c index b247c0fe2e..1039159946 100644 --- a/src/backend/commands/lockcmds.c +++ b/src/backend/commands/lockcmds.c @@ -120,7 +120,7 @@ LockTableRecurse(Oid reloid, LOCKMODE lockmode, bool nowait, Oid userid) List *children; ListCell *lc; - children = find_inheritance_children(reloid, NoLock); + children = find_inheritance_children(reloid, NoLock, false, NULL); foreach(lc, children) { diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index ec2f9616ed..15017c5a62 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -2694,7 +2694,7 @@ renameatt_internal(Oid myrelid, * expected_parents will only be 0 if we are not already recursing. */ if (expected_parents == 0 && - find_inheritance_children(myrelid, NoLock) != NIL) + find_inheritance_children(myrelid, NoLock, false, NULL) != NIL) ereport(ERROR, (errcode(ERRCODE_INVALID_TABLE_DEFINITION), errmsg("inherited column \"%s\" must be renamed in child tables too", @@ -2893,7 +2893,7 @@ rename_constraint_internal(Oid myrelid, else { if (expected_parents == 0 && - find_inheritance_children(myrelid, NoLock) != NIL) + find_inheritance_children(myrelid, NoLock, false, NULL) != NIL) ereport(ERROR, (errcode(ERRCODE_INVALID_TABLE_DEFINITION), errmsg("inherited constraint \"%s\" must be renamed in child tables too", @@ -5354,7 +5354,7 @@ ATExecAddColumn(List **wqueue, AlteredTableInfo *tab, Relation rel, */ if (colDef->identity && recurse && - find_inheritance_children(myrelid, NoLock) != NIL) + find_inheritance_children(myrelid, NoLock, false, NULL) != NIL) ereport(ERROR, (errcode(ERRCODE_INVALID_TABLE_DEFINITION), errmsg("cannot recursively add identity column to table that has child tables"))); @@ -5597,7 +5597,7 @@ ATExecAddColumn(List **wqueue, AlteredTableInfo *tab, Relation rel, * routines, we have to do this one level of recursion at a time; we can't * use find_all_inheritors to do it in one pass. */ - children = find_inheritance_children(RelationGetRelid(rel), lockmode); + children = find_inheritance_children(RelationGetRelid(rel), lockmode, false, NULL); /* * If we are told not to recurse, there had better not be any child @@ -6693,7 +6693,7 @@ ATExecDropColumn(List **wqueue, Relation rel, const char *colName, * routines, we have to do this one level of recursion at a time; we can't * use find_all_inheritors to do it in one pass. */ - children = find_inheritance_children(RelationGetRelid(rel), lockmode); + children = find_inheritance_children(RelationGetRelid(rel), lockmode, false, NULL); if (children) { @@ -7144,7 +7144,7 @@ ATAddCheckConstraint(List **wqueue, AlteredTableInfo *tab, Relation rel, * routines, we have to do this one level of recursion at a time; we can't * use find_all_inheritors to do it in one pass. */ - children = find_inheritance_children(RelationGetRelid(rel), lockmode); + children = find_inheritance_children(RelationGetRelid(rel), lockmode, false, NULL); /* * Check if ONLY was specified with ALTER TABLE. If so, allow the @@ -8831,7 +8831,7 @@ ATExecDropConstraint(Relation rel, const char *constrName, * use find_all_inheritors to do it in one pass. */ if (!is_no_inherit_constraint) - children = find_inheritance_children(RelationGetRelid(rel), lockmode); + children = find_inheritance_children(RelationGetRelid(rel), lockmode, false, NULL); else children = NIL; @@ -9173,7 +9173,7 @@ ATPrepAlterColumnType(List **wqueue, } } else if (!recursing && - find_inheritance_children(RelationGetRelid(rel), NoLock) != NIL) + find_inheritance_children(RelationGetRelid(rel), NoLock, false, NULL) != NIL) ereport(ERROR, (errcode(ERRCODE_INVALID_TABLE_DEFINITION), errmsg("type of inherited column \"%s\" must be changed in child tables too", diff --git a/src/backend/commands/trigger.c b/src/backend/commands/trigger.c index a189356cad..a7e549e0ae 100644 --- a/src/backend/commands/trigger.c +++ b/src/backend/commands/trigger.c @@ -1115,7 +1115,7 @@ CreateTrigger(CreateTrigStmt *stmt, const char *queryString, ListCell *l; List *idxs = NIL; - idxs = find_inheritance_children(indexOid, ShareRowExclusiveLock); + idxs = find_inheritance_children(indexOid, ShareRowExclusiveLock, false, NULL); foreach(l, idxs) childTbls = lappend_oid(childTbls, IndexGetRelation(lfirst_oid(l), diff --git a/src/include/catalog/pg_inherits_fn.h b/src/include/catalog/pg_inherits_fn.h index eebee977a5..34d2a2f39f 100644 --- a/src/include/catalog/pg_inherits_fn.h +++ b/src/include/catalog/pg_inherits_fn.h @@ -17,7 +17,8 @@ #include "nodes/pg_list.h" #include "storage/lock.h" -extern List *find_inheritance_children(Oid parentrelId, LOCKMODE lockmode); +extern List *find_inheritance_children(Oid parentrelId, LOCKMODE lockmode, + bool skip_locked, bool *skipped); extern List *find_all_inheritors(Oid parentrelId, LOCKMODE lockmode, List **parents); extern bool has_subclass(Oid relationId); -- 2.16.2
v7-0002-Add-skip_locked-argument-to-find_all_inheritors.patch (application/octet-stream, 11.8K) download From a970360bc97f5d59b4d6919700432ae7d2573c73 Mon Sep 17 00:00:00 2001 From: Nathan Bossart <bossartn@amazon.com> Date: Thu, 5 Apr 2018 16:52:56 +0000 Subject: [PATCH v7 02/12] Add skip_locked argument to find_all_inheritors(). --- contrib/sepgsql/dml.c | 2 +- src/backend/catalog/partition.c | 2 +- src/backend/catalog/pg_inherits.c | 30 +++++++++++++++++++++++++++--- src/backend/commands/analyze.c | 2 +- src/backend/commands/publicationcmds.c | 2 +- src/backend/commands/tablecmds.c | 18 +++++++++--------- src/backend/commands/trigger.c | 2 +- src/backend/commands/vacuum.c | 2 +- src/backend/executor/execPartition.c | 2 +- src/backend/optimizer/prep/prepunion.c | 2 +- src/backend/tcop/utility.c | 2 +- src/include/catalog/pg_inherits_fn.h | 2 +- 12 files changed, 46 insertions(+), 22 deletions(-) diff --git a/contrib/sepgsql/dml.c b/contrib/sepgsql/dml.c index c1fa320eb4..bbc72a59a2 100644 --- a/contrib/sepgsql/dml.c +++ b/contrib/sepgsql/dml.c @@ -330,7 +330,7 @@ sepgsql_dml_privileges(List *rangeTabls, bool abort_on_violation) if (!rte->inh) tableIds = list_make1_oid(rte->relid); else - tableIds = find_all_inheritors(rte->relid, NoLock, NULL); + tableIds = find_all_inheritors(rte->relid, NoLock, NULL, false); foreach(li, tableIds) { diff --git a/src/backend/catalog/partition.c b/src/backend/catalog/partition.c index 42453c5007..b701073dcb 100644 --- a/src/backend/catalog/partition.c +++ b/src/backend/catalog/partition.c @@ -1270,7 +1270,7 @@ check_default_allows_bound(Relation parent, Relation default_rel, */ if (default_rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE) all_parts = find_all_inheritors(RelationGetRelid(default_rel), - AccessExclusiveLock, NULL); + AccessExclusiveLock, NULL, false); else all_parts = list_make1_oid(RelationGetRelid(default_rel)); diff --git a/src/backend/catalog/pg_inherits.c b/src/backend/catalog/pg_inherits.c index 17f8b58fba..c986540974 100644 --- a/src/backend/catalog/pg_inherits.c +++ b/src/backend/catalog/pg_inherits.c @@ -181,10 +181,12 @@ find_inheritance_children(Oid parentrelId, LOCKMODE lockmode, bool skip_locked, * The specified lock type is acquired on all child relations (but not on the * given rel; caller should already have locked it). If lockmode is NoLock * then no locks are acquired, but caller must beware of race conditions - * against possible DROPs of child relations. + * against possible DROPs of child relations. If skip_locked is true and a + * child relation can not be locked immediately without waiting, both the + * returned OID list and *numparents will be set to NIL. */ List * -find_all_inheritors(Oid parentrelId, LOCKMODE lockmode, List **numparents) +find_all_inheritors(Oid parentrelId, LOCKMODE lockmode, List **numparents, bool skip_locked) { /* hash table for O(1) rel_oid -> rel_numparents cell lookup */ HTAB *seen_rels; @@ -192,6 +194,7 @@ find_all_inheritors(Oid parentrelId, LOCKMODE lockmode, List **numparents) List *rels_list, *rel_numparents; ListCell *l; + bool skipped = false; memset(&ctl, 0, sizeof(ctl)); ctl.keysize = sizeof(Oid); @@ -220,7 +223,7 @@ find_all_inheritors(Oid parentrelId, LOCKMODE lockmode, List **numparents) ListCell *lc; /* Get the direct children of this rel */ - currentchildren = find_inheritance_children(currentrel, lockmode, false, NULL); + currentchildren = find_inheritance_children(currentrel, lockmode, skip_locked, &skipped); /* * Add to the queue only those children not already seen. This avoids @@ -249,6 +252,27 @@ find_all_inheritors(Oid parentrelId, LOCKMODE lockmode, List **numparents) hash_entry->numparents_cell = rel_numparents->tail; } } + + if (skipped) + break; + } + + /* if a child relation was skipped, set the return lists to NIL */ + if (skipped) + { + /* release all locks */ + foreach(l, rels_list) + { + Oid currentrel = lfirst_oid(l); + + if (currentrel != parentrelId) + UnlockRelationOid(currentrel, lockmode); + } + + list_free(rel_numparents); + list_free(rels_list); + rel_numparents = NIL; + rels_list = NIL; } if (numparents) diff --git a/src/backend/commands/analyze.c b/src/backend/commands/analyze.c index 378784a93c..451e923132 100644 --- a/src/backend/commands/analyze.c +++ b/src/backend/commands/analyze.c @@ -1335,7 +1335,7 @@ acquire_inherited_sample_rows(Relation onerel, int elevel, * the children. */ tableOIDs = - find_all_inheritors(RelationGetRelid(onerel), AccessShareLock, NULL); + find_all_inheritors(RelationGetRelid(onerel), AccessShareLock, NULL, false); /* * Check that there's at least one descendant, else fail. This could diff --git a/src/backend/commands/publicationcmds.c b/src/backend/commands/publicationcmds.c index 9c5aa9ebc2..b347d671a5 100644 --- a/src/backend/commands/publicationcmds.c +++ b/src/backend/commands/publicationcmds.c @@ -516,7 +516,7 @@ OpenTableList(List *tables) List *children; children = find_all_inheritors(myrelid, ShareUpdateExclusiveLock, - NULL); + NULL, false); foreach(child, children) { diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 15017c5a62..3d19dd728f 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -1356,7 +1356,7 @@ ExecuteTruncate(TruncateStmt *stmt) ListCell *child; List *children; - children = find_all_inheritors(myrelid, AccessExclusiveLock, NULL); + children = find_all_inheritors(myrelid, AccessExclusiveLock, NULL, false); foreach(child, children) { @@ -2667,7 +2667,7 @@ renameatt_internal(Oid myrelid, * outside the inheritance hierarchy being processed. */ child_oids = find_all_inheritors(myrelid, AccessExclusiveLock, - &child_numparents); + &child_numparents, false); /* * find_all_inheritors does the recursive search of the inheritance @@ -2877,7 +2877,7 @@ rename_constraint_internal(Oid myrelid, *li; child_oids = find_all_inheritors(myrelid, AccessExclusiveLock, - &child_numparents); + &child_numparents, false); forboth(lo, child_oids, li, child_numparents) { @@ -4943,7 +4943,7 @@ ATSimpleRecursion(List **wqueue, Relation rel, ListCell *child; List *children; - children = find_all_inheritors(relid, lockmode, NULL); + children = find_all_inheritors(relid, lockmode, NULL, false); /* * find_all_inheritors does the recursive search of the inheritance @@ -7918,7 +7918,7 @@ ATExecValidateConstraint(Relation rel, char *constrName, bool recurse, */ if (!recursing && !con->connoinherit) children = find_all_inheritors(RelationGetRelid(rel), - lockmode, NULL); + lockmode, NULL, false); /* * For CHECK constraints, we must ensure that we only mark the @@ -9122,7 +9122,7 @@ ATPrepAlterColumnType(List **wqueue, ListCell *child; List *children; - children = find_all_inheritors(relid, lockmode, NULL); + children = find_all_inheritors(relid, lockmode, NULL, false); /* * find_all_inheritors does the recursive search of the inheritance @@ -11355,7 +11355,7 @@ ATExecAddInherit(Relation child_rel, RangeVar *parent, LOCKMODE lockmode) * We use weakest lock we can on child's children, namely AccessShareLock. */ children = find_all_inheritors(RelationGetRelid(child_rel), - AccessShareLock, NULL); + AccessShareLock, NULL, false); if (list_member_oid(children, RelationGetRelid(parent_rel))) ereport(ERROR, @@ -14059,7 +14059,7 @@ ATExecAttachPartition(List **wqueue, Relation rel, PartitionCmd *cmd) * weaker lock now and the stronger one only when needed. */ attachrel_children = find_all_inheritors(RelationGetRelid(attachrel), - AccessExclusiveLock, NULL); + AccessExclusiveLock, NULL, false); if (list_member_oid(attachrel_children, RelationGetRelid(rel))) ereport(ERROR, (errcode(ERRCODE_DUPLICATE_TABLE), @@ -14252,7 +14252,7 @@ ATExecAttachPartition(List **wqueue, Relation rel, PartitionCmd *cmd) get_proposed_default_constraint(partBoundConstraint); defaultrel_children = find_all_inheritors(defaultPartOid, - AccessExclusiveLock, NULL); + AccessExclusiveLock, NULL, false); ValidatePartitionConstraints(wqueue, defaultrel, defaultrel_children, defPartConstraint, true); diff --git a/src/backend/commands/trigger.c b/src/backend/commands/trigger.c index a7e549e0ae..1aa65ffc97 100644 --- a/src/backend/commands/trigger.c +++ b/src/backend/commands/trigger.c @@ -371,7 +371,7 @@ CreateTrigger(CreateTrigStmt *stmt, const char *queryString, rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE; if (partition_recurse) list_free(find_all_inheritors(RelationGetRelid(rel), - ShareRowExclusiveLock, NULL)); + ShareRowExclusiveLock, NULL, false)); /* Compute tgtype */ TRIGGER_CLEAR_TYPE(tgtype); diff --git a/src/backend/commands/vacuum.c b/src/backend/commands/vacuum.c index a1782c2874..db1b6e2d26 100644 --- a/src/backend/commands/vacuum.c +++ b/src/backend/commands/vacuum.c @@ -479,7 +479,7 @@ expand_vacuum_rel(VacuumRelation *vrel) */ if (include_parts) { - List *part_oids = find_all_inheritors(relid, NoLock, NULL); + List *part_oids = find_all_inheritors(relid, NoLock, NULL, false); ListCell *part_lc; foreach(part_lc, part_oids) diff --git a/src/backend/executor/execPartition.c b/src/backend/executor/execPartition.c index ad532773a3..bf77eedbcb 100644 --- a/src/backend/executor/execPartition.c +++ b/src/backend/executor/execPartition.c @@ -77,7 +77,7 @@ ExecSetupPartitionTupleRouting(ModifyTableState *mtstate, Relation rel) * Get the information about the partition tree after locking all the * partitions. */ - (void) find_all_inheritors(RelationGetRelid(rel), RowExclusiveLock, NULL); + (void) find_all_inheritors(RelationGetRelid(rel), RowExclusiveLock, NULL, false); proute = (PartitionTupleRouting *) palloc0(sizeof(PartitionTupleRouting)); proute->partition_dispatch_info = RelationGetPartitionDispatchInfo(rel, &proute->num_dispatch, diff --git a/src/backend/optimizer/prep/prepunion.c b/src/backend/optimizer/prep/prepunion.c index 5236ab378e..d5112a6ec8 100644 --- a/src/backend/optimizer/prep/prepunion.c +++ b/src/backend/optimizer/prep/prepunion.c @@ -1556,7 +1556,7 @@ expand_inherited_rtentry(PlannerInfo *root, RangeTblEntry *rte, Index rti) lockmode = AccessShareLock; /* Scan for all members of inheritance set, acquire needed locks */ - inhOIDs = find_all_inheritors(parentOID, lockmode, NULL); + inhOIDs = find_all_inheritors(parentOID, lockmode, NULL, false); /* * Check that there's at least one descendant, else treat as no-child diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 84f2591736..af4e1fb0ff 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1323,7 +1323,7 @@ ProcessUtilitySlow(ParseState *pstate, rel = heap_open(relid, NoLock); if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE) inheritors = find_all_inheritors(relid, lockmode, - NULL); + NULL, false); heap_close(rel, NoLock); } diff --git a/src/include/catalog/pg_inherits_fn.h b/src/include/catalog/pg_inherits_fn.h index 34d2a2f39f..5374dc48da 100644 --- a/src/include/catalog/pg_inherits_fn.h +++ b/src/include/catalog/pg_inherits_fn.h @@ -20,7 +20,7 @@ extern List *find_inheritance_children(Oid parentrelId, LOCKMODE lockmode, bool skip_locked, bool *skipped); extern List *find_all_inheritors(Oid parentrelId, LOCKMODE lockmode, - List **parents); + List **parents, bool skip_locked); extern bool has_subclass(Oid relationId); extern bool has_superclass(Oid relationId); extern bool typeInheritsFrom(Oid subclassTypeId, Oid superclassTypeId); -- 2.16.2
v7-0003-Add-skip_locked-argument-to-vac_open_indexes.patch (application/octet-stream, 3.9K) download From ac8708a0f9c65f7cee4898aa8d83d09294e2fa0d Mon Sep 17 00:00:00 2001 From: Nathan Bossart <bossartn@amazon.com> Date: Thu, 5 Apr 2018 16:58:27 +0000 Subject: [PATCH v7 03/12] Add skip_locked argument to vac_open_indexes(). --- src/backend/commands/analyze.c | 2 +- src/backend/commands/vacuum.c | 27 +++++++++++++++++++++++++-- src/backend/commands/vacuumlazy.c | 2 +- src/include/commands/vacuum.h | 3 ++- 4 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/backend/commands/analyze.c b/src/backend/commands/analyze.c index 451e923132..5fdbbd630e 100644 --- a/src/backend/commands/analyze.c +++ b/src/backend/commands/analyze.c @@ -468,7 +468,7 @@ do_analyze_rel(Relation onerel, int options, VacuumParams *params, * all. */ if (!inh) - vac_open_indexes(onerel, AccessShareLock, &nindexes, &Irel); + vac_open_indexes(onerel, AccessShareLock, &nindexes, &Irel, false, NULL); else { Irel = NULL; diff --git a/src/backend/commands/vacuum.c b/src/backend/commands/vacuum.c index db1b6e2d26..3ccf6cf98d 100644 --- a/src/backend/commands/vacuum.c +++ b/src/backend/commands/vacuum.c @@ -1603,14 +1603,19 @@ vacuum_rel(Oid relid, RangeVar *relation, int options, VacuumParams *params) * vacuum even if the index isn't indisvalid; this is important because in a * unique index, uniqueness checks will be performed anyway and had better not * hit dangling index pointers. + * + * If skip_locked is true and an index cannot be locked immediately without + * waiting, *Irel will be set to NULL and nindexes will be set to 0. If skipped + * is provided, it will be set to true if an index was skipped (false otherwise). */ void vac_open_indexes(Relation relation, LOCKMODE lockmode, - int *nindexes, Relation **Irel) + int *nindexes, Relation **Irel, bool skip_locked, bool *skipped) { List *indexoidlist; ListCell *indexoidscan; int i; + bool did_skip = false; Assert(lockmode != NoLock); @@ -1631,13 +1636,31 @@ vac_open_indexes(Relation relation, LOCKMODE lockmode, Oid indexoid = lfirst_oid(indexoidscan); Relation indrel; - indrel = index_open(indexoid, lockmode); + if (skip_locked && !ConditionalLockRelationOid(indexoid, lockmode)) + { + did_skip = true; + break; + } + + indrel = index_open(indexoid, skip_locked ? NoLock : lockmode); if (IndexIsReady(indrel->rd_index)) (*Irel)[i++] = indrel; else index_close(indrel, lockmode); } + if (did_skip) + { + vac_close_indexes(i, *Irel, lockmode); + + /* clear all return values */ + *Irel = NULL; + i = 0; + } + + if (skipped != NULL) + *skipped = did_skip; + *nindexes = i; list_free(indexoidlist); diff --git a/src/backend/commands/vacuumlazy.c b/src/backend/commands/vacuumlazy.c index d2a006671a..13e5bd452f 100644 --- a/src/backend/commands/vacuumlazy.c +++ b/src/backend/commands/vacuumlazy.c @@ -259,7 +259,7 @@ lazy_vacuum_rel(Relation onerel, int options, VacuumParams *params, vacrelstats->lock_waiter_detected = false; /* Open all indexes of the relation */ - vac_open_indexes(onerel, RowExclusiveLock, &nindexes, &Irel); + vac_open_indexes(onerel, RowExclusiveLock, &nindexes, &Irel, false, NULL); vacrelstats->hasindex = (nindexes > 0); /* Do the vacuuming */ diff --git a/src/include/commands/vacuum.h b/src/include/commands/vacuum.h index 85d472f0a5..69b1e384ef 100644 --- a/src/include/commands/vacuum.h +++ b/src/include/commands/vacuum.h @@ -160,7 +160,8 @@ extern void ExecVacuum(VacuumStmt *vacstmt, bool isTopLevel); extern void vacuum(int options, List *relations, VacuumParams *params, BufferAccessStrategy bstrategy, bool isTopLevel); extern void vac_open_indexes(Relation relation, LOCKMODE lockmode, - int *nindexes, Relation **Irel); + int *nindexes, Relation **Irel, + bool skip_locked, bool *skipped); extern void vac_close_indexes(int nindexes, Relation *Irel, LOCKMODE lockmode); extern double vac_estimate_reltuples(Relation relation, BlockNumber total_pages, -- 2.16.2
v7-0004-Rename-VACOPT_NOWAIT-to-VACOPT_SKIP_LOCKED.patch (application/octet-stream, 3.1K) download From c8357942dedb0df2eade3e460189e8fd23bbecac Mon Sep 17 00:00:00 2001 From: Nathan Bossart <bossartn@amazon.com> Date: Wed, 4 Apr 2018 16:54:53 +0000 Subject: [PATCH v7 04/12] Rename VACOPT_NOWAIT to VACOPT_SKIP_LOCKED. --- src/backend/commands/analyze.c | 2 +- src/backend/commands/vacuum.c | 2 +- src/backend/postmaster/autovacuum.c | 2 +- src/include/nodes/parsenodes.h | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/backend/commands/analyze.c b/src/backend/commands/analyze.c index 5fdbbd630e..4b05cb7fb3 100644 --- a/src/backend/commands/analyze.c +++ b/src/backend/commands/analyze.c @@ -143,7 +143,7 @@ analyze_rel(Oid relid, RangeVar *relation, int options, * matter if we ever try to accumulate stats on dead tuples.) If the rel * has been dropped since we last saw it, we don't need to process it. */ - if (!(options & VACOPT_NOWAIT)) + if (!(options & VACOPT_SKIP_LOCKED)) onerel = try_relation_open(relid, ShareUpdateExclusiveLock); else if (ConditionalLockRelationOid(relid, ShareUpdateExclusiveLock)) onerel = try_relation_open(relid, NoLock); diff --git a/src/backend/commands/vacuum.c b/src/backend/commands/vacuum.c index 3ccf6cf98d..7a48286108 100644 --- a/src/backend/commands/vacuum.c +++ b/src/backend/commands/vacuum.c @@ -1371,7 +1371,7 @@ vacuum_rel(Oid relid, RangeVar *relation, int options, VacuumParams *params) * If we've been asked not to wait for the relation lock, acquire it first * in non-blocking mode, before calling try_relation_open(). */ - if (!(options & VACOPT_NOWAIT)) + if (!(options & VACOPT_SKIP_LOCKED)) onerel = try_relation_open(relid, lmode); else if (ConditionalLockRelationOid(relid, lmode)) onerel = try_relation_open(relid, NoLock); diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c index 3b90b16dae..2d1930e838 100644 --- a/src/backend/postmaster/autovacuum.c +++ b/src/backend/postmaster/autovacuum.c @@ -2904,7 +2904,7 @@ table_recheck_autovac(Oid relid, HTAB *table_toast_map, tab->at_vacoptions = VACOPT_SKIPTOAST | (dovacuum ? VACOPT_VACUUM : 0) | (doanalyze ? VACOPT_ANALYZE : 0) | - (!wraparound ? VACOPT_NOWAIT : 0); + (!wraparound ? VACOPT_SKIP_LOCKED : 0); tab->at_params.freeze_min_age = freeze_min_age; tab->at_params.freeze_table_age = freeze_table_age; tab->at_params.multixact_freeze_min_age = multixact_freeze_min_age; diff --git a/src/include/nodes/parsenodes.h b/src/include/nodes/parsenodes.h index 699fa77bc7..5340d29f0b 100644 --- a/src/include/nodes/parsenodes.h +++ b/src/include/nodes/parsenodes.h @@ -3158,7 +3158,7 @@ typedef enum VacuumOption VACOPT_VERBOSE = 1 << 2, /* print progress info */ VACOPT_FREEZE = 1 << 3, /* FREEZE option */ VACOPT_FULL = 1 << 4, /* FULL (non-concurrent) vacuum */ - VACOPT_NOWAIT = 1 << 5, /* don't wait to get lock (autovacuum only) */ + VACOPT_SKIP_LOCKED = 1 << 5, /* don't wait to get lock (autovacuum only) */ VACOPT_SKIPTOAST = 1 << 6, /* don't process the TOAST table, if any */ VACOPT_DISABLE_PAGE_SKIPPING = 1 << 7 /* don't skip any pages */ } VacuumOption; -- 2.16.2
v7-0005-Add-assertion-that-we-are-not-an-autovacuum-worke.patch (application/octet-stream, 970B) download From d1df6f5ea08cc0a0b166bd6913a05d208de55585 Mon Sep 17 00:00:00 2001 From: Nathan Bossart <bossartn@amazon.com> Date: Wed, 4 Apr 2018 16:55:33 +0000 Subject: [PATCH v7 05/12] Add assertion that we are not an autovacuum worker in expand_vacuum_rel(). --- src/backend/commands/vacuum.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/backend/commands/vacuum.c b/src/backend/commands/vacuum.c index 7a48286108..55999b21d0 100644 --- a/src/backend/commands/vacuum.c +++ b/src/backend/commands/vacuum.c @@ -443,6 +443,12 @@ expand_vacuum_rel(VacuumRelation *vrel) Form_pg_class classForm; bool include_parts; + /* + * Since autovacuum workers supply OIDs when calling vacuum(), no + * autovacuum worker should reach this code. + */ + Assert(!IsAutoVacuumWorkerProcess()); + /* * We transiently take AccessShareLock to protect the syscache lookup * below, as well as find_all_inheritors's expectation that the caller -- 2.16.2
v7-0006-Create-a-helper-function-for-determining-the-log-.patch (application/octet-stream, 4.9K) download From 8f2eb87abc413586af1b3833aeb1eebf482f548b Mon Sep 17 00:00:00 2001 From: Nathan Bossart <bossartn@amazon.com> Date: Thu, 5 Apr 2018 17:20:42 +0000 Subject: [PATCH v7 06/12] Create a helper function for determining the log level for skipped relations. --- src/backend/commands/analyze.c | 45 +++++++++++----------------------- src/backend/commands/vacuum.c | 55 +++++++++++++++++++++++++----------------- src/include/commands/vacuum.h | 1 + 3 files changed, 48 insertions(+), 53 deletions(-) diff --git a/src/backend/commands/analyze.c b/src/backend/commands/analyze.c index 4b05cb7fb3..d9e31606ff 100644 --- a/src/backend/commands/analyze.c +++ b/src/backend/commands/analyze.c @@ -159,38 +159,21 @@ analyze_rel(Oid relid, RangeVar *relation, int options, */ if (!onerel) { - /* - * If the RangeVar is not defined, we do not have enough information - * to provide a meaningful log statement. Chances are that - * analyze_rel's caller has intentionally not provided this - * information so that this logging is skipped, anyway. - */ - if (relation == NULL) - return; - - /* - * Determine the log level. For autovacuum logs, we emit a LOG if - * log_autovacuum_min_duration is not disabled. For manual ANALYZE, - * we emit a WARNING to match the log statements in the permissions - * checks. - */ - if (!IsAutoVacuumWorkerProcess()) - elevel = WARNING; - else if (params->log_min_duration >= 0) - elevel = LOG; - else - return; + elevel = get_elevel_for_skipped_relation(relation, params); - if (!rel_lock) - ereport(elevel, - (errcode(ERRCODE_LOCK_NOT_AVAILABLE), - errmsg("skipping analyze of \"%s\" --- lock not available", - relation->relname))); - else - ereport(elevel, - (errcode(ERRCODE_UNDEFINED_TABLE), - errmsg("skipping analyze of \"%s\" --- relation no longer exists", - relation->relname))); + if (elevel != 0) + { + if (!rel_lock) + ereport(elevel, + (errcode(ERRCODE_LOCK_NOT_AVAILABLE), + errmsg("skipping analyze of \"%s\" --- lock not available", + relation->relname))); + else + ereport(elevel, + (errcode(ERRCODE_UNDEFINED_TABLE), + errmsg("skipping analyze of \"%s\" --- relation no longer exists", + relation->relname))); + } return; } diff --git a/src/backend/commands/vacuum.c b/src/backend/commands/vacuum.c index 55999b21d0..d7967dda6b 100644 --- a/src/backend/commands/vacuum.c +++ b/src/backend/commands/vacuum.c @@ -1393,28 +1393,7 @@ vacuum_rel(Oid relid, RangeVar *relation, int options, VacuumParams *params) */ if (!onerel) { - int elevel = 0; - - /* - * Determine the log level. - * - * If the RangeVar is not defined, we do not have enough information - * to provide a meaningful log statement. Chances are that - * vacuum_rel's caller has intentionally not provided this information - * so that this logging is skipped, anyway. - * - * Otherwise, for autovacuum logs, we emit a LOG if - * log_autovacuum_min_duration is not disabled. For manual VACUUM, we - * emit a WARNING to match the log statements in the permission - * checks. - */ - if (relation != NULL) - { - if (!IsAutoVacuumWorkerProcess()) - elevel = WARNING; - else if (params->log_min_duration >= 0) - elevel = LOG; - } + int elevel = get_elevel_for_skipped_relation(relation, params); if (elevel != 0) { @@ -1724,3 +1703,35 @@ vacuum_delay_point(void) CHECK_FOR_INTERRUPTS(); } } + +/* + * get_elevel_for_skip_locked + * + * This is a helper function for determining the appropriate logging level for + * messages related to SKIP LOCKED or concurrently dropped relations. + * + * If the RangeVar is not defined, we do not have enough information to provide + * a meaningful log statement, and 0 is returned. Chances are that the caller + * has intentionally not provided this information so that the logging is + * skipped anyway. + * + * Otherwise, for autovacuum logs, we emit a LOG if log_autovacuum_min_duration + * is not disabled. For manual commands, we emit a WARNING to match the log + * statements in the permission checks for VACUUM and ANALYZE. + */ +int +get_elevel_for_skipped_relation(RangeVar *relation, VacuumParams *params) +{ + Assert(params != NULL); + + if (relation == NULL) + return 0; + + if (!IsAutoVacuumWorkerProcess()) + return WARNING; + + if (params->log_min_duration >= 0) + return LOG; + + return 0; +} diff --git a/src/include/commands/vacuum.h b/src/include/commands/vacuum.h index 69b1e384ef..64215ea60a 100644 --- a/src/include/commands/vacuum.h +++ b/src/include/commands/vacuum.h @@ -186,6 +186,7 @@ extern void vacuum_set_xid_limits(Relation rel, MultiXactId *mxactFullScanLimit); extern void vac_update_datfrozenxid(void); extern void vacuum_delay_point(void); +extern int get_elevel_for_skipped_relation(RangeVar *relation, VacuumParams *params); /* in commands/vacuumlazy.c */ extern void lazy_vacuum_rel(Relation onerel, int options, -- 2.16.2
v7-0007-Create-a-helper-function-for-cleaning-up-from-do_.patch (application/octet-stream, 2.1K) download From 6ef01699d703389b69d43a1894b116ec611b8e32 Mon Sep 17 00:00:00 2001 From: Nathan Bossart <bossartn@amazon.com> Date: Thu, 5 Apr 2018 17:37:54 +0000 Subject: [PATCH v7 07/12] Create a helper function for cleaning up from do_analyze_rel(). --- src/backend/commands/analyze.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/backend/commands/analyze.c b/src/backend/commands/analyze.c index d9e31606ff..03e0bad1f3 100644 --- a/src/backend/commands/analyze.c +++ b/src/backend/commands/analyze.c @@ -102,6 +102,9 @@ static void update_attstats(Oid relid, bool inh, int natts, VacAttrStats **vacattrstats); static Datum std_fetch_func(VacAttrStatsP stats, int rownum, bool *isNull); static Datum ind_fetch_func(VacAttrStatsP stats, int rownum, bool *isNull); +static void do_analyze_rel_cleanup(int save_nestlevel, Oid save_userid, + int save_sec_context, MemoryContext caller_context, + RangeVar *rangeVar, VacuumParams *params); /* @@ -705,6 +708,29 @@ do_analyze_rel(Relation onerel, int options, VacuumParams *params, pg_rusage_show(&ru0)))); } + /* pass NULL for the RangeVar to avoid the SKIP LOCKED message */ + do_analyze_rel_cleanup(save_nestlevel, save_userid, save_sec_context, + caller_context, NULL, params); +} + +/* + * Convenience function for cleaning up before exiting do_analyze_rel(). + * + * This also emits a "skipping analyze" log statement based on the rangeVar and + * params provided. If rangeVar is NULL, no such log statement will be emitted. + */ +static void +do_analyze_rel_cleanup(int save_nestlevel, Oid save_userid, int save_sec_context, + MemoryContext caller_context, RangeVar *rangeVar, VacuumParams *params) +{ + int elevel = get_elevel_for_skipped_relation(rangeVar, params); + + if (elevel != 0) + ereport(elevel, + (errcode(ERRCODE_LOCK_NOT_AVAILABLE), + errmsg("skipping analyze of \"%s\" --- lock not available", + rangeVar->relname))); + /* Roll back any GUC changes executed by index functions */ AtEOXact_GUC(false, save_nestlevel); -- 2.16.2
v7-0008-Skip-VACUUM-ANALYZE-with-VACOPT_SKIP_LOCKED-if-in.patch (application/octet-stream, 6.9K) download From 6f29356c60a6f44c4f2eaa56cf1e32b9c4f25524 Mon Sep 17 00:00:00 2001 From: Nathan Bossart <bossartn@amazon.com> Date: Thu, 5 Apr 2018 19:00:40 +0000 Subject: [PATCH v7 08/12] Skip VACUUM/ANALYZE with VACOPT_SKIP_LOCKED if indexes can't be opened. --- src/backend/commands/analyze.c | 29 ++++++++++++++++++++++++----- src/backend/commands/vacuum.c | 23 +++++++++++++++++++---- src/backend/commands/vacuumlazy.c | 23 +++++++++++++++++++++-- src/include/commands/vacuum.h | 2 +- 4 files changed, 65 insertions(+), 12 deletions(-) diff --git a/src/backend/commands/analyze.c b/src/backend/commands/analyze.c index 03e0bad1f3..0421d77ad7 100644 --- a/src/backend/commands/analyze.c +++ b/src/backend/commands/analyze.c @@ -84,7 +84,7 @@ static BufferAccessStrategy vac_strategy; static void do_analyze_rel(Relation onerel, int options, VacuumParams *params, List *va_cols, AcquireSampleRowsFunc acquirefunc, BlockNumber relpages, - bool inh, bool in_outer_xact, int elevel); + bool inh, bool in_outer_xact, int elevel, RangeVar *rangeVar); static void compute_index_stats(Relation onerel, double totalrows, AnlIndexData *indexdata, int nindexes, HeapTuple *rows, int numrows, @@ -294,14 +294,14 @@ analyze_rel(Oid relid, RangeVar *relation, int options, */ if (onerel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE) do_analyze_rel(onerel, options, params, va_cols, acquirefunc, - relpages, false, in_outer_xact, elevel); + relpages, false, in_outer_xact, elevel, relation); /* * If there are child tables, do recursive ANALYZE. */ if (onerel->rd_rel->relhassubclass) do_analyze_rel(onerel, options, params, va_cols, acquirefunc, relpages, - true, in_outer_xact, elevel); + true, in_outer_xact, elevel, relation); /* * Close source relation now, but keep lock so that no one deletes it @@ -331,7 +331,7 @@ static void do_analyze_rel(Relation onerel, int options, VacuumParams *params, List *va_cols, AcquireSampleRowsFunc acquirefunc, BlockNumber relpages, bool inh, bool in_outer_xact, - int elevel) + int elevel, RangeVar *rangeVar) { int attr_cnt, tcnt, @@ -454,7 +454,26 @@ do_analyze_rel(Relation onerel, int options, VacuumParams *params, * all. */ if (!inh) - vac_open_indexes(onerel, AccessShareLock, &nindexes, &Irel, false, NULL); + { + bool skipped = false; + + vac_open_indexes(onerel, AccessShareLock, &nindexes, &Irel, + (options & VACOPT_SKIP_LOCKED), &skipped); + + /* + * If SKIP LOCKED is specified and we would have to wait for a lock on + * an index, skip analyzing the relation. + */ + if (skipped) + { + do_analyze_rel_cleanup(save_nestlevel, save_userid, + save_sec_context, caller_context, + rangeVar, params); + + return; + } + + } else { Irel = NULL; diff --git a/src/backend/commands/vacuum.c b/src/backend/commands/vacuum.c index d7967dda6b..fb7dd8d536 100644 --- a/src/backend/commands/vacuum.c +++ b/src/backend/commands/vacuum.c @@ -1314,6 +1314,7 @@ vacuum_rel(Oid relid, RangeVar *relation, int options, VacuumParams *params) int save_sec_context; int save_nestlevel; bool rel_lock = true; + bool skipped = false; Assert(params != NULL); @@ -1539,7 +1540,18 @@ vacuum_rel(Oid relid, RangeVar *relation, int options, VacuumParams *params) (options & VACOPT_VERBOSE) != 0); } else - lazy_vacuum_rel(onerel, options, params, vac_strategy); + skipped = !lazy_vacuum_rel(onerel, options, params, vac_strategy); + + if (skipped) + { + int elevel = get_elevel_for_skipped_relation(relation, params); + + if (elevel != 0) + ereport(elevel, + (errcode(ERRCODE_LOCK_NOT_AVAILABLE), + errmsg("skipping vacuum of \"%s\" --- lock not available", + relation->relname))); + } /* Roll back any GUC changes executed by index functions */ AtEOXact_GUC(false, save_nestlevel); @@ -1563,8 +1575,11 @@ vacuum_rel(Oid relid, RangeVar *relation, int options, VacuumParams *params) * "analyze" will not get done on the toast table. This is good, because * the toaster always uses hardcoded index access and statistics are * totally unimportant for toast relations. + * + * If we skipped vacuuming the main relation, skip vacuuming the secondary + * toast rel as well. */ - if (toast_relid != InvalidOid) + if (!skipped && toast_relid != InvalidOid) vacuum_rel(toast_relid, NULL, options, params); /* @@ -1572,8 +1587,8 @@ vacuum_rel(Oid relid, RangeVar *relation, int options, VacuumParams *params) */ UnlockRelationIdForSession(&onerelid, lmode); - /* Report that we really did it. */ - return true; + /* Report if we really did it. */ + return !skipped; } diff --git a/src/backend/commands/vacuumlazy.c b/src/backend/commands/vacuumlazy.c index 13e5bd452f..3a584b5115 100644 --- a/src/backend/commands/vacuumlazy.c +++ b/src/backend/commands/vacuumlazy.c @@ -186,8 +186,12 @@ static bool heap_page_is_all_visible(Relation rel, Buffer buf, * * At entry, we have already established a transaction and opened * and locked the relation. + * + * If SKIP LOCKED is specified in the options and lazy_vacuum_rel() + * skips the relation due to a conflicting lock, false is returned. + * Else, true is returned. */ -void +bool lazy_vacuum_rel(Relation onerel, int options, VacuumParams *params, BufferAccessStrategy bstrategy) { @@ -209,6 +213,7 @@ lazy_vacuum_rel(Relation onerel, int options, VacuumParams *params, double new_live_tuples; TransactionId new_frozen_xid; MultiXactId new_min_multi; + bool skipped = false; Assert(params != NULL); @@ -259,7 +264,19 @@ lazy_vacuum_rel(Relation onerel, int options, VacuumParams *params, vacrelstats->lock_waiter_detected = false; /* Open all indexes of the relation */ - vac_open_indexes(onerel, RowExclusiveLock, &nindexes, &Irel, false, NULL); + vac_open_indexes(onerel, RowExclusiveLock, &nindexes, &Irel, + (options & VACOPT_SKIP_LOCKED), &skipped); + + /* + * If SKIP LOCKED is specified and we would have to wait for a lock on an + * index, skip vacuuming the relation. + */ + if (skipped) + { + pgstat_progress_end_command(); + return false; + } + vacrelstats->hasindex = (nindexes > 0); /* Do the vacuuming */ @@ -409,6 +426,8 @@ lazy_vacuum_rel(Relation onerel, int options, VacuumParams *params, pfree(buf.data); } } + + return true; } /* diff --git a/src/include/commands/vacuum.h b/src/include/commands/vacuum.h index 64215ea60a..23fd8392c5 100644 --- a/src/include/commands/vacuum.h +++ b/src/include/commands/vacuum.h @@ -189,7 +189,7 @@ extern void vacuum_delay_point(void); extern int get_elevel_for_skipped_relation(RangeVar *relation, VacuumParams *params); /* in commands/vacuumlazy.c */ -extern void lazy_vacuum_rel(Relation onerel, int options, +extern bool lazy_vacuum_rel(Relation onerel, int options, VacuumParams *params, BufferAccessStrategy bstrategy); /* in commands/analyze.c */ -- 2.16.2
v7-0009-Skip-ANALYZE-with-VACOPT_SKIP_LOCKED-if-acquire_i.patch (application/octet-stream, 4.0K) download From 7ad5ee333bc8fee0c860be2ad537eeb5d5d728ce Mon Sep 17 00:00:00 2001 From: Nathan Bossart <bossartn@amazon.com> Date: Thu, 5 Apr 2018 18:43:45 +0000 Subject: [PATCH v7 09/12] Skip ANALYZE with VACOPT_SKIP_LOCKED if acquire_inherited_sample_rows() blocks. --- src/backend/commands/analyze.c | 54 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 47 insertions(+), 7 deletions(-) diff --git a/src/backend/commands/analyze.c b/src/backend/commands/analyze.c index 0421d77ad7..1997726fd1 100644 --- a/src/backend/commands/analyze.c +++ b/src/backend/commands/analyze.c @@ -97,7 +97,8 @@ static int acquire_sample_rows(Relation onerel, int elevel, static int compare_rows(const void *a, const void *b); static int acquire_inherited_sample_rows(Relation onerel, int elevel, HeapTuple *rows, int targrows, - double *totalrows, double *totaldeadrows); + double *totalrows, double *totaldeadrows, + bool skip_locked, bool *skipped); static void update_attstats(Oid relid, bool inh, int natts, VacAttrStats **vacattrstats); static Datum std_fetch_func(VacAttrStatsP stats, int rownum, bool *isNull); @@ -353,6 +354,7 @@ do_analyze_rel(Relation onerel, int options, VacuumParams *params, Oid save_userid; int save_sec_context; int save_nestlevel; + bool skipped = false; if (inh) ereport(elevel, @@ -550,9 +552,23 @@ do_analyze_rel(Relation onerel, int options, VacuumParams *params, */ rows = (HeapTuple *) palloc(targrows * sizeof(HeapTuple)); if (inh) + { numrows = acquire_inherited_sample_rows(onerel, elevel, rows, targrows, - &totalrows, &totaldeadrows); + &totalrows, &totaldeadrows, + (options & VACOPT_SKIP_LOCKED), &skipped); + + /* + * If SKIP LOCKED is specified and we would have to wait for a lock on + * a child relation, skip analyzing the relation. + */ + if (skipped) + { + do_analyze_rel_cleanup(save_nestlevel, save_userid, save_sec_context, + caller_context, rangeVar, params); + return; + } + } else numrows = (*acquirefunc) (onerel, elevel, rows, targrows, @@ -1337,15 +1353,20 @@ compare_rows(const void *a, const void *b) /* * acquire_inherited_sample_rows -- acquire sample rows from inheritance tree * - * This has the same API as acquire_sample_rows, except that rows are + * This has roughly the same API as acquire_sample_rows, except that rows are * collected from all inheritance children as well as the specified table. - * We fail and return zero if there are no inheritance children, or if all - * children are foreign tables that don't support ANALYZE. + * We fail and return zero if there are no inheritance children, if all + * children are foreign tables that don't support ANALYZE, or if skip_locked + * is true and we cannot obtain a lock on all children without waiting. + * + * If skipped is supplied, it will be set to true if skip_locked takes effect + * (false otherwise). */ static int acquire_inherited_sample_rows(Relation onerel, int elevel, HeapTuple *rows, int targrows, - double *totalrows, double *totaldeadrows) + double *totalrows, double *totaldeadrows, + bool skip_locked, bool *skipped) { List *tableOIDs; Relation *rels; @@ -1363,7 +1384,26 @@ acquire_inherited_sample_rows(Relation onerel, int elevel, * the children. */ tableOIDs = - find_all_inheritors(RelationGetRelid(onerel), AccessShareLock, NULL, false); + find_all_inheritors(RelationGetRelid(onerel), AccessShareLock, NULL, skip_locked); + + /* + * If tableOIDs is NIL, skip_locked took effect. In that case, we should + * fail, being sure to set skipped if it is provided. + */ + if (tableOIDs == NIL) + { + if (skipped != NULL) + *skipped = true; + + return 0; + } + + /* + * All skip_locked-related logic precedes this point, so we can just set + * skipped to false if it is provided. + */ + if (skipped != NULL) + *skipped = false; /* * Check that there's at least one descendant, else fail. This could -- 2.16.2
v7-0010-Skip-VACUUM-ANALYZE-with-VACOPT_SKIP_LOCKED-if-ex.patch (application/octet-stream, 2.6K) download From 2a38290b6ccc36d3d0389d833aed44b7d1ac6d64 Mon Sep 17 00:00:00 2001 From: Nathan Bossart <bossartn@amazon.com> Date: Thu, 5 Apr 2018 18:52:11 +0000 Subject: [PATCH v7 10/12] Skip VACUUM/ANALYZE with VACOPT_SKIP_LOCKED if expand_vacuum_rel() would block. --- src/backend/commands/vacuum.c | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/src/backend/commands/vacuum.c b/src/backend/commands/vacuum.c index fb7dd8d536..f19d112af6 100644 --- a/src/backend/commands/vacuum.c +++ b/src/backend/commands/vacuum.c @@ -68,7 +68,7 @@ static BufferAccessStrategy vac_strategy; /* non-export function prototypes */ -static List *expand_vacuum_rel(VacuumRelation *vrel); +static List *expand_vacuum_rel(VacuumRelation *vrel, int options); static List *get_all_vacuum_rels(void); static void vac_truncate_clog(TransactionId frozenXID, MultiXactId minMulti, @@ -257,7 +257,7 @@ vacuum(int options, List *relations, VacuumParams *params, List *sublist; MemoryContext old_context; - sublist = expand_vacuum_rel(vrel); + sublist = expand_vacuum_rel(vrel, options); old_context = MemoryContextSwitchTo(vac_context); newrels = list_concat(newrels, sublist); MemoryContextSwitchTo(old_context); @@ -423,7 +423,7 @@ vacuum(int options, List *relations, VacuumParams *params, * are made in vac_context. */ static List * -expand_vacuum_rel(VacuumRelation *vrel) +expand_vacuum_rel(VacuumRelation *vrel, int options) { List *vacrels = NIL; MemoryContext oldcontext; @@ -454,7 +454,29 @@ expand_vacuum_rel(VacuumRelation *vrel) * below, as well as find_all_inheritors's expectation that the caller * holds some lock on the starting relation. */ - relid = RangeVarGetRelid(vrel->relation, AccessShareLock, false); + relid = RangeVarGetRelidExtended(vrel->relation, + AccessShareLock, + (options & VACOPT_SKIP_LOCKED) ? RVR_SKIP_LOCKED : 0, + NULL, NULL); + + /* + * If the lock is unavailable, emit the same log statement that + * vacuum_rel() and analyze_rel() would. + */ + if (!OidIsValid(relid)) + { + if (options & VACOPT_VACUUM) + ereport(WARNING, + (errcode(ERRCODE_LOCK_NOT_AVAILABLE), + errmsg("skipping vacuum of \"%s\" --- lock not available", + vrel->relation->relname))); + else + ereport(WARNING, + (errcode(ERRCODE_LOCK_NOT_AVAILABLE), + errmsg("skipping analyze of \"%s\" --- lock not available", + vrel->relation->relname))); + return vacrels; + } /* * Make a returnable VacuumRelation for this rel. -- 2.16.2
v7-0011-Skip-VACUUM-FULL-with-VACOPT_SKIP_LOCKED-if-clust.patch (application/octet-stream, 5.7K) download From 81f01e3cc649bd752944524dddc9382b90d8d0a5 Mon Sep 17 00:00:00 2001 From: Nathan Bossart <bossartn@amazon.com> Date: Thu, 5 Apr 2018 19:04:25 +0000 Subject: [PATCH v7 11/12] Skip VACUUM FULL with VACOPT_SKIP_LOCKED if cluster_rel() would block. --- src/backend/commands/cluster.c | 37 +++++++++++++++++++++++++------------ src/backend/commands/vacuum.c | 5 +++-- src/include/commands/cluster.h | 4 ++-- 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c index d088dc11a6..63486bf34f 100644 --- a/src/backend/commands/cluster.c +++ b/src/backend/commands/cluster.c @@ -186,7 +186,7 @@ cluster(ClusterStmt *stmt, bool isTopLevel) heap_close(rel, NoLock); /* Do the job. */ - cluster_rel(tableOid, indexOid, false, stmt->verbose); + cluster_rel(tableOid, indexOid, false, stmt->verbose, false); } else { @@ -234,7 +234,7 @@ cluster(ClusterStmt *stmt, bool isTopLevel) /* functions in indexes may want a snapshot set */ PushActiveSnapshot(GetTransactionSnapshot()); /* Do the job. */ - cluster_rel(rvtc->tableOid, rvtc->indexOid, true, stmt->verbose); + cluster_rel(rvtc->tableOid, rvtc->indexOid, true, stmt->verbose, false); PopActiveSnapshot(); CommitTransactionCommand(); } @@ -263,9 +263,15 @@ cluster(ClusterStmt *stmt, bool isTopLevel) * If indexOid is InvalidOid, the table will be rewritten in physical order * instead of index order. This is the new implementation of VACUUM FULL, * and error messages should refer to the operation as VACUUM not CLUSTER. + * + * If skip_locked is true and the relation cannot be immediately locked, + * processing will be skipped and false will be returned (otherwise, true is + * returned). Note that skip_locked will not prevent blocking on the index + * provided. Since this functionality is only used for VACUUM FULL, this + * omission has no effect. */ -void -cluster_rel(Oid tableOid, Oid indexOid, bool recheck, bool verbose) +bool +cluster_rel(Oid tableOid, Oid indexOid, bool recheck, bool verbose, bool skip_locked) { Relation OldHeap; @@ -278,11 +284,16 @@ cluster_rel(Oid tableOid, Oid indexOid, bool recheck, bool verbose) * case, since cluster() already did it.) The index lock is taken inside * check_index_is_clusterable. */ - OldHeap = try_relation_open(tableOid, AccessExclusiveLock); + if (!skip_locked) + OldHeap = try_relation_open(tableOid, AccessExclusiveLock); + else if (ConditionalLockRelationOid(tableOid, AccessExclusiveLock)) + OldHeap = try_relation_open(tableOid, NoLock); + else + return false; /* If the table has gone away, we can skip processing it */ if (!OldHeap) - return; + return true; /* * Since we may open a new transaction for each relation, we have to check @@ -301,7 +312,7 @@ cluster_rel(Oid tableOid, Oid indexOid, bool recheck, bool verbose) if (!pg_class_ownercheck(tableOid, GetUserId())) { relation_close(OldHeap, AccessExclusiveLock); - return; + return true; } /* @@ -315,7 +326,7 @@ cluster_rel(Oid tableOid, Oid indexOid, bool recheck, bool verbose) if (RELATION_IS_OTHER_TEMP(OldHeap)) { relation_close(OldHeap, AccessExclusiveLock); - return; + return true; } if (OidIsValid(indexOid)) @@ -326,7 +337,7 @@ cluster_rel(Oid tableOid, Oid indexOid, bool recheck, bool verbose) if (!SearchSysCacheExists1(RELOID, ObjectIdGetDatum(indexOid))) { relation_close(OldHeap, AccessExclusiveLock); - return; + return true; } /* @@ -336,14 +347,14 @@ cluster_rel(Oid tableOid, Oid indexOid, bool recheck, bool verbose) if (!HeapTupleIsValid(tuple)) /* probably can't happen */ { relation_close(OldHeap, AccessExclusiveLock); - return; + return true; } indexForm = (Form_pg_index) GETSTRUCT(tuple); if (!indexForm->indisclustered) { ReleaseSysCache(tuple); relation_close(OldHeap, AccessExclusiveLock); - return; + return true; } ReleaseSysCache(tuple); } @@ -397,7 +408,7 @@ cluster_rel(Oid tableOid, Oid indexOid, bool recheck, bool verbose) !RelationIsPopulated(OldHeap)) { relation_close(OldHeap, AccessExclusiveLock); - return; + return true; } /* @@ -412,6 +423,8 @@ cluster_rel(Oid tableOid, Oid indexOid, bool recheck, bool verbose) rebuild_relation(OldHeap, indexOid, verbose); /* NB: rebuild_relation does heap_close() on OldHeap */ + + return true; } /* diff --git a/src/backend/commands/vacuum.c b/src/backend/commands/vacuum.c index f19d112af6..74349f30d9 100644 --- a/src/backend/commands/vacuum.c +++ b/src/backend/commands/vacuum.c @@ -1558,8 +1558,9 @@ vacuum_rel(Oid relid, RangeVar *relation, int options, VacuumParams *params) onerel = NULL; /* VACUUM FULL is now a variant of CLUSTER; see cluster.c */ - cluster_rel(relid, InvalidOid, false, - (options & VACOPT_VERBOSE) != 0); + skipped = !cluster_rel(relid, InvalidOid, false, + (options & VACOPT_VERBOSE) != 0, + (options & VACOPT_SKIP_LOCKED) != 0); } else skipped = !lazy_vacuum_rel(onerel, options, params, vac_strategy); diff --git a/src/include/commands/cluster.h b/src/include/commands/cluster.h index b338cb1098..49bb35fafe 100644 --- a/src/include/commands/cluster.h +++ b/src/include/commands/cluster.h @@ -19,8 +19,8 @@ extern void cluster(ClusterStmt *stmt, bool isTopLevel); -extern void cluster_rel(Oid tableOid, Oid indexOid, bool recheck, - bool verbose); +extern bool cluster_rel(Oid tableOid, Oid indexOid, bool recheck, + bool verbose, bool skip_locked); extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid, bool recheck, LOCKMODE lockmode); extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal); -- 2.16.2
v7-0012-Open-up-VACOPT_SKIP_LOCKED-to-users.patch (application/octet-stream, 13.0K) download From ce073c5046c659476a250d2879dee5df0f7f78f6 Mon Sep 17 00:00:00 2001 From: Nathan Bossart <bossartn@amazon.com> Date: Wed, 4 Apr 2018 21:53:48 +0000 Subject: [PATCH v7 12/12] Open up VACOPT_SKIP_LOCKED to users. --- doc/src/sgml/ref/analyze.sgml | 12 ++ doc/src/sgml/ref/vacuum.sgml | 12 ++ src/backend/parser/gram.y | 2 + src/include/nodes/parsenodes.h | 2 +- src/test/isolation/expected/vacuum-skip-locked.out | 171 +++++++++++++++++++++ src/test/isolation/isolation_schedule | 1 + src/test/isolation/specs/vacuum-skip-locked.spec | 59 +++++++ src/test/regress/expected/vacuum.out | 10 ++ src/test/regress/sql/vacuum.sql | 10 ++ 9 files changed, 278 insertions(+), 1 deletion(-) create mode 100644 src/test/isolation/expected/vacuum-skip-locked.out create mode 100644 src/test/isolation/specs/vacuum-skip-locked.spec diff --git a/doc/src/sgml/ref/analyze.sgml b/doc/src/sgml/ref/analyze.sgml index 10b3a9a673..4e6ae38a52 100644 --- a/doc/src/sgml/ref/analyze.sgml +++ b/doc/src/sgml/ref/analyze.sgml @@ -27,6 +27,7 @@ ANALYZE [ VERBOSE ] [ <replaceable class="parameter">table_and_columns</replacea <phrase>where <replaceable class="parameter">option</replaceable> can be one of:</phrase> VERBOSE + SKIP LOCKED <phrase>and <replaceable class="parameter">table_and_columns</replaceable> is:</phrase> @@ -76,6 +77,17 @@ ANALYZE [ VERBOSE ] [ <replaceable class="parameter">table_and_columns</replacea </listitem> </varlistentry> + <varlistentry> + <term><literal>SKIP LOCKED</literal></term> + <listitem> + <para> + Specifies that <command>ANALYZE</command> should not wait for any + conflicting locks to be released: if a relation cannot be locked + immediately without waiting, the relation is skipped. + </para> + </listitem> + </varlistentry> + <varlistentry> <term><replaceable class="parameter">table_name</replaceable></term> <listitem> diff --git a/doc/src/sgml/ref/vacuum.sgml b/doc/src/sgml/ref/vacuum.sgml index b760e8ede1..1ef5bc9d4a 100644 --- a/doc/src/sgml/ref/vacuum.sgml +++ b/doc/src/sgml/ref/vacuum.sgml @@ -31,6 +31,7 @@ VACUUM [ FULL ] [ FREEZE ] [ VERBOSE ] [ ANALYZE ] [ <replaceable class="paramet VERBOSE ANALYZE DISABLE_PAGE_SKIPPING + SKIP LOCKED <phrase>and <replaceable class="parameter">table_and_columns</replaceable> is:</phrase> @@ -160,6 +161,17 @@ VACUUM [ FULL ] [ FREEZE ] [ VERBOSE ] [ ANALYZE ] [ <replaceable class="paramet </listitem> </varlistentry> + <varlistentry> + <term><literal>SKIP LOCKED</literal></term> + <listitem> + <para> + Specifies that <command>VACUUM</command> should not wait for any + conflicting locks to be released: if a relation cannot be locked + immediately without waiting, the relation is skipped. + </para> + </listitem> + </varlistentry> + <varlistentry> <term><replaceable class="parameter">table_name</replaceable></term> <listitem> diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y index 1592b58bb4..e170e8762a 100644 --- a/src/backend/parser/gram.y +++ b/src/backend/parser/gram.y @@ -10543,6 +10543,7 @@ vacuum_option_elem: errmsg("unrecognized VACUUM option \"%s\"", $1), parser_errposition(@1))); } + | SKIP LOCKED { $$ = VACOPT_SKIP_LOCKED; } ; AnalyzeStmt: analyze_keyword opt_verbose opt_vacuum_relation_list @@ -10570,6 +10571,7 @@ analyze_option_list: analyze_option_elem: VERBOSE { $$ = VACOPT_VERBOSE; } + | SKIP LOCKED { $$ = VACOPT_SKIP_LOCKED; } ; analyze_keyword: diff --git a/src/include/nodes/parsenodes.h b/src/include/nodes/parsenodes.h index 5340d29f0b..8d9a57215a 100644 --- a/src/include/nodes/parsenodes.h +++ b/src/include/nodes/parsenodes.h @@ -3158,7 +3158,7 @@ typedef enum VacuumOption VACOPT_VERBOSE = 1 << 2, /* print progress info */ VACOPT_FREEZE = 1 << 3, /* FREEZE option */ VACOPT_FULL = 1 << 4, /* FULL (non-concurrent) vacuum */ - VACOPT_SKIP_LOCKED = 1 << 5, /* don't wait to get lock (autovacuum only) */ + VACOPT_SKIP_LOCKED = 1 << 5, /* don't wait to get lock */ VACOPT_SKIPTOAST = 1 << 6, /* don't process the TOAST table, if any */ VACOPT_DISABLE_PAGE_SKIPPING = 1 << 7 /* don't skip any pages */ } VacuumOption; diff --git a/src/test/isolation/expected/vacuum-skip-locked.out b/src/test/isolation/expected/vacuum-skip-locked.out new file mode 100644 index 0000000000..a6fe617db1 --- /dev/null +++ b/src/test/isolation/expected/vacuum-skip-locked.out @@ -0,0 +1,171 @@ +Parsed test spec with 2 sessions + +starting permutation: lock_share vac_specified commit +step lock_share: + BEGIN; + LOCK part1 IN SHARE MODE; + +WARNING: skipping vacuum of "part1" --- lock not available +step vac_specified: VACUUM (SKIP LOCKED) part1, part2; +step commit: + COMMIT; + + +starting permutation: lock_share vac_all_parts commit +step lock_share: + BEGIN; + LOCK part1 IN SHARE MODE; + +step vac_all_parts: VACUUM (SKIP LOCKED) parted; +step commit: + COMMIT; + + +starting permutation: lock_share analyze_specified commit +step lock_share: + BEGIN; + LOCK part1 IN SHARE MODE; + +WARNING: skipping analyze of "part1" --- lock not available +step analyze_specified: ANALYZE (SKIP LOCKED) part1, part2; +step commit: + COMMIT; + + +starting permutation: lock_share analyze_all_parts commit +step lock_share: + BEGIN; + LOCK part1 IN SHARE MODE; + +step analyze_all_parts: ANALYZE (SKIP LOCKED) parted; +step commit: + COMMIT; + + +starting permutation: lock_share vac_analyze_specified commit +step lock_share: + BEGIN; + LOCK part1 IN SHARE MODE; + +WARNING: skipping vacuum of "part1" --- lock not available +step vac_analyze_specified: VACUUM (ANALYZE, SKIP LOCKED) part1, part2; +step commit: + COMMIT; + + +starting permutation: lock_share vac_analyze_all_parts commit +step lock_share: + BEGIN; + LOCK part1 IN SHARE MODE; + +step vac_analyze_all_parts: VACUUM (ANALYZE, SKIP LOCKED) parted; +step commit: + COMMIT; + + +starting permutation: lock_share vac_full_specified commit +step lock_share: + BEGIN; + LOCK part1 IN SHARE MODE; + +WARNING: skipping vacuum of "part1" --- lock not available +step vac_full_specified: VACUUM (SKIP LOCKED, FULL) part1, part2; +step commit: + COMMIT; + + +starting permutation: lock_share vac_full_all_parts commit +step lock_share: + BEGIN; + LOCK part1 IN SHARE MODE; + +step vac_full_all_parts: VACUUM (SKIP LOCKED, FULL) parted; +step commit: + COMMIT; + + +starting permutation: lock_access_exclusive vac_specified commit +step lock_access_exclusive: + BEGIN; + LOCK part1 IN ACCESS EXCLUSIVE MODE; + +WARNING: skipping vacuum of "part1" --- lock not available +step vac_specified: VACUUM (SKIP LOCKED) part1, part2; +step commit: + COMMIT; + + +starting permutation: lock_access_exclusive vac_all_parts commit +step lock_access_exclusive: + BEGIN; + LOCK part1 IN ACCESS EXCLUSIVE MODE; + +step vac_all_parts: VACUUM (SKIP LOCKED) parted; +step commit: + COMMIT; + + +starting permutation: lock_access_exclusive analyze_specified commit +step lock_access_exclusive: + BEGIN; + LOCK part1 IN ACCESS EXCLUSIVE MODE; + +WARNING: skipping analyze of "part1" --- lock not available +step analyze_specified: ANALYZE (SKIP LOCKED) part1, part2; +step commit: + COMMIT; + + +starting permutation: lock_access_exclusive analyze_all_parts commit +step lock_access_exclusive: + BEGIN; + LOCK part1 IN ACCESS EXCLUSIVE MODE; + +WARNING: skipping analyze of "parted" --- lock not available +step analyze_all_parts: ANALYZE (SKIP LOCKED) parted; +step commit: + COMMIT; + + +starting permutation: lock_access_exclusive vac_analyze_specified commit +step lock_access_exclusive: + BEGIN; + LOCK part1 IN ACCESS EXCLUSIVE MODE; + +WARNING: skipping vacuum of "part1" --- lock not available +step vac_analyze_specified: VACUUM (ANALYZE, SKIP LOCKED) part1, part2; +step commit: + COMMIT; + + +starting permutation: lock_access_exclusive vac_analyze_all_parts commit +step lock_access_exclusive: + BEGIN; + LOCK part1 IN ACCESS EXCLUSIVE MODE; + +WARNING: skipping analyze of "parted" --- lock not available +step vac_analyze_all_parts: VACUUM (ANALYZE, SKIP LOCKED) parted; +step commit: + COMMIT; + + +starting permutation: lock_access_exclusive vac_full_specified commit +step lock_access_exclusive: + BEGIN; + LOCK part1 IN ACCESS EXCLUSIVE MODE; + +WARNING: skipping vacuum of "part1" --- lock not available +step vac_full_specified: VACUUM (SKIP LOCKED, FULL) part1, part2; +step commit: + COMMIT; + + +starting permutation: lock_access_exclusive vac_full_all_parts commit +step lock_access_exclusive: + BEGIN; + LOCK part1 IN ACCESS EXCLUSIVE MODE; + +step vac_full_all_parts: VACUUM (SKIP LOCKED, FULL) parted; +step commit: + COMMIT; + diff --git a/src/test/isolation/isolation_schedule b/src/test/isolation/isolation_schedule index 99dd7c6bdb..bbb4bd4186 100644 --- a/src/test/isolation/isolation_schedule +++ b/src/test/isolation/isolation_schedule @@ -72,3 +72,4 @@ test: timeouts test: vacuum-concurrent-drop test: predicate-gist test: predicate-gin +test: vacuum-skip-locked diff --git a/src/test/isolation/specs/vacuum-skip-locked.spec b/src/test/isolation/specs/vacuum-skip-locked.spec new file mode 100644 index 0000000000..3e9e968798 --- /dev/null +++ b/src/test/isolation/specs/vacuum-skip-locked.spec @@ -0,0 +1,59 @@ +# Test for the SKIP LOCKED option for VACUUM and ANALYZE commands. +# +# This also verifies that log messages are not emitted for skipped relations +# that were not specified in the VACUUM or ANALYZE command. + +setup +{ + CREATE TABLE parted (a INT) PARTITION BY LIST (a); + CREATE TABLE part1 PARTITION OF parted FOR VALUES IN (1); + CREATE TABLE part2 PARTITION OF parted FOR VALUES IN (2); +} + +teardown +{ + DROP TABLE IF EXISTS parted; +} + +session "s1" +step "lock_share" +{ + BEGIN; + LOCK part1 IN SHARE MODE; +} +step "lock_access_exclusive" +{ + BEGIN; + LOCK part1 IN ACCESS EXCLUSIVE MODE; +} +step "commit" +{ + COMMIT; +} + +session "s2" +step "vac_specified" { VACUUM (SKIP LOCKED) part1, part2; } +step "vac_all_parts" { VACUUM (SKIP LOCKED) parted; } +step "analyze_specified" { ANALYZE (SKIP LOCKED) part1, part2; } +step "analyze_all_parts" { ANALYZE (SKIP LOCKED) parted; } +step "vac_analyze_specified" { VACUUM (ANALYZE, SKIP LOCKED) part1, part2; } +step "vac_analyze_all_parts" { VACUUM (ANALYZE, SKIP LOCKED) parted; } +step "vac_full_specified" { VACUUM (SKIP LOCKED, FULL) part1, part2; } +step "vac_full_all_parts" { VACUUM (SKIP LOCKED, FULL) parted; } + +permutation "lock_share" "vac_specified" "commit" +permutation "lock_share" "vac_all_parts" "commit" +permutation "lock_share" "analyze_specified" "commit" +permutation "lock_share" "analyze_all_parts" "commit" +permutation "lock_share" "vac_analyze_specified" "commit" +permutation "lock_share" "vac_analyze_all_parts" "commit" +permutation "lock_share" "vac_full_specified" "commit" +permutation "lock_share" "vac_full_all_parts" "commit" +permutation "lock_access_exclusive" "vac_specified" "commit" +permutation "lock_access_exclusive" "vac_all_parts" "commit" +permutation "lock_access_exclusive" "analyze_specified" "commit" +permutation "lock_access_exclusive" "analyze_all_parts" "commit" +permutation "lock_access_exclusive" "vac_analyze_specified" "commit" +permutation "lock_access_exclusive" "vac_analyze_all_parts" "commit" +permutation "lock_access_exclusive" "vac_full_specified" "commit" +permutation "lock_access_exclusive" "vac_full_all_parts" "commit" diff --git a/src/test/regress/expected/vacuum.out b/src/test/regress/expected/vacuum.out index d66e2aa3b7..5d4a1e51c0 100644 --- a/src/test/regress/expected/vacuum.out +++ b/src/test/regress/expected/vacuum.out @@ -119,6 +119,16 @@ ANALYZE (nonexistant-arg) does_not_exist; ERROR: syntax error at or near "nonexistant" LINE 1: ANALYZE (nonexistant-arg) does_not_exist; ^ +-- ensure argument order independence, and that SKIP LOCKED on non-existing +-- relation still errors out. +ANALYZE (SKIP LOCKED, VERBOSE) does_not_exist; +ERROR: relation "does_not_exist" does not exist +ANALYZE (VERBOSE, SKIP LOCKED) does_not_exist; +ERROR: relation "does_not_exist" does not exist +-- SKIP LOCKED option +VACUUM (SKIP LOCKED) vactst; +VACUUM (SKIP LOCKED, FULL) vactst; +ANALYZE (SKIP LOCKED) vactst; DROP TABLE vaccluster; DROP TABLE vactst; DROP TABLE vacparted; diff --git a/src/test/regress/sql/vacuum.sql b/src/test/regress/sql/vacuum.sql index 275ce2e270..15749ffd94 100644 --- a/src/test/regress/sql/vacuum.sql +++ b/src/test/regress/sql/vacuum.sql @@ -93,6 +93,16 @@ ANALYZE vactst (i), vacparted (does_not_exist); ANALYZE (VERBOSE) does_not_exist; ANALYZE (nonexistant-arg) does_not_exist; +-- ensure argument order independence, and that SKIP LOCKED on non-existing +-- relation still errors out. +ANALYZE (SKIP LOCKED, VERBOSE) does_not_exist; +ANALYZE (VERBOSE, SKIP LOCKED) does_not_exist; + +-- SKIP LOCKED option +VACUUM (SKIP LOCKED) vactst; +VACUUM (SKIP LOCKED, FULL) vactst; +ANALYZE (SKIP LOCKED) vactst; + DROP TABLE vaccluster; DROP TABLE vactst; DROP TABLE vacparted; -- 2.16.2

view thread (42+ messages)  latest in thread

Message-ID: <239EE2DC-9FB3-4CB2-92AC-94E035F8BCB1@amazon.com>
Permalink:  ../239EE2DC-9FB3-4CB2-92AC-94E035F8BCB1@amazon.com/
Also on:    postgresql.org/message-id/239EE2DC-9FB3-4CB2-92AC-94E035F8BCB1@amazon.com

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: bossartn@amazon.com, michael@paquier.xyz, andres@anarazel.de, sawada.mshk@gmail.com, robertmhaas@gmail.com, lyes.amd@gmail.com, tomas.vondra@2ndquadrant.com, pgsql-hackers@postgresql.org
  Subject: Re: BUG #14941: Vacuum crashes
  In-Reply-To: <239EE2DC-9FB3-4CB2-92AC-94E035F8BCB1@amazon.com>
  References: <4B232BD7-A97D-4461-81E6-28A49764B60E@amazon.com> <20180110025331.GE336@paquier.xyz> <CBEE19BB-6C70-424C-95E5-F8ABC7AA2185@amazon.com> <20180110231442.GA22983@paquier.xyz> <20180304001252.5sxn3glccnu5qxjm@alap3.anarazel.de> <7327B413-1A57-477F-A6A0-6FD80CB5482D@amazon.com> <20180307050345.GA3095@paquier.xyz> <3834EFB8-2A90-41C7-A7C8-8B956150479D@amazon.com> <4D1115BF-ED38-44E2-B799-C7F9E421A3B8@amazon.com> <20180331001946.uie7elezusxes7qw@alap3.anarazel.de> <20180403051146.GA12320@paquier.xyz> <239EE2DC-9FB3-4CB2-92AC-94E035F8BCB1@amazon.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox