Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ffa33e7

Browse files
committed
WIP refactoring, move functions to reasonable files
1 parent 11b1d5e commit ffa33e7

17 files changed

+529
-564
lines changed

expected/pathman_calamity.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ ERROR: relation "0" does not exist
556556
SELECT add_to_pathman_config('calamity.part_test', NULL); /* no column */
557557
ERROR: 'expression' should not be NULL
558558
SELECT add_to_pathman_config('calamity.part_test', 'V_A_L'); /* wrong column */
559-
ERROR: Cannot find type name for attribute "v_a_l" of relation "part_test"
559+
ERROR: cannot find type name for attribute "v_a_l" of relation "part_test"
560560
SELECT add_to_pathman_config('calamity.part_test', 'val'); /* OK */
561561
add_to_pathman_config
562562
-----------------------

expected/pathman_utility_stmt.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ SELECT * FROM copy_stmt_hooking.test WHERE val > 20;
159159

160160
/* COPY FROM (partitioned column is not specified) */
161161
COPY copy_stmt_hooking.test(comment) FROM stdin;
162-
ERROR: partition expression's value should not be NULL
162+
ERROR: partitioning expression's value should not be NULL
163163
/* COPY FROM (we don't support FREEZE) */
164164
COPY copy_stmt_hooking.test FROM stdin WITH (FREEZE);
165165
ERROR: freeze is not supported for partitioned tables

src/hooks.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -800,8 +800,10 @@ pathman_process_utility_hook(Node *parsetree,
800800
/* Override standard RENAME statement if needed */
801801
else if (is_pathman_related_table_rename(parsetree,
802802
&relation_oid))
803+
{
803804
PathmanRenameConstraint(relation_oid,
804805
(const RenameStmt *) parsetree);
806+
}
805807

806808
/* Override standard ALTER COLUMN TYPE statement if needed */
807809
else if (is_pathman_related_alter_column_type(parsetree,
@@ -810,15 +812,15 @@ pathman_process_utility_hook(Node *parsetree,
810812
&part_type))
811813
{
812814
if (part_type == PT_HASH)
813-
{
814815
ereport(ERROR,
815816
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
816817
errmsg("cannot change type of column \"%s\""
817818
" of table \"%s\" partitioned by HASH",
818819
get_attname(relation_oid, attr_number),
819820
get_rel_name(relation_oid))));
820-
}
821-
mark_pathman_expression_for_update(relation_oid);
821+
822+
/* Don't forget to invalidate parsed partitioning expression */
823+
pathman_config_invalidate_parsed_expression(relation_oid);
822824
}
823825
}
824826

src/include/init.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,15 @@ bool pathman_config_contains_relation(Oid relid,
204204
Datum *values,
205205
bool *isnull,
206206
TransactionId *xmin,
207-
HeapTuple *tuple);
207+
ItemPointerData *iptr);
208+
209+
void pathman_config_invalidate_parsed_expression(Oid relid);
210+
211+
void pathman_config_refresh_parsed_expression(Oid relid,
212+
Datum *values,
213+
bool *isnull,
214+
ItemPointer iptr);
215+
208216

209217
bool read_pathman_params(Oid relid,
210218
Datum *values,

src/include/partition_creation.h

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -78,23 +78,6 @@ Node * build_raw_hash_check_tree(Node *raw_expression,
7878

7979
void drop_check_constraint(Oid relid);
8080

81-
typedef struct
82-
{
83-
Oid expr_type;
84-
Datum expr_datum;
85-
Node *raw_expr;
86-
} PartExpressionInfo;
87-
88-
/* Expression parsing functions */
89-
PartExpressionInfo *get_part_expression_info(Oid relid,
90-
const char *expr_string,
91-
bool check_hash_func,
92-
bool make_plan);
93-
94-
Node *parse_partitioning_expression(Oid relid,
95-
const char *expression,
96-
char **query_string_out,
97-
Node **parsetree_out);
9881

9982
/* Update triggers */
10083
void create_single_update_trigger_internal(Oid partition_relid,

src/include/partition_filter.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,11 @@
2525
#endif
2626

2727

28-
#define ERR_PART_ATTR_NULL "partition expression's value should not be NULL"
29-
#define ERR_PART_ATTR_MULTIPLE_RESULTS \
30-
"partition expression's value should be single, not set"
31-
#define ERR_PART_ATTR_NO_PART "no suitable partition for key '%s'"
32-
#define ERR_PART_ATTR_MULTIPLE "PartitionFilter selected more than one partition"
33-
#define ERR_PART_DESC_CONVERT "could not convert row type for partition"
28+
#define ERR_PART_ATTR_NULL "partitioning expression's value should not be NULL"
29+
#define ERR_PART_ATTR_MULTIPLE_RESULTS "partitioning expression should return single value"
30+
#define ERR_PART_ATTR_NO_PART "no suitable partition for key '%s'"
31+
#define ERR_PART_ATTR_MULTIPLE "PartitionFilter selected more than one partition"
32+
#define ERR_PART_DESC_CONVERT "could not convert row type for partition"
3433

3534

3635
/*

src/include/pathman.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@
4949
#define Anum_pathman_config_expression 2 /* partition expression (original) */
5050
#define Anum_pathman_config_parttype 3 /* partitioning type (1|2) */
5151
#define Anum_pathman_config_range_interval 4 /* interval for RANGE pt. (text) */
52-
#define Anum_pathman_config_expression_p 5 /* parsed partition expression (text) */
53-
#define Anum_pathman_config_atttype 6 /* partitioned atttype */
52+
#define Anum_pathman_config_expression_p 5 /* parsed partitioning expression (text) */
53+
#define Anum_pathman_config_atttype 6 /* partitioned atttype (oid) */
5454

5555
/* type modifier (typmod) for 'range_interval' */
5656
#define PATHMAN_CONFIG_interval_typmod -1

src/include/relation_info.h

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include "postgres.h"
1616
#include "access/attnum.h"
17+
#include "access/sysattr.h"
1718
#include "fmgr.h"
1819
#include "nodes/bitmapset.h"
1920
#include "nodes/nodes.h"
@@ -86,7 +87,10 @@ FreeBound(Bound *bound, bool byval)
8687
}
8788

8889
inline static int
89-
cmp_bounds(FmgrInfo *cmp_func, const Oid collid, const Bound *b1, const Bound *b2)
90+
cmp_bounds(FmgrInfo *cmp_func,
91+
const Oid collid,
92+
const Bound *b1,
93+
const Bound *b2)
9094
{
9195
if (IsMinusInfinity(b1) || IsPlusInfinity(b2))
9296
return -1;
@@ -139,10 +143,10 @@ typedef struct
139143
Oid *children; /* Oids of child partitions */
140144
RangeEntry *ranges; /* per-partition range entry or NULL */
141145

142-
const char *attname; /* original expression */
146+
const char *expr_cstr; /* original expression */
143147
Node *expr; /* planned expression */
144148
List *expr_vars; /* vars from expression, lazy */
145-
Bitmapset *expr_atts; /* set with attnums from expression */
149+
Bitmapset *expr_atts; /* attnums from expression */
146150

147151
Oid atttype; /* expression type */
148152
int32 atttypmod; /* expression type modifier */
@@ -155,6 +159,8 @@ typedef struct
155159
hash_proc; /* hash function for 'atttype' */
156160
} PartRelationInfo;
157161

162+
#define PART_EXPR_VARNO ( 1 )
163+
158164
/*
159165
* PartParentInfo
160166
* Cached parent of the specified partition.
@@ -166,18 +172,6 @@ typedef struct
166172
Oid parent_rel;
167173
} PartParentInfo;
168174

169-
/*
170-
* CustomConst
171-
* Modified Const that also stores 'varattno' attribute from some Var
172-
* We can check that is CustomConst by checking `location` attrubute.
173-
* It should be equal -2
174-
*/
175-
typedef struct
176-
{
177-
Const cns;
178-
AttrNumber varattno;
179-
} CustomConst;
180-
181175
/*
182176
* PartBoundInfo
183177
* Cached bounds of the specified partition.
@@ -242,11 +236,13 @@ static inline List *
242236
PrelExpressionColumnNames(const PartRelationInfo *prel)
243237
{
244238
List *columns = NIL;
245-
int j = -1;
239+
int i = -1;
246240

247-
while ((j = bms_next_member(prel->expr_atts, j)) >= 0)
241+
while ((i = bms_next_member(prel->expr_atts, i)) >= 0)
248242
{
249-
char *attname = get_attname(prel->key, j);
243+
AttrNumber attnum = i + FirstLowInvalidHeapAttributeNumber;
244+
char *attname = get_attname(PrelParentRelid(prel), attnum);
245+
250246
columns = lappend(columns, makeString(attname));
251247
}
252248

@@ -257,15 +253,22 @@ PrelExpressionColumnNames(const PartRelationInfo *prel)
257253
const PartRelationInfo *refresh_pathman_relation_info(Oid relid,
258254
Datum *values,
259255
bool allow_incomplete);
260-
PartRelationInfo * invalidate_pathman_relation_info(Oid relid, bool *found);
256+
PartRelationInfo *invalidate_pathman_relation_info(Oid relid, bool *found);
261257
void remove_pathman_relation_info(Oid relid);
262258
const PartRelationInfo *get_pathman_relation_info(Oid relid);
263259
const PartRelationInfo *get_pathman_relation_info_after_lock(Oid relid,
264260
bool unlock_if_not_found,
265261
LockAcquireResult *lock_result);
266262

267-
/* Expression related routines */
268-
void mark_pathman_expression_for_update(Oid relid);
263+
/* Partitioning expression routines */
264+
Node *parse_partitioning_expression(const Oid relid,
265+
const char *expression,
266+
char **query_string_out,
267+
Node **parsetree_out);
268+
269+
Datum plan_partitioning_expression(const Oid relid,
270+
const char *expr_cstr,
271+
Oid *expr_type);
269272

270273
/* Global invalidation routines */
271274
void delay_pathman_shutdown(void);
@@ -280,12 +283,12 @@ Oid get_parent_of_partition(Oid partition, PartParentSearch *status);
280283

281284
/* Bounds cache */
282285
void forget_bounds_of_partition(Oid partition);
283-
PartBoundInfo * get_bounds_of_partition(Oid partition,
284-
const PartRelationInfo *prel);
286+
PartBoundInfo *get_bounds_of_partition(Oid partition,
287+
const PartRelationInfo *prel);
285288

286289
/* Safe casts for PartType */
287290
PartType DatumGetPartType(Datum datum);
288-
char * PartTypeToCString(PartType parttype);
291+
char *PartTypeToCString(PartType parttype);
289292

290293
/* PartRelationInfo checker */
291294
void shout_if_prel_is_invalid(const Oid parent_oid,
@@ -367,5 +370,6 @@ extern bool pg_pathman_enable_bounds_cache;
367370

368371
void init_relation_info_static_data(void);
369372

373+
370374
#endif /* RELATION_INFO_H */
371375

src/include/utils.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,5 @@ Datum extract_binary_interval_from_text(Datum interval_text,
6565
char ** deconstruct_text_array(Datum array, int *array_size);
6666
RangeVar ** qualified_relnames_to_rangevars(char **relnames, size_t nrelnames);
6767

68-
AttrNumber *get_pathman_attributes_map(const PartRelationInfo *prel,
69-
Relation child);
7068

7169
#endif /* PATHMAN_UTILS_H */

src/init.c

Lines changed: 85 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -599,12 +599,11 @@ build_update_trigger_func_name_internal(Oid relid)
599599

600600
/*
601601
* Check that relation 'relid' is partitioned by pg_pathman.
602-
*
603-
* Extract tuple into 'values' and 'isnull' if they're provided.
602+
* Extract tuple into 'values', 'isnull', 'xmin', 'iptr' if they're provided.
604603
*/
605604
bool
606605
pathman_config_contains_relation(Oid relid, Datum *values, bool *isnull,
607-
TransactionId *xmin, HeapTuple *tuple)
606+
TransactionId *xmin, ItemPointerData* iptr)
608607
{
609608
Relation rel;
610609
HeapScanDesc scan;
@@ -662,8 +661,9 @@ pathman_config_contains_relation(Oid relid, Datum *values, bool *isnull,
662661
*xmin = DatumGetTransactionId(value);
663662
}
664663

665-
if (tuple)
666-
*tuple = heap_copytuple(htup);
664+
/* Set ItemPointer if necessary */
665+
if (iptr)
666+
*iptr = htup->t_self;
667667
}
668668

669669
/* Clean resources */
@@ -677,9 +677,78 @@ pathman_config_contains_relation(Oid relid, Datum *values, bool *isnull,
677677
return contains_rel;
678678
}
679679

680+
/* Invalidate parsed partitioning expression in PATHMAN_CONFIG */
681+
void
682+
pathman_config_invalidate_parsed_expression(Oid relid)
683+
{
684+
ItemPointerData iptr; /* pointer to tuple */
685+
Datum values[Natts_pathman_config];
686+
bool nulls[Natts_pathman_config];
687+
688+
/* Check that PATHMAN_CONFIG table contains this relation */
689+
if (pathman_config_contains_relation(relid, values, nulls, NULL, &iptr))
690+
{
691+
Relation rel;
692+
HeapTuple new_htup;
693+
694+
/* Reset parsed expression */
695+
values[Anum_pathman_config_expression_p - 1] = (Datum) 0;
696+
nulls[Anum_pathman_config_expression_p - 1] = true;
697+
698+
/* Reset expression type */
699+
values[Anum_pathman_config_atttype - 1] = (Datum) 0;
700+
nulls[Anum_pathman_config_atttype - 1] = true;
701+
702+
rel = heap_open(get_pathman_config_relid(false), RowExclusiveLock);
703+
704+
/* Form new tuple and perform an update */
705+
new_htup = heap_form_tuple(RelationGetDescr(rel), values, nulls);
706+
simple_heap_update(rel, &iptr, new_htup);
707+
CatalogUpdateIndexes(rel, new_htup);
708+
709+
heap_close(rel, RowExclusiveLock);
710+
}
711+
}
712+
713+
/* Refresh parsed partitioning expression in PATHMAN_CONFIG */
714+
void
715+
pathman_config_refresh_parsed_expression(Oid relid,
716+
Datum *values,
717+
bool *isnull,
718+
ItemPointer iptr)
719+
{
720+
char *expr_cstr;
721+
Oid expr_type;
722+
Datum expr_datum;
723+
724+
Relation rel;
725+
HeapTuple htup_new;
726+
727+
/* get and parse expression */
728+
expr_cstr = TextDatumGetCString(values[Anum_pathman_config_expression - 1]);
729+
expr_datum = plan_partitioning_expression(relid, expr_cstr, &expr_type);
730+
pfree(expr_cstr);
731+
732+
/* prepare tuple values */
733+
values[Anum_pathman_config_expression_p - 1] = expr_datum;
734+
isnull[Anum_pathman_config_expression_p - 1] = false;
735+
736+
values[Anum_pathman_config_atttype - 1] = ObjectIdGetDatum(expr_type);
737+
isnull[Anum_pathman_config_atttype - 1] = false;
738+
739+
rel = heap_open(get_pathman_config_relid(false), RowExclusiveLock);
740+
741+
htup_new = heap_form_tuple(RelationGetDescr(rel), values, isnull);
742+
simple_heap_update(rel, iptr, htup_new);
743+
CatalogUpdateIndexes(rel, htup_new);
744+
745+
heap_close(rel, RowExclusiveLock);
746+
}
747+
748+
680749
/*
681-
* Loads additional pathman parameters like 'enable_parent' or 'auto'
682-
* from PATHMAN_CONFIG_PARAMS.
750+
* Loads additional pathman parameters like 'enable_parent'
751+
* or 'auto' from PATHMAN_CONFIG_PARAMS.
683752
*/
684753
bool
685754
read_pathman_params(Oid relid, Datum *values, bool *isnull)
@@ -722,6 +791,7 @@ read_pathman_params(Oid relid, Datum *values, bool *isnull)
722791
return row_found;
723792
}
724793

794+
725795
/*
726796
* Go through the PATHMAN_CONFIG table and create PartRelationInfo entries.
727797
*/
@@ -788,10 +858,9 @@ read_pathman_config(void)
788858

789859
/*
790860
* Validates range constraint. It MUST have one of the following formats:
791-
*
792-
* EXPRESSION >= CONST AND EXPRESSION < CONST
793-
* EXPRESSION >= CONST
794-
* EXPRESSION < CONST
861+
* 1) EXPRESSION >= CONST AND EXPRESSION < CONST
862+
* 2) EXPRESSION >= CONST
863+
* 3) EXPRESSION < CONST
795864
*
796865
* Writes 'lower' & 'upper' and 'lower_null' & 'upper_null' values on success.
797866
*/
@@ -838,7 +907,11 @@ validate_range_constraint(const Expr *expr,
838907
lower, upper, lower_null, upper_null);
839908
}
840909

841-
/* Validates a single expression of kind EXPRESSION >= CONST | EXPRESSION < CONST */
910+
/*
911+
* Validates a single expression of kind:
912+
* 1) EXPRESSION >= CONST
913+
* 2) EXPRESSION < CONST
914+
*/
842915
static bool
843916
validate_range_opexpr(const Expr *expr,
844917
const PartRelationInfo *prel,

0 commit comments

Comments
 (0)