Skip to content

Commit 8a4698c

Browse files
committed
WIP refactoring, fix functions close_pathman_relation_info() & drop_range_partition_expand_next()
1 parent d1a20b6 commit 8a4698c

10 files changed

+148
-169
lines changed

src/include/partition_creation.h

-10
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,6 @@ void drop_pathman_check_constraint(Oid relid);
8181
void add_pathman_check_constraint(Oid relid, Constraint *constraint);
8282

8383

84-
/* Update triggers */
85-
void create_single_update_trigger_internal(Oid partition_relid,
86-
const char *trigname,
87-
List *columns);
88-
89-
bool has_update_trigger_internal(Oid parent);
90-
91-
void drop_single_update_trigger_internal(Oid relid,
92-
const char *trigname);
93-
9484
/* Partitioning callback type */
9585
typedef enum
9686
{

src/include/relation_info.h

+10
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#define RELATION_INFO_H
1313

1414

15+
#include "utils.h"
16+
1517
#include "postgres.h"
1618
#include "access/attnum.h"
1719
#include "access/sysattr.h"
@@ -89,6 +91,14 @@ FreeBound(Bound *bound, bool byval)
8991
pfree(DatumGetPointer(BoundGetValue(bound)));
9092
}
9193

94+
static inline char *
95+
BoundToCString(const Bound *bound, Oid value_type)
96+
{
97+
return IsInfinite(bound) ?
98+
pstrdup("NULL") :
99+
datum_to_cstring(bound->value, value_type);
100+
}
101+
92102
static inline int
93103
cmp_bounds(FmgrInfo *cmp_func,
94104
const Oid collid,

src/include/utils.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@
1212
#define PATHMAN_UTILS_H
1313

1414

15-
#include "pathman.h"
16-
1715
#include "postgres.h"
1816
#include "parser/parse_oper.h"
17+
#include "fmgr.h"
1918

2019

2120
/*

src/partition_creation.c

+52-87
Original file line numberDiff line numberDiff line change
@@ -334,16 +334,16 @@ create_partitions_for_value_internal(Oid relid, Datum value, Oid value_type,
334334

335335
PG_TRY();
336336
{
337-
const PartRelationInfo *prel;
338-
LockAcquireResult lock_result; /* could we lock the parent? */
339-
Datum values[Natts_pathman_config];
340-
bool isnull[Natts_pathman_config];
337+
LockAcquireResult lock_result; /* could we lock the parent? */
338+
Datum values[Natts_pathman_config];
339+
bool isnull[Natts_pathman_config];
341340

342341
/* Get both PartRelationInfo & PATHMAN_CONFIG contents for this relation */
343342
if (pathman_config_contains_relation(relid, values, isnull, NULL, NULL))
344343
{
345-
Oid base_bound_type; /* base type of prel->ev_type */
346-
Oid base_value_type; /* base type of value_type */
344+
PartRelationInfo *prel;
345+
Oid base_bound_type; /* base type of prel->ev_type */
346+
Oid base_value_type; /* base type of value_type */
347347

348348
/* Fetch PartRelationInfo by 'relid' */
349349
prel = get_pathman_relation_info_after_lock(relid, true, &lock_result);
@@ -426,6 +426,9 @@ create_partitions_for_value_internal(Oid relid, Datum value, Oid value_type,
426426
value, base_value_type,
427427
prel->ev_collid);
428428
}
429+
430+
/* Don't forget to close 'prel'! */
431+
close_pathman_relation_info(prel);
429432
}
430433
else
431434
elog(ERROR, "table \"%s\" is not partitioned",
@@ -1356,56 +1359,57 @@ check_range_available(Oid parent_relid,
13561359
Oid value_type,
13571360
bool raise_error)
13581361
{
1359-
const PartRelationInfo *prel;
1360-
RangeEntry *ranges;
1361-
FmgrInfo cmp_func;
1362-
uint32 i;
1362+
PartRelationInfo *prel;
1363+
bool result = true;
13631364

13641365
/* Try fetching the PartRelationInfo structure */
1365-
prel = get_pathman_relation_info(parent_relid);
1366-
1367-
/* If there's no prel, return TRUE (overlap is not possible) */
1368-
if (!prel)
1366+
if ((prel = get_pathman_relation_info(parent_relid)) != NULL)
13691367
{
1370-
ereport(WARNING, (errmsg("table \"%s\" is not partitioned",
1371-
get_rel_name_or_relid(parent_relid))));
1372-
return true;
1373-
}
1368+
RangeEntry *ranges;
1369+
FmgrInfo cmp_func;
1370+
uint32 i;
13741371

1375-
/* Emit an error if it is not partitioned by RANGE */
1376-
shout_if_prel_is_invalid(parent_relid, prel, PT_RANGE);
1372+
/* Emit an error if it is not partitioned by RANGE */
1373+
shout_if_prel_is_invalid(parent_relid, prel, PT_RANGE);
13771374

1378-
/* Fetch comparison function */
1379-
fill_type_cmp_fmgr_info(&cmp_func,
1380-
getBaseType(value_type),
1381-
getBaseType(prel->ev_type));
1375+
/* Fetch comparison function */
1376+
fill_type_cmp_fmgr_info(&cmp_func,
1377+
getBaseType(value_type),
1378+
getBaseType(prel->ev_type));
13821379

1383-
ranges = PrelGetRangesArray(prel);
1384-
for (i = 0; i < PrelChildrenCount(prel); i++)
1385-
{
1386-
int c1, c2;
1380+
ranges = PrelGetRangesArray(prel);
1381+
for (i = 0; i < PrelChildrenCount(prel); i++)
1382+
{
1383+
int c1, c2;
13871384

1388-
c1 = cmp_bounds(&cmp_func, prel->ev_collid, start, &ranges[i].max);
1389-
c2 = cmp_bounds(&cmp_func, prel->ev_collid, end, &ranges[i].min);
1385+
c1 = cmp_bounds(&cmp_func, prel->ev_collid, start, &ranges[i].max);
1386+
c2 = cmp_bounds(&cmp_func, prel->ev_collid, end, &ranges[i].min);
13901387

1391-
/* There's something! */
1392-
if (c1 < 0 && c2 > 0)
1393-
{
1394-
if (raise_error)
1395-
elog(ERROR, "specified range [%s, %s) overlaps "
1396-
"with existing partitions",
1397-
IsInfinite(start) ?
1398-
"NULL" :
1399-
datum_to_cstring(BoundGetValue(start), value_type),
1400-
IsInfinite(end) ?
1401-
"NULL" :
1402-
datum_to_cstring(BoundGetValue(end), value_type));
1403-
1404-
else return false;
1388+
/* There's something! */
1389+
if (c1 < 0 && c2 > 0)
1390+
{
1391+
if (raise_error)
1392+
{
1393+
elog(ERROR, "specified range [%s, %s) overlaps "
1394+
"with existing partitions",
1395+
BoundToCString(start, value_type),
1396+
BoundToCString(end, value_type));
1397+
}
1398+
/* Too bad, so sad */
1399+
else result = false;
1400+
}
14051401
}
1402+
1403+
/* Don't forget to close 'prel'! */
1404+
close_pathman_relation_info(prel);
1405+
}
1406+
else
1407+
{
1408+
ereport(WARNING, (errmsg("table \"%s\" is not partitioned",
1409+
get_rel_name_or_relid(parent_relid))));
14061410
}
14071411

1408-
return true;
1412+
return result;
14091413
}
14101414

14111415
/* Build HASH check constraint expression tree */
@@ -1669,15 +1673,15 @@ invoke_init_callback_internal(init_callback_params *cb_params)
16691673
*end_value = NULL;
16701674
Bound sv_datum = cb_params->params.range_params.start_value,
16711675
ev_datum = cb_params->params.range_params.end_value;
1672-
Oid type = cb_params->params.range_params.value_type;
1676+
Oid value_type = cb_params->params.range_params.value_type;
16731677

16741678
/* Convert min to CSTRING */
16751679
if (!IsInfinite(&sv_datum))
1676-
start_value = datum_to_cstring(BoundGetValue(&sv_datum), type);
1680+
start_value = BoundToCString(&sv_datum, value_type);
16771681

16781682
/* Convert max to CSTRING */
16791683
if (!IsInfinite(&ev_datum))
1680-
end_value = datum_to_cstring(BoundGetValue(&ev_datum), type);
1684+
end_value = BoundToCString(&ev_datum, value_type);
16811685

16821686
pushJsonbValue(&jsonb_state, WJB_BEGIN_OBJECT, NULL);
16831687

@@ -1861,42 +1865,3 @@ build_partitioning_expression(Oid parent_relid,
18611865

18621866
return expr;
18631867
}
1864-
1865-
/*
1866-
* -------------------------
1867-
* Update triggers management
1868-
* -------------------------
1869-
*/
1870-
1871-
/* Create trigger for partition */
1872-
void
1873-
create_single_update_trigger_internal(Oid partition_relid,
1874-
const char *trigname,
1875-
List *columns)
1876-
{
1877-
CreateTrigStmt *stmt;
1878-
List *func;
1879-
1880-
func = list_make2(makeString(get_namespace_name(get_pathman_schema())),
1881-
makeString(CppAsString(pathman_update_trigger_func)));
1882-
1883-
stmt = makeNode(CreateTrigStmt);
1884-
stmt->trigname = (char *) trigname;
1885-
stmt->relation = makeRangeVarFromRelid(partition_relid);
1886-
stmt->funcname = func;
1887-
stmt->args = NIL;
1888-
stmt->row = true;
1889-
stmt->timing = TRIGGER_TYPE_BEFORE;
1890-
stmt->events = TRIGGER_TYPE_UPDATE;
1891-
stmt->columns = columns;
1892-
stmt->whenClause = NULL;
1893-
stmt->isconstraint = false;
1894-
stmt->deferrable = false;
1895-
stmt->initdeferred = false;
1896-
stmt->constrrel = NULL;
1897-
1898-
(void) CreateTrigger(stmt, NULL, InvalidOid, InvalidOid,
1899-
InvalidOid, InvalidOid, false);
1900-
1901-
CommandCounterIncrement();
1902-
}

src/pl_funcs.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -437,8 +437,8 @@ show_partition_list_internal(PG_FUNCTION_ARGS)
437437
if (!IsInfinite(&re->min))
438438
{
439439
Datum rmin = CStringGetTextDatum(
440-
datum_to_cstring(BoundGetValue(&re->min),
441-
prel->ev_type));
440+
BoundToCString(&re->min,
441+
prel->ev_type));
442442

443443
values[Anum_pathman_pl_range_min - 1] = rmin;
444444
}
@@ -448,8 +448,8 @@ show_partition_list_internal(PG_FUNCTION_ARGS)
448448
if (!IsInfinite(&re->max))
449449
{
450450
Datum rmax = CStringGetTextDatum(
451-
datum_to_cstring(BoundGetValue(&re->max),
452-
prel->ev_type));
451+
BoundToCString(&re->max,
452+
prel->ev_type));
453453

454454
values[Anum_pathman_pl_range_max - 1] = rmax;
455455
}

src/pl_hash_funcs.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ create_hash_partitions_internal(PG_FUNCTION_ARGS)
5454
RangeVar **rangevars = NULL;
5555

5656
/* Check that there's no partitions yet */
57-
if (get_pathman_relation_info(parent_relid))
57+
if (has_pathman_relation_info(parent_relid))
5858
ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5959
errmsg("cannot add new HASH partitions")));
6060

0 commit comments

Comments
 (0)