1313 *
1414 *
1515 * IDENTIFICATION
16- * $PostgreSQL: pgsql/src/backend/commands/vacuum.c,v 1.291 2004/09/13 20:06:29 tgl Exp $
16+ * $PostgreSQL: pgsql/src/backend/commands/vacuum.c,v 1.292 2004/09/30 23:21:19 tgl Exp $
1717 *
1818 *-------------------------------------------------------------------------
1919 */
@@ -1062,7 +1062,7 @@ full_vacuum_rel(Relation onerel, VacuumStmt *vacstmt)
10621062scan_heap (vacrelstats ,onerel ,& vacuum_pages ,& fraged_pages );
10631063
10641064/* Now open all indexes of the relation */
1065- vac_open_indexes (onerel ,& nindexes ,& Irel );
1065+ vac_open_indexes (onerel ,AccessExclusiveLock , & nindexes ,& Irel );
10661066if (nindexes > 0 )
10671067vacrelstats -> hasindex = true;
10681068
@@ -1088,11 +1088,11 @@ full_vacuum_rel(Relation onerel, VacuumStmt *vacstmt)
10881088/* Try to shrink heap */
10891089repair_frag (vacrelstats ,onerel ,& vacuum_pages ,& fraged_pages ,
10901090nindexes ,Irel );
1091- vac_close_indexes (nindexes ,Irel );
1091+ vac_close_indexes (nindexes ,Irel , NoLock );
10921092}
10931093else
10941094{
1095- vac_close_indexes (nindexes ,Irel );
1095+ vac_close_indexes (nindexes ,Irel , NoLock );
10961096if (vacuum_pages .num_pages > 0 )
10971097{
10981098/* Clean pages from vacuum_pages list */
@@ -3210,8 +3210,14 @@ vac_cmp_vtlinks(const void *left, const void *right)
32103210}
32113211
32123212
3213+ /*
3214+ * Open all the indexes of the given relation, obtaining the specified kind
3215+ * of lock on each. Return an array of Relation pointers for the indexes
3216+ * into *Irel, and the number of indexes into *nindexes.
3217+ */
32133218void
3214- vac_open_indexes (Relation relation ,int * nindexes ,Relation * * Irel )
3219+ vac_open_indexes (Relation relation ,LOCKMODE lockmode ,
3220+ int * nindexes ,Relation * * Irel )
32153221{
32163222List * indexoidlist ;
32173223ListCell * indexoidscan ;
@@ -3230,23 +3236,34 @@ vac_open_indexes(Relation relation, int *nindexes, Relation **Irel)
32303236foreach (indexoidscan ,indexoidlist )
32313237{
32323238Oid indexoid = lfirst_oid (indexoidscan );
3239+ Relation ind ;
32333240
3234- (* Irel )[i ]= index_open (indexoid );
3235- i ++ ;
3241+ ind = index_open (indexoid );
3242+ (* Irel )[i ++ ]= ind ;
3243+ LockRelation (ind ,lockmode );
32363244}
32373245
32383246list_free (indexoidlist );
32393247}
32403248
3241-
3249+ /*
3250+ * Release the resources acquired by vac_open_indexes. Optionally release
3251+ * the locks (say NoLock to keep 'em).
3252+ */
32423253void
3243- vac_close_indexes (int nindexes ,Relation * Irel )
3254+ vac_close_indexes (int nindexes ,Relation * Irel , LOCKMODE lockmode )
32443255{
32453256if (Irel == NULL )
32463257return ;
32473258
32483259while (nindexes -- )
3249- index_close (Irel [nindexes ]);
3260+ {
3261+ Relation ind = Irel [nindexes ];
3262+
3263+ if (lockmode != NoLock )
3264+ UnlockRelation (ind ,lockmode );
3265+ index_close (ind );
3266+ }
32503267pfree (Irel );
32513268}
32523269