Skip to content

Commit 89b13a0

Browse files
committed
added set_interval() function
1 parent f8f71ab commit 89b13a0

File tree

6 files changed

+124
-64
lines changed

6 files changed

+124
-64
lines changed

init.sql

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,19 @@
88
* ------------------------------------------------------------------------
99
*/
1010

11+
12+
/*
13+
* Takes text representation of interval value and checks if it is corresponds
14+
* to partitioning key. The function throws an error if it fails to convert
15+
* text to Datum
16+
*/
17+
CREATE OR REPLACE FUNCTION @extschema@.validate_interval_value(
18+
parent REGCLASS,
19+
interval_value TEXT)
20+
RETURNS BOOL AS 'pg_pathman', 'validate_interval_value'
21+
LANGUAGE C STRICT;
22+
23+
1124
/*
1225
* Pathman config
1326
* partrel - regclass (relation type, stored as Oid)
@@ -23,7 +36,8 @@ CREATE TABLE IF NOT EXISTS @extschema@.pathman_config (
2336
parttype INTEGER NOT NULL,
2437
range_interval TEXT,
2538

26-
CHECK (parttype IN (1, 2)) /* check for allowed part types */
39+
CHECK (parttype IN (1, 2)), /* check for allowed part types */
40+
CHECK (@extschema@.validate_interval_value(partrel, range_interval))
2741
);
2842

2943

range.sql

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,30 @@ BEGIN
435435
END
436436
$$ LANGUAGE plpgsql;
437437

438+
439+
/*
440+
* Set (or reset) default interval for auto created partitions
441+
*/
442+
CREATE OR REPLACE FUNCTION @extschema@.set_interval(parent REGCLASS, value ANYELEMENT)
443+
RETURNS VOID AS
444+
$$
445+
DECLARE
446+
affected INTEGER;
447+
BEGIN
448+
UPDATE @extschema@.pathman_config
449+
SET range_interval = value::text
450+
WHERE partrel = parent;
451+
452+
GET DIAGNOSTICS affected = ROW_COUNT;
453+
454+
IF affected = 0 THEN
455+
RAISE EXCEPTION 'table "%" is not partitioned', parent;
456+
END IF;
457+
END
458+
$$
459+
LANGUAGE plpgsql;
460+
461+
438462
/*
439463
* Split RANGE partition
440464
*/

src/partition_creation.c

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,6 @@
4343
#include "utils/typcache.h"
4444

4545

46-
static Datum extract_binary_interval_from_text(Datum interval_text,
47-
Oid part_atttype,
48-
Oid *interval_type);
49-
5046
static void extract_op_func_and_ret_type(char *opname, Oid type1, Oid type2,
5147
Oid *move_bound_op_func,
5248
Oid *move_bound_op_ret_type);
@@ -427,64 +423,6 @@ create_partitions_for_value_internal(Oid relid, Datum value, Oid value_type)
427423
return partid;
428424
}
429425

430-
/*
431-
* Convert interval from TEXT to binary form using partitioned column's type.
432-
*/
433-
static Datum
434-
extract_binary_interval_from_text(Datum interval_text, /* interval as TEXT */
435-
Oid part_atttype, /* partitioned column's type */
436-
Oid *interval_type) /* returned value */
437-
{
438-
Datum interval_binary;
439-
const char *interval_cstring;
440-
441-
interval_cstring = TextDatumGetCString(interval_text);
442-
443-
/* If 'part_atttype' is a *date type*, cast 'range_interval' to INTERVAL */
444-
if (is_date_type_internal(part_atttype))
445-
{
446-
int32 interval_typmod = PATHMAN_CONFIG_interval_typmod;
447-
448-
/* Convert interval from CSTRING to internal form */
449-
interval_binary = DirectFunctionCall3(interval_in,
450-
CStringGetDatum(interval_cstring),
451-
ObjectIdGetDatum(InvalidOid),
452-
Int32GetDatum(interval_typmod));
453-
if (interval_type)
454-
*interval_type = INTERVALOID;
455-
}
456-
/* Otherwise cast it to the partitioned column's type */
457-
else
458-
{
459-
HeapTuple htup;
460-
Oid typein_proc = InvalidOid;
461-
462-
htup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(part_atttype));
463-
if (HeapTupleIsValid(htup))
464-
{
465-
typein_proc = ((Form_pg_type) GETSTRUCT(htup))->typinput;
466-
ReleaseSysCache(htup);
467-
}
468-
else
469-
elog(ERROR, "Cannot find input function for type %u", part_atttype);
470-
471-
/*
472-
* Convert interval from CSTRING to 'prel->atttype'.
473-
*
474-
* Note: We pass 3 arguments in case
475-
* 'typein_proc' also takes Oid & typmod.
476-
*/
477-
interval_binary = OidFunctionCall3(typein_proc,
478-
CStringGetDatum(interval_cstring),
479-
ObjectIdGetDatum(part_atttype),
480-
Int32GetDatum(-1));
481-
if (interval_type)
482-
*interval_type = part_atttype;
483-
}
484-
485-
return interval_binary;
486-
}
487-
488426
/*
489427
* Fetch binary operator by name and return it's function and ret type.
490428
*/

src/pl_range_funcs.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ PG_FUNCTION_INFO_V1( build_range_condition );
6060
PG_FUNCTION_INFO_V1( build_sequence_name );
6161
PG_FUNCTION_INFO_V1( merge_range_partitions );
6262
PG_FUNCTION_INFO_V1( drop_range_partition_expand_next );
63+
PG_FUNCTION_INFO_V1( validate_interval_value );
6364

6465

6566
/*
@@ -749,4 +750,26 @@ drop_range_partition_expand_next(PG_FUNCTION_ARGS)
749750
drop_table(relid);
750751

751752
PG_RETURN_VOID();
752-
}
753+
}
754+
755+
/*
756+
* Takes text representation of interval value and checks if it is corresponds
757+
* to partitioning key. The function throws an error if it fails to convert
758+
* text to Datum
759+
*/
760+
Datum
761+
validate_interval_value(PG_FUNCTION_ARGS)
762+
{
763+
const PartRelationInfo *prel;
764+
Oid parent = PG_GETARG_OID(0);
765+
Datum interval = PG_GETARG_DATUM(1);
766+
767+
/* TODO!!! */
768+
prel = get_pathman_relation_info(parent);
769+
if (!prel)
770+
PG_RETURN_BOOL(true);
771+
772+
extract_binary_interval_from_text(interval, prel->atttype, NULL);
773+
774+
PG_RETURN_BOOL(true);
775+
}

src/utils.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,3 +410,61 @@ perform_type_cast(Datum value, Oid in_type, Oid out_type, bool *success)
410410
}
411411
}
412412
}
413+
414+
/*
415+
* Convert interval from TEXT to binary form using partitioned column's type.
416+
*/
417+
Datum
418+
extract_binary_interval_from_text(Datum interval_text, /* interval as TEXT */
419+
Oid part_atttype, /* partitioned column's type */
420+
Oid *interval_type) /* returned value */
421+
{
422+
Datum interval_binary;
423+
const char *interval_cstring;
424+
425+
interval_cstring = TextDatumGetCString(interval_text);
426+
427+
/* If 'part_atttype' is a *date type*, cast 'range_interval' to INTERVAL */
428+
if (is_date_type_internal(part_atttype))
429+
{
430+
int32 interval_typmod = PATHMAN_CONFIG_interval_typmod;
431+
432+
/* Convert interval from CSTRING to internal form */
433+
interval_binary = DirectFunctionCall3(interval_in,
434+
CStringGetDatum(interval_cstring),
435+
ObjectIdGetDatum(InvalidOid),
436+
Int32GetDatum(interval_typmod));
437+
if (interval_type)
438+
*interval_type = INTERVALOID;
439+
}
440+
/* Otherwise cast it to the partitioned column's type */
441+
else
442+
{
443+
HeapTuple htup;
444+
Oid typein_proc = InvalidOid;
445+
446+
htup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(part_atttype));
447+
if (HeapTupleIsValid(htup))
448+
{
449+
typein_proc = ((Form_pg_type) GETSTRUCT(htup))->typinput;
450+
ReleaseSysCache(htup);
451+
}
452+
else
453+
elog(ERROR, "Cannot find input function for type %u", part_atttype);
454+
455+
/*
456+
* Convert interval from CSTRING to 'prel->atttype'.
457+
*
458+
* Note: We pass 3 arguments in case
459+
* 'typein_proc' also takes Oid & typmod.
460+
*/
461+
interval_binary = OidFunctionCall3(typein_proc,
462+
CStringGetDatum(interval_cstring),
463+
ObjectIdGetDatum(part_atttype),
464+
Int32GetDatum(-1));
465+
if (interval_type)
466+
*interval_type = part_atttype;
467+
}
468+
469+
return interval_binary;
470+
}

src/utils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ void fill_type_cmp_fmgr_info(FmgrInfo *finfo, Oid type1, Oid type2);
5656
*/
5757
char * datum_to_cstring(Datum datum, Oid typid);
5858
Datum perform_type_cast(Datum value, Oid in_type, Oid out_type, bool *success);
59+
Datum extract_binary_interval_from_text(Datum interval_text,
60+
Oid part_atttype,
61+
Oid *interval_type);
5962

6063

6164
#endif /* PATHMAN_UTILS_H */

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy