Skip to content

Commit ff97121

Browse files
committed
Start modifying range functions
1 parent 1ccf5b9 commit ff97121

File tree

6 files changed

+26
-130
lines changed

6 files changed

+26
-130
lines changed

init.sql

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
* text to Datum
1616
*/
1717
CREATE OR REPLACE FUNCTION @extschema@.validate_interval_value(
18-
partrel REGCLASS,
19-
attname TEXT,
18+
atttype OID,
2019
parttype INTEGER,
2120
range_interval TEXT)
2221
RETURNS BOOL AS 'pg_pathman', 'validate_interval_value'
@@ -44,11 +43,10 @@ CREATE TABLE IF NOT EXISTS @extschema@.pathman_config (
4443
CHECK (parttype IN (1, 2))
4544

4645
/* check for correct interval */
47-
/*
4846
CHECK (@extschema@.validate_interval_value(partrel,
49-
attname,
47+
atttype,
5048
parttype,
51-
range_interval)) */
49+
range_interval))
5250
);
5351

5452

@@ -786,26 +784,6 @@ CREATE OR REPLACE FUNCTION @extschema@.validate_relname(
786784
RETURNS VOID AS 'pg_pathman', 'validate_relname'
787785
LANGUAGE C;
788786

789-
/*
790-
* Checks if attribute is nullable
791-
*/
792-
CREATE OR REPLACE FUNCTION @extschema@.is_attribute_nullable(
793-
relid REGCLASS,
794-
attname TEXT)
795-
RETURNS BOOLEAN AS 'pg_pathman', 'is_attribute_nullable'
796-
LANGUAGE C STRICT;
797-
798-
/*
799-
* Checks if expression is suitable
800-
*/
801-
/*
802-
CREATE OR REPLACE FUNCTION @extschema@.is_expression_suitable(
803-
relid REGCLASS,
804-
expr TEXT)
805-
RETURNS BOOLEAN AS 'pg_pathman', 'is_expression_suitable'
806-
LANGUAGE C STRICT;
807-
*/
808-
809787
/*
810788
* Check if regclass is date or timestamp.
811789
*/

range.sql

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ LANGUAGE plpgsql;
2626
*/
2727
CREATE OR REPLACE FUNCTION @extschema@.check_boundaries(
2828
parent_relid REGCLASS,
29-
attribute TEXT,
29+
expression TEXT,
3030
start_value ANYELEMENT,
3131
end_value ANYELEMENT)
3232
RETURNS VOID AS
@@ -40,32 +40,32 @@ BEGIN
4040
/* Get min and max values */
4141
EXECUTE format('SELECT count(*), min(%1$s), max(%1$s)
4242
FROM %2$s WHERE NOT %1$s IS NULL',
43-
attribute, parent_relid::TEXT)
43+
expression, parent_relid::TEXT)
4444
INTO v_count, v_min, v_max;
4545

4646
/* Check if column has NULL values */
4747
IF v_count > 0 AND (v_min IS NULL OR v_max IS NULL) THEN
48-
RAISE EXCEPTION 'column "%" contains NULL values', attribute;
48+
RAISE EXCEPTION 'expression "%" returns NULL values', expression;
4949
END IF;
5050

5151
/* Check lower boundary */
5252
IF start_value > v_min THEN
53-
RAISE EXCEPTION 'start value is less than min value of "%"', attribute;
53+
RAISE EXCEPTION 'start value is less than min value of "%"', expression;
5454
END IF;
5555

5656
/* Check upper boundary */
5757
IF end_value <= v_max THEN
58-
RAISE EXCEPTION 'not enough partitions to fit all values of "%"', attribute;
58+
RAISE EXCEPTION 'not enough partitions to fit all values of "%"', expression;
5959
END IF;
6060
END
6161
$$ LANGUAGE plpgsql;
6262

6363
/*
64-
* Creates RANGE partitions for specified relation based on datetime attribute
64+
* Creates RANGE partitions for specified relation based on datetime expression
6565
*/
6666
CREATE OR REPLACE FUNCTION @extschema@.create_range_partitions(
6767
parent_relid REGCLASS,
68-
attribute TEXT,
68+
expression TEXT,
6969
start_value ANYELEMENT,
7070
p_interval INTERVAL,
7171
p_count INTEGER DEFAULT NULL,
@@ -91,16 +91,16 @@ BEGIN
9191
PERFORM @extschema@.lock_partitioned_relation(parent_relid);
9292
END IF;
9393

94-
attribute := lower(attribute);
95-
PERFORM @extschema@.common_relation_checks(parent_relid, attribute);
94+
expression := lower(expression);
95+
PERFORM @extschema@.common_relation_checks(parent_relid, expression);
9696

9797
IF p_count < 0 THEN
9898
RAISE EXCEPTION '"p_count" must not be less than 0';
9999
END IF;
100100

101101
/* Try to determine partitions count if not set */
102102
IF p_count IS NULL THEN
103-
EXECUTE format('SELECT count(*), max(%s) FROM %s', attribute, parent_relid)
103+
EXECUTE format('SELECT count(*), max(%s) FROM %s', expression, parent_relid)
104104
INTO v_rows_count, v_max;
105105

106106
IF v_rows_count = 0 THEN
@@ -253,6 +253,9 @@ BEGIN
253253
end_value);
254254
END IF;
255255

256+
/* Insert new entry to pathman config */
257+
PERFORM @extschema@.add_to_pathman_config(parent_relid, expression, NULL, false);
258+
256259
/* Insert new entry to pathman config */
257260
INSERT INTO @extschema@.pathman_config (partrel, attname, parttype, range_interval)
258261
VALUES (parent_relid, attribute, 2, p_interval::TEXT);

src/include/relation_info.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ typedef struct
128128
RangeEntry *ranges; /* per-partition range entry or NULL */
129129

130130
Node *expr; /* planned expression */
131+
char *expr_string; /* string with original expression */
131132
PartType parttype; /* partitioning type (HASH | RANGE) */
132133
Oid atttype; /* expression type */
133134
int32 atttypmod; /* expression type modifier */

src/pl_funcs.c

Lines changed: 2 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -372,20 +372,11 @@ show_partition_list_internal(PG_FUNCTION_ARGS)
372372

373373
continue;
374374
}
375-
376-
// FIX this
377-
//partattr_cstr = get_attname(PrelParentRelid(prel), prel->attnum);
378-
//if (!partattr_cstr)
379-
//{
380-
/* Parent does not exist, go to the next 'prel' */
381-
// usercxt->current_prel = NULL;
382-
// continue;
383-
//}
384375

385376
/* Fill in common values */
386377
values[Anum_pathman_pl_parent - 1] = PrelParentRelid(prel);
387378
values[Anum_pathman_pl_parttype - 1] = prel->parttype;
388-
values[Anum_pathman_pl_partattr - 1] = CStringGetTextDatum(partattr_cstr);
379+
values[Anum_pathman_pl_partattr - 1] = prel->expr_string;
389380

390381
switch (prel->parttype)
391382
{
@@ -489,74 +480,6 @@ is_date_type(PG_FUNCTION_ARGS)
489480
PG_RETURN_BOOL(is_date_type_internal(PG_GETARG_OID(0)));
490481
}
491482

492-
/*
493-
Datum
494-
is_expression_suitable(PG_FUNCTION_ARGS)
495-
{
496-
Oid relid = PG_GETARG_OID(0);
497-
char *expr = text_to_cstring(PG_GETARG_TEXT_P(1));
498-
bool result;
499-
500-
TypeCacheEntry *tce;
501-
Oid type_oid = get_partition_expr_type(relid, expr);
502-
503-
tce = lookup_type_cache(type_oid, TYPECACHE_HASH_PROC);
504-
result = (tce->hash_proc != InvalidOid);
505-
506-
PG_RETURN_BOOL(result);
507-
}*/
508-
509-
Datum
510-
is_attribute_nullable(PG_FUNCTION_ARGS)
511-
{
512-
/*
513-
Oid relid = PG_GETARG_OID(0);
514-
char *relname = get_rel_name(relid),
515-
*namespace_name = get_namespace_name(get_rel_namespace(relid));
516-
char *expr = text_to_cstring(PG_GETARG_TEXT_P(1));
517-
char *fmt = "SELECT (%s) FROM %s.%s";
518-
bool result = true;
519-
HeapTuple tp;
520-
List *parsetree_list,
521-
*querytree_list,
522-
*plantree_list;
523-
EState *estate;
524-
ExprContext *econtext;
525-
Node *parsetree,
526-
*target_entry;
527-
Query *query;
528-
PlannedStmt *plan;
529-
MemoryContext oldcontext;
530-
SeqScanState *scanstate;
531-
Oid expr_type;
532-
533-
int n = snprintf(NULL, 0, fmt, expr, namespace_name, relname);
534-
char *query_string = (char *) palloc(n + 1);
535-
snprintf(query_string, n + 1, fmt, expr, namespace_name, relname);
536-
537-
parsetree_list = raw_parser(query_string);
538-
539-
Assert(list_length(parsetree_list) == 1);
540-
parsetree = (Node *)(lfirst(list_head(parsetree_list)));
541-
542-
query = parse_analyze(parsetree, query_string, NULL, 0);
543-
plan = pg_plan_query(query, 0, NULL);
544-
545-
target_entry = lfirst(list_head(plan->planTree->targetlist));
546-
expr_type = get_call_expr_argtype(((TargetEntry *)target_entry)->expr, 0);
547-
548-
estate = CreateExecutorState();
549-
550-
Assert(nodeTag(plan->planTree) == T_SeqScan);
551-
scanstate = ExecInitSeqScan(plan->planTree, estate, 0);
552-
553-
pfree(query_string);
554-
*/
555-
bool result = true;
556-
PG_RETURN_BOOL(result); /* keep compiler happy */
557-
}
558-
559-
560483
/*
561484
* ------------------------
562485
* Useful string builders
@@ -670,7 +593,7 @@ add_to_pathman_config(PG_FUNCTION_ARGS)
670593
elog(ERROR, "'parent_relid' should not be NULL");
671594

672595
if (PG_ARGISNULL(1))
673-
elog(ERROR, "'attname' should not be NULL");
596+
elog(ERROR, "'expression' should not be NULL");
674597

675598
/* Read parameters */
676599
relid = PG_GETARG_OID(0);

src/pl_range_funcs.c

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -637,38 +637,27 @@ drop_range_partition_expand_next(PG_FUNCTION_ARGS)
637637
Datum
638638
validate_interval_value(PG_FUNCTION_ARGS)
639639
{
640-
Oid partrel = PG_GETARG_OID(0);
641-
text *attname = PG_GETARG_TEXT_P(1);
642-
PartType parttype = DatumGetPartType(PG_GETARG_DATUM(2));
643-
Datum interval_text = PG_GETARG_DATUM(3);
640+
Oid atttype = PG_GETARG_OID(0);
641+
PartType parttype = DatumGetPartType(PG_GETARG_DATUM(1));
642+
Datum interval_text = PG_GETARG_DATUM(2);
644643
Datum interval_value;
645644
Oid interval_type;
646645

647646
if (PG_ARGISNULL(0))
648-
elog(ERROR, "'partrel' should not be NULL");
647+
elog(ERROR, "'atttype' should not be NULL");
649648

650649
if (PG_ARGISNULL(1))
651-
elog(ERROR, "'attname' should not be NULL");
652-
653-
if (PG_ARGISNULL(2))
654650
elog(ERROR, "'parttype' should not be NULL");
655651

656652
/*
657653
* NULL interval is fine for both HASH and RANGE. But for RANGE we need
658654
* to make some additional checks
659655
*/
660-
if (!PG_ARGISNULL(3))
656+
if (!PG_ARGISNULL(2))
661657
{
662-
char *attname_cstr;
663-
Oid atttype; /* type of partitioned attribute */
664-
665658
if (parttype == PT_HASH)
666659
elog(ERROR, "interval must be NULL for HASH partitioned table");
667660

668-
/* Convert attname to CSTRING and fetch column's type */
669-
attname_cstr = text_to_cstring(attname);
670-
atttype = get_attribute_type(partrel, attname_cstr, false);
671-
672661
/* Try converting textual representation */
673662
interval_value = extract_binary_interval_from_text(interval_text,
674663
atttype,

src/relation_info.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ refresh_pathman_relation_info(Oid relid,
194194
* from config
195195
*/
196196
oldcontext = MemoryContextSwitchTo(TopMemoryContext);
197+
prel->expr_string = TextDatumGetCString(
198+
values[Anum_pathman_config_raw_expression - 1]);
197199
prel->expr = (Node *) stringToNode(expr);
198200
fix_opfuncids(prel->expr);
199201
prel->expr = expression_mutator(prel->expr, NULL);

0 commit comments

Comments
 (0)