Skip to content

Commit

Permalink
Merge branch 'ce'
Browse files Browse the repository at this point in the history
  • Loading branch information
funny-falcon committed Aug 29, 2023
2 parents 22855f0 + 061898e commit 5d371bc
Show file tree
Hide file tree
Showing 7 changed files with 318 additions and 302 deletions.
93 changes: 7 additions & 86 deletions engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@
#include "catalog/pg_tablespace.h"
#include "miscadmin.h"
#include "port/pg_crc32c.h"
#ifdef PGPRO_EE
/* For file_is_in_cfs_tablespace() only. */
#include "common/cfs_common.h"
#endif
#include "storage/copydir.h"
#if PG_VERSION_NUM >= 120000
#include "storage/md.h"
Expand Down Expand Up @@ -95,53 +91,6 @@ ptrack_write_chunk(int fd, pg_crc32c *crc, char *chunk, size_t size)
}
}

/*
* Determines whether given file path is a path to a cfm file.
*/
bool
is_cfm_file_path(const char *filepath) {
ssize_t len = strlen(filepath);

// For this length checks we assume that the filename is at least
// 1 character longer than the corresponding extension ".cfm":
// strlen(".cfm") == 4 therefore we assume that the filename can't be
// shorter than 5 bytes, for example: "5.cfm".
return strlen(filepath) >= 5 && strcmp(&filepath[len-4], ".cfm") == 0;
}

#if CFS_SUPPORT
/*
* Determines the relation file size specified by fullpath as if it
* was not compressed.
*/
off_t
get_cfs_relation_file_decompressed_size(RelFileNodeBackend rnode, const char *fullpath, ForkNumber forknum) {
File fd;
off_t size;

#if PG_VERSION_NUM >= 120000
int compressor;
compressor = md_get_compressor_internal(nodeOf(rnode), rnode.backend, forknum);
fd = PathNameOpenFile(fullpath, O_RDWR | PG_BINARY, compressor);
#else
fd = PathNameOpenFile(fullpath, O_RDWR | PG_BINARY | PG_COMPRESSION);
#endif

if(fd < 0)
return (off_t)-1;

#if PG_VERSION_NUM >= 120000
size = FileSize(fd);
#else
size = FileSeek(fd, 0, SEEK_END);
#endif

FileClose(fd);

return size;
}
#endif

/*
* Delete ptrack files when ptrack is disabled.
*
Expand Down Expand Up @@ -549,13 +498,8 @@ assign_ptrack_map_size(int newval, void *extra)
* For use in functions that copy directories bypassing buffer manager.
*/
static void
#if CFS_SUPPORT
ptrack_mark_file(Oid dbOid, Oid tablespaceOid,
const char *filepath, const char *filename, bool is_cfs)
#else
ptrack_mark_file(Oid dbOid, Oid tablespaceOid,
const char *filepath, const char *filename)
#endif
{
RelFileNodeBackend rnode;
ForkNumber forknum;
Expand All @@ -564,9 +508,6 @@ ptrack_mark_file(Oid dbOid, Oid tablespaceOid,
struct stat stat_buf;
int oidchars;
char oidbuf[OIDCHARS + 1];
#if CFS_SUPPORT
off_t rel_size;
#endif

/* Do not track temporary relations */
if (looks_like_temp_rel_name(filename))
Expand All @@ -585,21 +526,6 @@ ptrack_mark_file(Oid dbOid, Oid tablespaceOid,
oidbuf[oidchars] = '\0';
nodeRel(nodeOf(rnode)) = atooid(oidbuf);

#if CFS_SUPPORT
// if current tablespace is cfs-compressed and md_get_compressor_internal
// returns the type of the compressing algorithm for filepath, then it
// needs to be de-compressed to obtain its size
if(is_cfs && md_get_compressor_internal(nodeOf(rnode), rnode.backend, forknum) != 0) {
rel_size = get_cfs_relation_file_decompressed_size(rnode, filepath, forknum);

if(rel_size == (off_t)-1) {
elog(WARNING, "ptrack: could not open cfs-compressed relation file: %s", filepath);
return;
}

nblocks = rel_size / BLCKSZ;
} else
#endif
/* Compute number of blocks based on file size */
if (stat(filepath, &stat_buf) == 0)
nblocks = stat_buf.st_size / BLCKSZ;
Expand All @@ -620,9 +546,6 @@ ptrack_walkdir(const char *path, Oid tablespaceOid, Oid dbOid)
{
DIR *dir;
struct dirent *de;
#if CFS_SUPPORT
bool is_cfs;
#endif

/* Do not walk during bootstrap and if ptrack is disabled */
if (ptrack_map_size == 0
Expand All @@ -631,10 +554,6 @@ ptrack_walkdir(const char *path, Oid tablespaceOid, Oid dbOid)
|| InitializingParallelWorker)
return;

#if CFS_SUPPORT
is_cfs = file_is_in_cfs_tablespace(path);
#endif

dir = AllocateDir(path);

while ((de = ReadDirExtended(dir, path, LOG)) != NULL)
Expand Down Expand Up @@ -662,11 +581,7 @@ ptrack_walkdir(const char *path, Oid tablespaceOid, Oid dbOid)
}

if (S_ISREG(fst.st_mode))
#if CFS_SUPPORT
ptrack_mark_file(dbOid, tablespaceOid, subpath, de->d_name, is_cfs);
#else
ptrack_mark_file(dbOid, tablespaceOid, subpath, de->d_name);
#endif
ptrack_mark_file(dbOid, tablespaceOid, subpath, de->d_name);
}

FreeDir(dir); /* we ignore any error here */
Expand Down Expand Up @@ -714,21 +629,27 @@ ptrack_mark_block(RelFileNodeBackend smgr_rnode,
old_init_lsn.value = pg_atomic_read_u64(&ptrack_map->init_lsn);
if (old_init_lsn.value == InvalidXLogRecPtr)
{
#if USE_ASSERT_CHECKING
elog(DEBUG1, "ptrack_mark_block: init_lsn " UINT64_FORMAT " <- " UINT64_FORMAT, old_init_lsn.value, new_lsn);
#endif

while (old_init_lsn.value < new_lsn &&
!pg_atomic_compare_exchange_u64(&ptrack_map->init_lsn, (uint64 *) &old_init_lsn.value, new_lsn));
}

/* Atomically assign new LSN value to the first slot */
old_lsn.value = pg_atomic_read_u64(&ptrack_map->entries[slot1]);
#if USE_ASSERT_CHECKING
elog(DEBUG3, "ptrack_mark_block: map[%zu]=" UINT64_FORMAT " <- " UINT64_FORMAT, slot1, old_lsn.value, new_lsn);
#endif
while (old_lsn.value < new_lsn &&
!pg_atomic_compare_exchange_u64(&ptrack_map->entries[slot1], (uint64 *) &old_lsn.value, new_lsn));

/* And to the second */
old_lsn.value = pg_atomic_read_u64(&ptrack_map->entries[slot2]);
#if USE_ASSERT_CHECKING
elog(DEBUG3, "ptrack_mark_block: map[%zu]=" UINT64_FORMAT " <- " UINT64_FORMAT, slot2, old_lsn.value, new_lsn);
#endif
while (old_lsn.value < new_lsn &&
!pg_atomic_compare_exchange_u64(&ptrack_map->entries[slot2], (uint64 *) &old_lsn.value, new_lsn));
}
11 changes: 0 additions & 11 deletions engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@
#define PTRACK_MAGIC "ptk"
#define PTRACK_MAGIC_SIZE 4

/* CFS support macro */
#if defined(PGPRO_EE) && PG_VERSION_NUM >= 110000
#define CFS_SUPPORT 1
#endif

/*
* Header of ptrack map.
*/
Expand Down Expand Up @@ -116,10 +111,4 @@ extern void ptrack_walkdir(const char *path, Oid tablespaceOid, Oid dbOid);
extern void ptrack_mark_block(RelFileNodeBackend smgr_rnode,
ForkNumber forkno, BlockNumber blkno);

extern bool is_cfm_file_path(const char *path);
#ifdef PGPRO_EE
extern off_t get_cfs_relation_file_decompressed_size(RelFileNodeBackend rnode,
const char *fullpath, ForkNumber forknum);
#endif

#endif /* PTRACK_ENGINE_H */
Loading

0 comments on commit 5d371bc

Please sign in to comment.