Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use RelationGetSmgr instead of rd_smgr #5028

Merged
merged 1 commit into from
Nov 28, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 26 additions & 1 deletion src/copy.c
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,31 @@ 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
Comment on lines +706 to +729
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this code reside in the compat module?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use compat for wrapper functions that abstract API differences between the PG versions. For verbatim imports from PG source we use src/import but since copy.c was the only call site i put it there directly


/*
* Use COPY FROM to copy data from file to relation.
*/
Expand Down Expand Up @@ -1237,7 +1262,7 @@ copyfrom(CopyChunkState *ccstate, List *range_table, Hypertable *ht, MemoryConte
heap_sync(ccstate->rel);
#else
if (!RelationNeedsWAL(ccstate->rel))
smgrimmedsync(ccstate->rel->rd_smgr, MAIN_FORKNUM);
smgrimmedsync(RelationGetSmgr(ccstate->rel), MAIN_FORKNUM);
#endif

return processed;
Expand Down