|
8 | 8 | *
|
9 | 9 | *
|
10 | 10 | * IDENTIFICATION
|
11 |
| - * $Header: /cvsroot/pgsql/src/backend/commands/vacuum.c,v 1.193 2001/05/18 21:24:18 momjian Exp $ |
| 11 | + * $Header: /cvsroot/pgsql/src/backend/commands/vacuum.c,v 1.194 2001/05/25 15:34:49 momjian Exp $ |
12 | 12 | *
|
13 | 13 | *-------------------------------------------------------------------------
|
14 | 14 | */
|
15 | 15 | #include"postgres.h"
|
16 | 16 |
|
17 | 17 | #include<fcntl.h>
|
18 | 18 | #include<unistd.h>
|
| 19 | +#include<stdlib.h> |
| 20 | +#include<limits.h> |
19 | 21 | #include<time.h>
|
20 | 22 | #include<sys/time.h>
|
21 | 23 | #include<sys/types.h>
|
| 24 | +#include<dirent.h> |
22 | 25 | #include<sys/file.h>
|
23 | 26 | #include<sys/stat.h>
|
24 | 27 |
|
|
30 | 33 |
|
31 | 34 | #include"access/genam.h"
|
32 | 35 | #include"access/heapam.h"
|
| 36 | +#include"access/transam.h" |
33 | 37 | #include"access/xlog.h"
|
34 | 38 | #include"catalog/catalog.h"
|
35 | 39 | #include"catalog/catname.h"
|
@@ -159,6 +163,7 @@ static intvac_cmp_vtlinks(const void *left, const void *right);
|
159 | 163 | staticboolenough_space(VacPagevacpage,Sizelen);
|
160 | 164 | staticvoidinit_rusage(VacRUsage*ru0);
|
161 | 165 | staticchar*show_rusage(VacRUsage*ru0);
|
| 166 | +staticvoidreport_orphans(void); |
162 | 167 |
|
163 | 168 |
|
164 | 169 | /*
|
@@ -236,6 +241,10 @@ vacuum(VacuumStmt *vacstmt)
|
236 | 241 |
|
237 | 242 | /* clean up */
|
238 | 243 | vacuum_shutdown();
|
| 244 | + |
| 245 | +if (VacRelName==NULL) |
| 246 | +report_orphans(); |
| 247 | + |
239 | 248 | }
|
240 | 249 |
|
241 | 250 | /*
|
@@ -2646,3 +2655,74 @@ show_rusage(VacRUsage *ru0)
|
2646 | 2655 |
|
2647 | 2656 | returnresult;
|
2648 | 2657 | }
|
| 2658 | + |
| 2659 | +/* |
| 2660 | + * report_orphans |
| 2661 | + * |
| 2662 | + * Report files that are not referenced by any pg_class.relfilenode. |
| 2663 | + * Could be caused by backend crash no cleaning up. |
| 2664 | + */ |
| 2665 | +staticvoid |
| 2666 | +report_orphans(void) |
| 2667 | +{ |
| 2668 | +DIR*db_dir; |
| 2669 | +structdirent*db_de; |
| 2670 | +Relationrel; |
| 2671 | +TupleDesctupdesc; |
| 2672 | +HeapScanDescscan; |
| 2673 | +HeapTupletuple; |
| 2674 | +Oiddir_file_oid; |
| 2675 | +Oidrel_file_oid; |
| 2676 | +Datumd; |
| 2677 | +booln; |
| 2678 | +boolmatch_found; |
| 2679 | +charcwd[MAXPGPATH]; |
| 2680 | + |
| 2681 | +getcwd(cwd,MAXPGPATH); |
| 2682 | +db_dir=opendir("."); |
| 2683 | +rel=heap_openr(RelationRelationName,AccessShareLock); |
| 2684 | +Assert(db_dir); |
| 2685 | + |
| 2686 | +/* |
| 2687 | + * Cycle through directory and check each file against |
| 2688 | + * pg_class.relfilenode. |
| 2689 | + * XXX This is O(n^2). Is it too slow? bjm |
| 2690 | + */ |
| 2691 | +while ((db_de=readdir(db_dir))!=NULL) |
| 2692 | +{ |
| 2693 | +if (strspn(db_de->d_name,"0123456789")== |
| 2694 | +strlen(db_de->d_name)) |
| 2695 | +{ |
| 2696 | +dir_file_oid= (Oid)strtoul((db_de->d_name),NULL,10); |
| 2697 | + |
| 2698 | +if (dir_file_oid >=GetMinStartupOid()|| |
| 2699 | +dir_file_oid <=BootstrapObjectIdData) |
| 2700 | +continue; |
| 2701 | + |
| 2702 | +tupdesc=RelationGetDescr(rel); |
| 2703 | + |
| 2704 | +match_found= false; |
| 2705 | +scan=heap_beginscan(rel, false,SnapshotNow,0, (ScanKey)NULL); |
| 2706 | +while (HeapTupleIsValid(tuple=heap_getnext(scan,0))) |
| 2707 | +{ |
| 2708 | +d=heap_getattr(tuple,Anum_pg_class_relfilenode,tupdesc,&n); |
| 2709 | +rel_file_oid=DatumGetObjectId(d); |
| 2710 | +if (dir_file_oid==rel_file_oid) |
| 2711 | +{ |
| 2712 | +match_found= true; |
| 2713 | +break; |
| 2714 | +} |
| 2715 | +} |
| 2716 | +heap_endscan(scan); |
| 2717 | +/* make sure there was no oid wrap-around during the scan */ |
| 2718 | +if (!match_found&&dir_file_oid <=ShmemVariableCache->nextOid) |
| 2719 | +elog(NOTICE, |
| 2720 | +"Unreferenced file found in database directory:\n\t%s/%s", |
| 2721 | +cwd,db_de->d_name); |
| 2722 | +/* Maybe one day we can unlink too. bjm 2001-05-24 */ |
| 2723 | +} |
| 2724 | +} |
| 2725 | + |
| 2726 | +heap_close(rel,AccessShareLock); |
| 2727 | +closedir(db_dir); |
| 2728 | +} |