@@ -1501,6 +1501,12 @@ PathNameOpenFilePerm(const char *fileName, int fileFlags, mode_t fileMode)
1501
1501
return -1 ;
1502
1502
}
1503
1503
++ nfile ;
1504
+
1505
+ if (SFS_KEEPING_SNAPSHOT () && (fileFlags & O_EXCL ))
1506
+ {
1507
+ OpenSnapshotFiles (vfdP , ControlFile -> recent_snapshot , true);
1508
+ pg_atomic_write_u32 (& vfdP -> snap_map -> size , SFS_NEW_FILE_MARKER );
1509
+ }
1504
1510
DO_DB (elog (LOG , "PathNameOpenFile: success %d" ,
1505
1511
vfdP -> fd ));
1506
1512
@@ -2039,6 +2045,10 @@ FileRead(File file, char *buffer, int amount, uint32 wait_event_info)
2039
2045
if (!OpenSnapshotFiles (vfdP , snap_id , false))
2040
2046
continue ;
2041
2047
2048
+ if (pg_atomic_read_u32 (& vfdP -> snap_map -> size ) == SFS_NEW_FILE_MARKER )
2049
+ {
2050
+ return 0 ; /* empty file */
2051
+ }
2042
2052
offs = vfdP -> snap_map -> offs [vfdP -> seekPos /BLCKSZ ];
2043
2053
if (offs )
2044
2054
{
@@ -2183,7 +2193,8 @@ FileWrite(File file, char *buffer, int amount, uint32 wait_event_info)
2183
2193
2184
2194
OpenSnapshotFiles (vfdP , ControlFile -> recent_snapshot , true);
2185
2195
2186
- if (!vfdP -> snap_map -> offs [block_no ]) /* This block was not saved yet in this snapshot */
2196
+ if (pg_atomic_read_u32 (& vfdP -> snap_map -> size ) != SFS_NEW_FILE_MARKER
2197
+ && vfdP -> snap_map -> offs [block_no ] == 0 ) /* This block was not saved yet in this snapshot */
2187
2198
{
2188
2199
sfs_segment_offs_t snap_offs = pg_atomic_fetch_add_u32 (& vfdP -> snap_map -> size , 1 )* BLCKSZ ;
2189
2200
char orig_block [BLCKSZ ];
@@ -2373,7 +2384,7 @@ FileSeek(File file, off_t offset, int whence)
2373
2384
if (whence == SEEK_END )
2374
2385
{
2375
2386
SnapshotId snap_id ;
2376
- SnapshotId current_snapshot = sfs_backend_snapshot != SFS_INVALID_SNAPSHOT ? sfs_backend_snapshot : ControlFile -> active_snapshot ;
2387
+ SnapshotId current_snapshot = sfs_backend_snapshot != SFS_INVALID_SNAPSHOT ? sfs_backend_snapshot : ControlFile -> active_snapshot ;
2377
2388
if (current_snapshot != SFS_INVALID_SNAPSHOT )
2378
2389
{
2379
2390
if (current_snapshot < ControlFile -> oldest_snapshot || current_snapshot > ControlFile -> recent_snapshot )
@@ -2389,11 +2400,16 @@ FileSeek(File file, off_t offset, int whence)
2389
2400
if (!OpenSnapshotFiles (vfdP , snap_id , false))
2390
2401
continue ;
2391
2402
2392
- for (i = RELSEG_SIZE ; -- i ! = 0 ;)
2403
+ for (i = RELSEG_SIZE ; -- i > = 0 ;)
2393
2404
{
2394
- sfs_segment_offs_t offs = vfdP -> snap_map -> offs [i ];
2395
- if (offs >= vfdP -> seekPos )
2396
- vfdP -> seekPos = offs + BLCKSZ - 1 ;
2405
+ if (vfdP -> snap_map -> offs [i ] != 0 )
2406
+ {
2407
+ if ((i + 1 )* BLCKSZ - offset >= vfdP -> seekPos )
2408
+ {
2409
+ vfdP -> seekPos = (i + 1 )* BLCKSZ - offset ;
2410
+ }
2411
+ break ;
2412
+ }
2397
2413
}
2398
2414
}
2399
2415
}
@@ -2453,7 +2469,8 @@ FileTruncate(File file, off_t offset, uint32 wait_event_info)
2453
2469
char orig_block [BLCKSZ ];
2454
2470
int rc ;
2455
2471
2456
- if (!vfdP -> snap_map -> offs [block_no ]) /* This block was not saved yet in this snapshot */
2472
+ if (pg_atomic_read_u32 (& vfdP -> snap_map -> size ) != SFS_NEW_FILE_MARKER
2473
+ && !vfdP -> snap_map -> offs [block_no ]) /* This block was not saved yet in this snapshot */
2457
2474
{
2458
2475
sfs_segment_offs_t snap_offs = pg_atomic_fetch_add_u32 (& vfdP -> snap_map -> size , 1 )* BLCKSZ ;
2459
2476
vfdP -> snap_map -> offs [block_no ] = snap_offs + 1 ;
@@ -3887,7 +3904,7 @@ sfs_remove_snapshot_file(const char *fname, bool isdir, int elevel)
3887
3904
&& snap_id <= sfs_current_snapshot )
3888
3905
{
3889
3906
if (unlink (fname ) != 0 )
3890
- elog (elevel , "Failed to remove file %s: %m" , fname );
3907
+ elog (elevel , "Failed to remove snapshot file %s: %m" , fname );
3891
3908
}
3892
3909
}
3893
3910
}
@@ -3907,7 +3924,7 @@ sfs_remove_applied_snapshot_file(const char *fname, bool isdir, int elevel)
3907
3924
&& snap_id >= sfs_current_snapshot )
3908
3925
{
3909
3926
if (unlink (fname ) != 0 )
3910
- elog (elevel , "Failed to remove file %s: %m" , fname );
3927
+ elog (elevel , "Failed to remove applied snapshot file %s: %m" , fname );
3911
3928
}
3912
3929
}
3913
3930
}
@@ -3935,25 +3952,34 @@ sfs_recover_snapshot_file(const char *fname, bool isdir, int elevel)
3935
3952
if (!OpenSnapshotFiles (vfdP , snap_id , false))
3936
3953
elog (ERROR , "[SFS] Failed to open snapshot files" );
3937
3954
3938
- for (i = 0 ; i < RELSEG_SIZE ; i ++ )
3955
+ if (pg_atomic_read_u32 (& vfdP -> snap_map -> size ) == SFS_NEW_FILE_MARKER )
3956
+ {
3957
+ /* This file was created in this snapshot, so just remove it */
3958
+ if (unlink (fname ) != 0 )
3959
+ elog (elevel , "Failed to remove file %s: %m" , fname );
3960
+ }
3961
+ else
3939
3962
{
3940
- sfs_segment_offs_t offs = vfdP -> snap_map -> offs [i ];
3941
- if (offs )
3963
+ for (i = 0 ; i < RELSEG_SIZE ; i ++ )
3942
3964
{
3943
- char orig_block [BLCKSZ ];
3944
- offs -= 1 ;
3965
+ sfs_segment_offs_t offs = vfdP -> snap_map -> offs [i ];
3966
+ if (offs )
3967
+ {
3968
+ char orig_block [BLCKSZ ];
3969
+ offs -= 1 ;
3945
3970
3946
- if (lseek (vfdP -> snap_fd , offs , SEEK_SET ) != offs )
3947
- elog (ERROR , "[SFS] Could not seek file: %m" );
3971
+ if (lseek (vfdP -> snap_fd , offs , SEEK_SET ) != offs )
3972
+ elog (ERROR , "[SFS] Could not seek file: %m" );
3948
3973
3949
- if (sfs_read_file (vfdP -> snap_fd , orig_block , BLCKSZ ) != BLCKSZ )
3950
- elog (ERROR , "[SFS] Could not read file: %m" );
3974
+ if (sfs_read_file (vfdP -> snap_fd , orig_block , BLCKSZ ) != BLCKSZ )
3975
+ elog (ERROR , "[SFS] Could not read file: %m" );
3951
3976
3952
- if (lseek (vfdP -> fd , i * BLCKSZ , SEEK_SET ) != i * BLCKSZ )
3953
- elog (ERROR , "[SFS] Could not seek file: %m" );
3977
+ if (lseek (vfdP -> fd , i * BLCKSZ , SEEK_SET ) != i * BLCKSZ )
3978
+ elog (ERROR , "[SFS] Could not seek file: %m" );
3954
3979
3955
- if (!sfs_write_file (vfdP -> fd , orig_block , BLCKSZ ))
3956
- elog (ERROR , "[SFS] Could not write file: %m" );
3980
+ if (!sfs_write_file (vfdP -> fd , orig_block , BLCKSZ ))
3981
+ elog (ERROR , "[SFS] Could not write file: %m" );
3982
+ }
3957
3983
}
3958
3984
}
3959
3985
0 commit comments