@@ -61,7 +61,7 @@ static BufferAccessStrategy vac_strategy;
61
61
/* non-export function prototypes */
62
62
static List * get_rel_oids (Oid relid , const RangeVar * vacrel );
63
63
static void vac_truncate_clog (TransactionId frozenXID );
64
- static void vacuum_rel (Oid relid , VacuumStmt * vacstmt , bool do_toast ,
64
+ static bool vacuum_rel (Oid relid , VacuumStmt * vacstmt , bool do_toast ,
65
65
bool for_wraparound , bool * scanned_all );
66
66
67
67
@@ -226,8 +226,11 @@ vacuum(VacuumStmt *vacstmt, Oid relid, bool do_toast,
226
226
bool scanned_all = false;
227
227
228
228
if (vacstmt -> options & VACOPT_VACUUM )
229
- vacuum_rel (relid , vacstmt , do_toast , for_wraparound ,
230
- & scanned_all );
229
+ {
230
+ if (!vacuum_rel (relid , vacstmt , do_toast , for_wraparound ,
231
+ & scanned_all ))
232
+ continue ;
233
+ }
231
234
232
235
if (vacstmt -> options & VACOPT_ANALYZE )
233
236
{
@@ -764,7 +767,7 @@ vac_truncate_clog(TransactionId frozenXID)
764
767
*
765
768
* At entry and exit, we are not inside a transaction.
766
769
*/
767
- static void
770
+ static bool
768
771
vacuum_rel (Oid relid , VacuumStmt * vacstmt , bool do_toast , bool for_wraparound ,
769
772
bool * scanned_all )
770
773
{
@@ -835,14 +838,29 @@ vacuum_rel(Oid relid, VacuumStmt *vacstmt, bool do_toast, bool for_wraparound,
835
838
*
836
839
* There's a race condition here: the rel may have gone away since the
837
840
* last time we saw it. If so, we don't need to vacuum it.
841
+ *
842
+ * If we've been asked not to wait for the relation lock, acquire it
843
+ * first in non-blocking mode, before calling try_relation_open().
838
844
*/
839
- onerel = try_relation_open (relid , lmode );
845
+ if (!(vacstmt -> options & VACOPT_NOWAIT ))
846
+ onerel = try_relation_open (relid , lmode );
847
+ else if (ConditionalLockRelationOid (relid , lmode ))
848
+ onerel = try_relation_open (relid , NoLock );
849
+ else
850
+ {
851
+ onerel = NULL ;
852
+ if (IsAutoVacuumWorkerProcess () && Log_autovacuum_min_duration >= 0 )
853
+ ereport (LOG ,
854
+ (errcode (ERRCODE_LOCK_NOT_AVAILABLE ),
855
+ errmsg ("skipping vacuum of \"%s\" --- lock not available" ,
856
+ vacstmt -> relation -> relname )));
857
+ }
840
858
841
859
if (!onerel )
842
860
{
843
861
PopActiveSnapshot ();
844
862
CommitTransactionCommand ();
845
- return ;
863
+ return false ;
846
864
}
847
865
848
866
/*
@@ -873,7 +891,7 @@ vacuum_rel(Oid relid, VacuumStmt *vacstmt, bool do_toast, bool for_wraparound,
873
891
relation_close (onerel , lmode );
874
892
PopActiveSnapshot ();
875
893
CommitTransactionCommand ();
876
- return ;
894
+ return false ;
877
895
}
878
896
879
897
/*
@@ -890,7 +908,7 @@ vacuum_rel(Oid relid, VacuumStmt *vacstmt, bool do_toast, bool for_wraparound,
890
908
relation_close (onerel , lmode );
891
909
PopActiveSnapshot ();
892
910
CommitTransactionCommand ();
893
- return ;
911
+ return false ;
894
912
}
895
913
896
914
/*
@@ -905,7 +923,7 @@ vacuum_rel(Oid relid, VacuumStmt *vacstmt, bool do_toast, bool for_wraparound,
905
923
relation_close (onerel , lmode );
906
924
PopActiveSnapshot ();
907
925
CommitTransactionCommand ();
908
- return ;
926
+ return false ;
909
927
}
910
928
911
929
/*
@@ -989,6 +1007,9 @@ vacuum_rel(Oid relid, VacuumStmt *vacstmt, bool do_toast, bool for_wraparound,
989
1007
* Now release the session-level lock on the master table.
990
1008
*/
991
1009
UnlockRelationIdForSession (& onerelid , lmode );
1010
+
1011
+ /* Report that we really did it. */
1012
+ return true;
992
1013
}
993
1014
994
1015
0 commit comments