Skip to content

Commit

Permalink
Forward-port our hacked tupconvert.c to PostgreSQL 14
Browse files Browse the repository at this point in the history
  • Loading branch information
df7cb committed Nov 16, 2021
1 parent ee0377d commit 446e2c5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
20 changes: 18 additions & 2 deletions dirtyread_tupconvert.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
/*-------------------------------------------------------------------------
*
* Copy of PostgreSQL 11's tupconvert.c for use by pg_dirtyread. The difference
* is added support for system columns like xmin/xmax/oid. PostgreSQL 14
* refactored it a lot, but the PG 11 version still works, so we stick with it.
*
* tupconvert.c
* Tuple conversion support.
Expand Down Expand Up @@ -145,7 +149,14 @@ dirtyread_convert_tuples_by_name(TupleDesc indesc,
map = (TupleConversionMap *) palloc(sizeof(TupleConversionMap));
map->indesc = indesc;
map->outdesc = outdesc;
#if PG_VERSION_NUM >= 140000
/* TupleConversionMap->attrMap changed in PG14; luckily our old data structure is just a member of that */
map->attrMap = (AttrMap *) palloc(sizeof(AttrMap));
map->attrMap->attnums = attrMap;
map->attrMap->maplen = n;
#else
map->attrMap = attrMap;
#endif
/* preallocate workspace for Datum arrays */
map->outvalues = (Datum *) palloc(n * sizeof(Datum));
map->outisnull = (bool *) palloc(n * sizeof(bool));
Expand Down Expand Up @@ -321,9 +332,14 @@ dirtyread_convert_tuples_by_name_map(TupleDesc indesc,
* Perform conversion of a tuple according to the map.
*/
HeapTuple
dirtyread_do_convert_tuple(HeapTuple tuple, TupleConversionMap *map, TransactionId oldest_xmin)
dirtyread_do_convert_tuple(HeapTuple tuple, TupleConversionMap *map, OldestXminType oldest_xmin)
{
AttrNumber *attrMap = map->attrMap;
AttrNumber *attrMap =
#if PG_VERSION_NUM >= 140000
map->attrMap->attnums;
#else
map->attrMap;
#endif
Datum *invalues = map->invalues;
bool *inisnull = map->inisnull;
Datum *outvalues = map->outvalues;
Expand Down
9 changes: 8 additions & 1 deletion dirtyread_tupconvert.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
#define DIRTYREAD_TUPCONVERT_H

#include "access/tupconvert.h"
#include "utils/snapmgr.h"

#if PG_VERSION_NUM >= 140000
#define OldestXminType GlobalVisState *
#else
#define OldestXminType TransactionId
#endif

extern TupleConversionMap *dirtyread_convert_tuples_by_name(TupleDesc indesc,
TupleDesc outdesc,
Expand All @@ -24,7 +31,7 @@ extern AttrNumber *dirtyread_convert_tuples_by_name_map(TupleDesc indesc,
TupleDesc outdesc,
const char *msg);

extern HeapTuple dirtyread_do_convert_tuple(HeapTuple tuple, TupleConversionMap *map, TransactionId oldest_xmin);
extern HeapTuple dirtyread_do_convert_tuple(HeapTuple tuple, TupleConversionMap *map, OldestXminType oldest_xmin);

#define DeadFakeAttributeNumber FirstLowInvalidHeapAttributeNumber

Expand Down
7 changes: 6 additions & 1 deletion pg_dirtyread.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ typedef struct
#else
HeapScanDesc scan;
#endif
TransactionId oldest_xmin;
OldestXminType oldest_xmin;
} pg_dirtyread_ctx;

PG_MODULE_MAGIC;
Expand Down Expand Up @@ -117,6 +117,10 @@ pg_dirtyread(PG_FUNCTION_ARGS)
, NULL, 0
#endif
);

#if PG_VERSION_NUM >= 140000
usr_ctx->oldest_xmin = GlobalVisTestFor(usr_ctx->rel);
#else
/* only call GetOldestXmin while not in recovery */
if (!RecoveryInProgress())
usr_ctx->oldest_xmin = GetOldestXmin(
Expand All @@ -126,6 +130,7 @@ pg_dirtyread(PG_FUNCTION_ARGS)
false /* allDbs */
#endif
, 0);
#endif
funcctx->user_fctx = (void *) usr_ctx;
MemoryContextSwitchTo(oldcontext);
}
Expand Down

0 comments on commit 446e2c5

Please sign in to comment.