@@ -209,7 +209,7 @@ typedef struct
209
209
bool compressed ;
210
210
}TablespaceStatus ;
211
211
212
- static bool md_use_compression (SMgrRelation reln ,ForkNumber forknum )
212
+ static bool md_use_compression (RelFileNodeBackend rnode ,ForkNumber forknum )
213
213
{
214
214
static HTAB * tblspaceMap ;
215
215
char * compressionFilePath ;
@@ -219,9 +219,9 @@ static bool md_use_compression(SMgrRelation reln, ForkNumber forknum)
219
219
220
220
/* Do not compress system (catalog) relations created during bootstrap */
221
221
if (forknum != MAIN_FORKNUM
222
- || reln -> smgr_rnode .node .spcNode == DEFAULTTABLESPACE_OID
223
- || reln -> smgr_rnode .node .spcNode == GLOBALTABLESPACE_OID
224
- || reln -> smgr_rnode .node .relNode < FirstNormalObjectId )
222
+ || rnode .node .spcNode == DEFAULTTABLESPACE_OID
223
+ || rnode .node .spcNode == GLOBALTABLESPACE_OID
224
+ || rnode .node .relNode < FirstNormalObjectId )
225
225
{
226
226
return false;
227
227
}
@@ -231,10 +231,10 @@ static bool md_use_compression(SMgrRelation reln, ForkNumber forknum)
231
231
ctl .entrysize = sizeof (TablespaceStatus );
232
232
tblspaceMap = hash_create ("tablespace_map" ,256 ,& ctl ,HASH_ELEM );
233
233
}
234
- ts = hash_search (tblspaceMap ,& reln -> smgr_rnode .node .spcNode ,HASH_ENTER ,& found );
234
+ ts = hash_search (tblspaceMap ,& rnode .node .spcNode ,HASH_ENTER ,& found );
235
235
if (!found ) {
236
236
compressionFilePath = psprintf ("pg_tblspc/%u/%s/pg_compression" ,
237
- reln -> smgr_rnode .node .spcNode ,
237
+ rnode .node .spcNode ,
238
238
TABLESPACE_VERSION_DIRECTORY );
239
239
compressionFile = fopen (compressionFilePath ,"r" );
240
240
if (compressionFile != NULL ) {
@@ -357,7 +357,7 @@ mdcreate(SMgrRelation reln, ForkNumber forkNum, bool isRedo)
357
357
if (isRedo && reln -> md_fd [forkNum ]!= NULL )
358
358
return ;/* created and opened already... */
359
359
360
- if (md_use_compression (reln ,forkNum ))
360
+ if (md_use_compression (reln -> smgr_rnode ,forkNum ))
361
361
{
362
362
flags |=PG_COMPRESSION ;
363
363
}
@@ -473,9 +473,11 @@ static void
473
473
mdunlinkfork (RelFileNodeBackend rnode ,ForkNumber forkNum ,bool isRedo )
474
474
{
475
475
char * path ;
476
+ char * segpath ;
476
477
int ret ;
477
478
478
479
path = relpath (rnode ,forkNum );
480
+ segpath = (char * )palloc (strlen (path )+ 16 );
479
481
480
482
/*
481
483
* Delete or truncate the first segment.
@@ -484,32 +486,54 @@ mdunlinkfork(RelFileNodeBackend rnode, ForkNumber forkNum, bool isRedo)
484
486
{
485
487
ret = unlink (path );
486
488
if (ret < 0 && errno != ENOENT )
489
+ {
487
490
ereport (WARNING ,
488
491
(errcode_for_file_access (),
489
492
errmsg ("could not remove file \"%s\": %m" ,path )));
493
+ }
494
+ else if (forkNum == MAIN_FORKNUM )
495
+ {
496
+ sprintf (segpath ,"%s.cfm" ,path );
497
+ unlink (segpath );
498
+ }
490
499
}
491
500
else
492
501
{
493
- /* truncate(2) would be easier here, but Windows hasn't got it */
494
- int fd ;
495
-
496
- fd = OpenTransientFile (path ,O_RDWR |PG_BINARY ,0 );
497
- if (fd >=0 )
502
+ if (md_use_compression (rnode ,forkNum ))
498
503
{
499
- int save_errno ;
500
-
501
- ret = ftruncate (fd ,0 );
502
- save_errno = errno ;
503
- CloseTransientFile (fd );
504
- errno = save_errno ;
504
+ File file = PathNameOpenFile (path ,O_RDWR |PG_BINARY |PG_COMPRESSION ,0 );
505
+ if (file >=0 ) {
506
+ elog (LOG ,"Truncate file %s" ,path );
507
+ if (FileTruncate (file ,0 )< 0 ) {
508
+ ereport (WARNING ,
509
+ (errcode_for_file_access (),
510
+ errmsg ("could not truncate file \"%s\": %m" ,path )));
511
+ }
512
+ }
513
+ FileClose (file );
514
+ }
515
+ else
516
+ {
517
+ /* truncate(2) would be easier here, but Windows hasn't got it */
518
+ int fd ;
519
+
520
+ fd = OpenTransientFile (path ,O_RDWR |PG_BINARY ,0 );
521
+ if (fd >=0 )
522
+ {
523
+ int save_errno ;
524
+
525
+ ret = ftruncate (fd ,0 );
526
+ save_errno = errno ;
527
+ CloseTransientFile (fd );
528
+ errno = save_errno ;
529
+ }
530
+ else
531
+ ret = -1 ;
532
+ if (ret < 0 && errno != ENOENT )
533
+ ereport (WARNING ,
534
+ (errcode_for_file_access (),
535
+ errmsg ("could not truncate file \"%s\": %m" ,path )));
505
536
}
506
- else
507
- ret = -1 ;
508
- if (ret < 0 && errno != ENOENT )
509
- ereport (WARNING ,
510
- (errcode_for_file_access (),
511
- errmsg ("could not truncate file \"%s\": %m" ,path )));
512
-
513
537
/* Register request to unlink first segment later */
514
538
register_unlink (rnode );
515
539
}
@@ -519,7 +543,6 @@ mdunlinkfork(RelFileNodeBackend rnode, ForkNumber forkNum, bool isRedo)
519
543
*/
520
544
if (ret >=0 )
521
545
{
522
- char * segpath = (char * )palloc (strlen (path )+ 16 );
523
546
BlockNumber segno ;
524
547
525
548
/*
@@ -550,8 +573,8 @@ mdunlinkfork(RelFileNodeBackend rnode, ForkNumber forkNum, bool isRedo)
550
573
sprintf (segpath ,"%s.cfm" ,path );
551
574
unlink (segpath );
552
575
}
553
- pfree (segpath );
554
576
}
577
+ pfree (segpath );
555
578
pfree (path );
556
579
}
557
580
@@ -657,7 +680,7 @@ mdopen(SMgrRelation reln, ForkNumber forknum, int behavior)
657
680
658
681
path = relpath (reln -> smgr_rnode ,forknum );
659
682
660
- if (md_use_compression (reln ,forknum ))
683
+ if (md_use_compression (reln -> smgr_rnode ,forknum ))
661
684
{
662
685
flags |=PG_COMPRESSION ;
663
686
}
@@ -1824,7 +1847,7 @@ _mdfd_openseg(SMgrRelation reln, ForkNumber forknum, BlockNumber segno,
1824
1847
int fd ;
1825
1848
char * fullpath ;
1826
1849
1827
- if (md_use_compression (reln ,forknum ))
1850
+ if (md_use_compression (reln -> smgr_rnode ,forknum ))
1828
1851
{
1829
1852
oflags |=PG_COMPRESSION ;
1830
1853
}