Skip to content

Commit

Permalink
Check for presence of RelationGetSmgr
Browse files Browse the repository at this point in the history
RelationGetSmgr was backported by upstream to the STABLE branches
but is not yet available in any released version so we cannot use
pg version to determine presence of RelationGetSmgr.
  • Loading branch information
svenklemm committed Nov 30, 2022
1 parent 09c0ba7 commit 1a806e2
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 25 deletions.
12 changes: 12 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,18 @@ if(NOT EXISTS ${PG_INCLUDEDIR}/pg_config.h)
)
endif()

# This is currently only available in snapshot branches so we cannot use the
# normal pg version check to determine availability.
if(EXISTS ${PG_INCLUDEDIR_SERVER}/utils/rel.h)
file(READ ${PG_INCLUDEDIR_SERVER}/utils/rel.h PG_UTILS_REL_H)
string(REGEX MATCH "RelationGetSmgr" PG_HAVE_RELATION_GET_SMGR
${PG_UTILS_REL_H})
endif()

if(PG_HAVE_RELATION_GET_SMGR)
add_compile_definitions(PG_HAVE_RELATION_GET_SMGR=1)
endif()

file(READ ${PG_INCLUDEDIR}/pg_config.h PG_CONFIG_H)
string(REGEX MATCH "#define USE_ASSERT_CHECKING 1" PG_USE_ASSERT_CHECKING
${PG_CONFIG_H})
Expand Down
30 changes: 30 additions & 0 deletions src/compat/compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -759,4 +759,34 @@ pg_strtoint64(const char *str)
is_crosspart_update)
#endif

#if PG13_GE
#ifndef PG_HAVE_RELATION_GET_SMGR
#include <storage/smgr.h>
/*
* RelationGetSmgr
* Returns smgr file handle for a relation, opening it if needed.
*
* Very little code is authorized to touch rel->rd_smgr directly. Instead
* use this function to fetch its value.
*
* Note: since a relcache flush can cause the file handle to be closed again,
* it's unwise to hold onto the pointer returned by this function for any
* long period. Recommended practice is to just re-execute RelationGetSmgr
* each time you need to access the SMgrRelation. It's quite cheap in
* comparison to whatever an smgr function is going to do.
*
* This has been backported but is not available in all minor versions so
* we backport ourselves for those versions.
*
*/
static inline SMgrRelation
RelationGetSmgr(Relation rel)
{
if (unlikely(rel->rd_smgr == NULL))
smgrsetowner(&(rel->rd_smgr), smgropen(rel->rd_node, rel->rd_backend));
return rel->rd_smgr;
}
#endif
#endif

#endif /* TIMESCALEDB_COMPAT_H */
25 changes: 0 additions & 25 deletions src/copy.c
Original file line number Diff line number Diff line change
Expand Up @@ -703,31 +703,6 @@ has_other_before_insert_row_trigger_than_ts(ResultRelInfo *resultRelInfo)
return false;
}

#if PG13_GE
/*
* RelationGetSmgr
* Returns smgr file handle for a relation, opening it if needed.
*
* Very little code is authorized to touch rel->rd_smgr directly. Instead
* use this function to fetch its value.
*
* Note: since a relcache flush can cause the file handle to be closed again,
* it's unwise to hold onto the pointer returned by this function for any
* long period. Recommended practice is to just re-execute RelationGetSmgr
* each time you need to access the SMgrRelation. It's quite cheap in
* comparison to whatever an smgr function is going to do.
*
* copied verbatim from postgres because it is a static function
*/
static inline SMgrRelation
RelationGetSmgr(Relation rel)
{
if (unlikely(rel->rd_smgr == NULL))
smgrsetowner(&(rel->rd_smgr), smgropen(rel->rd_node, rel->rd_backend));
return rel->rd_smgr;
}
#endif

/*
* Use COPY FROM to copy data from file to relation.
*/
Expand Down

0 comments on commit 1a806e2

Please sign in to comment.