Skip to content

Commit

Permalink
Avoid segfaults on 1.5-upgraded installations before 10e6c71
Browse files Browse the repository at this point in the history
  • Loading branch information
x4m committed Jun 25, 2020
1 parent afdf2f5 commit 3301383
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/include/pathman.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
*/
#define PATHMAN_CONFIG "pathman_config"
#define Natts_pathman_config 4
#define Natts_pathman_config_historic 5
#define Anum_pathman_config_partrel 1 /* partitioned relation (regclass) */
#define Anum_pathman_config_expr 2 /* partition expression (original) */
#define Anum_pathman_config_parttype 3 /* partitioning type (1|2) */
Expand Down
9 changes: 6 additions & 3 deletions src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,7 @@ pathman_config_contains_relation(Oid relid, Datum *values, bool *isnull,
Snapshot snapshot;
HeapTuple htup;
bool contains_rel = false;
TupleDesc tupleDescr;

ScanKeyInit(&key[0],
Anum_pathman_config_partrel,
Expand All @@ -628,13 +629,15 @@ pathman_config_contains_relation(Oid relid, Datum *values, bool *isnull,

/* Open PATHMAN_CONFIG with latest snapshot available */
rel = heap_open(get_pathman_config_relid(false), AccessShareLock);
tupleDescr = RelationGetDescr(rel);

/* Check that 'partrel' column is of regclass type */
Assert(TupleDescAttr(RelationGetDescr(rel),
Assert(TupleDescAttr(tupleDescr,
Anum_pathman_config_partrel - 1)->atttypid == REGCLASSOID);

/* Check that number of columns == Natts_pathman_config */
Assert(RelationGetDescr(rel)->natts == Natts_pathman_config);
Assert(tupleDescr->natts == Natts_pathman_config
|| tupleDescr->natts == Natts_pathman_config_historic);

snapshot = RegisterSnapshot(GetLatestSnapshot());
scan = heap_beginscan(rel, snapshot, 1, key);
Expand All @@ -647,7 +650,7 @@ pathman_config_contains_relation(Oid relid, Datum *values, bool *isnull,
if (values && isnull)
{
htup = heap_copytuple(htup);
heap_deform_tuple(htup, RelationGetDescr(rel), values, isnull);
heap_deform_tuple(htup, tupleDescr, values, isnull);

/* Perform checks for non-NULL columns */
Assert(!isnull[Anum_pathman_config_partrel - 1]);
Expand Down
8 changes: 4 additions & 4 deletions src/partition_creation.c
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,8 @@ create_partitions_for_value_internal(Oid relid, Datum value, Oid value_type,

PG_TRY();
{
Datum values[Natts_pathman_config];
bool isnull[Natts_pathman_config];
Datum values[Natts_pathman_config_historic];
bool isnull[Natts_pathman_config_historic];

/* Get both PartRelationInfo & PATHMAN_CONFIG contents for this relation */
if (pathman_config_contains_relation(relid, values, isnull, NULL, NULL))
Expand Down Expand Up @@ -1842,8 +1842,8 @@ build_partitioning_expression(Oid parent_relid,
List **columns) /* ret val #2 */
{
/* Values extracted from PATHMAN_CONFIG */
Datum values[Natts_pathman_config];
bool isnull[Natts_pathman_config];
Datum values[Natts_pathman_config_historic];
bool isnull[Natts_pathman_config_historic];
char *expr_cstr;
Node *expr;

Expand Down
12 changes: 10 additions & 2 deletions src/pl_funcs.c
Original file line number Diff line number Diff line change
Expand Up @@ -700,8 +700,8 @@ add_to_pathman_config(PG_FUNCTION_ARGS)
uint32 children_count;

Relation pathman_config;
Datum values[Natts_pathman_config];
bool isnull[Natts_pathman_config];
Datum values[Natts_pathman_config_historic];
bool isnull[Natts_pathman_config_historic];
HeapTuple htup;

Oid expr_type;
Expand Down Expand Up @@ -797,6 +797,14 @@ add_to_pathman_config(PG_FUNCTION_ARGS)
values[Anum_pathman_config_expr - 1] = CStringGetTextDatum(expression);
isnull[Anum_pathman_config_expr - 1] = false;

/*
* In case of 1.5 update before 10e6c71 there is acutlly 5 attributes in
* pathman_config description (inclusing cooked expression). To avoid
* potential problems we allocate 5th attribute and initialize it with null.
*/
values[Natts_pathman_config_historic - 1] = (Datum)0;
isnull[Natts_pathman_config_historic - 1] = true;

/* Insert new row into PATHMAN_CONFIG */
pathman_config = heap_open(get_pathman_config_relid(false), RowExclusiveLock);

Expand Down
4 changes: 2 additions & 2 deletions src/pl_range_funcs.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ create_single_range_partition_pl(PG_FUNCTION_ARGS)
RangeVar *partition_name_rv;
char *tablespace;

Datum values[Natts_pathman_config];
bool isnull[Natts_pathman_config];
Datum values[Natts_pathman_config_historic];
bool isnull[Natts_pathman_config_historic];


/* Handle 'parent_relid' */
Expand Down
4 changes: 2 additions & 2 deletions src/relation_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,8 @@ get_pathman_relation_info(Oid relid)
{
PartRelationInfo *prel = NULL;
ItemPointerData iptr;
Datum values[Natts_pathman_config];
bool isnull[Natts_pathman_config];
Datum values[Natts_pathman_config_historic];
bool isnull[Natts_pathman_config_historic];
bool found;

/*
Expand Down

0 comments on commit 3301383

Please sign in to comment.