Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit89b13a0

Browse files
committed
added set_interval() function
1 parentf8f71ab commit89b13a0

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+
CREATEOR REPLACE FUNCTION @extschema@.validate_interval_value(
18+
parentREGCLASS,
19+
interval_valueTEXT)
20+
RETURNS BOOLAS'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
parttypeINTEGERNOT NULL,
2437
range_intervalTEXT,
2538

26-
CHECK (parttypeIN (1,2))/* check for allowed part types*/
39+
CHECK (parttypeIN (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+
CREATEOR REPLACE FUNCTION @extschema@.set_interval(parent REGCLASS, value ANYELEMENT)
443+
RETURNS VOIDAS
444+
$$
445+
DECLARE
446+
affectedINTEGER;
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-
staticDatumextract_binary_interval_from_text(Datuminterval_text,
47-
Oidpart_atttype,
48-
Oid*interval_type);
49-
5046
staticvoidextract_op_func_and_ret_type(char*opname,Oidtype1,Oidtype2,
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
returnpartid;
428424
}
429425

430-
/*
431-
* Convert interval from TEXT to binary form using partitioned column's type.
432-
*/
433-
staticDatum
434-
extract_binary_interval_from_text(Datuminterval_text,/* interval as TEXT */
435-
Oidpart_atttype,/* partitioned column's type */
436-
Oid*interval_type)/* returned value */
437-
{
438-
Datuminterval_binary;
439-
constchar*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-
int32interval_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-
HeapTuplehtup;
460-
Oidtypein_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-
returninterval_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+
constPartRelationInfo*prel;
764+
Oidparent=PG_GETARG_OID(0);
765+
Datuminterval=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(Datuminterval_text,/* interval as TEXT */
419+
Oidpart_atttype,/* partitioned column's type */
420+
Oid*interval_type)/* returned value */
421+
{
422+
Datuminterval_binary;
423+
constchar*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+
int32interval_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+
HeapTuplehtup;
444+
Oidtypein_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+
returninterval_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(Datumdatum,Oidtypid);
5858
Datumperform_type_cast(Datumvalue,Oidin_type,Oidout_type,bool*success);
59+
Datumextract_binary_interval_from_text(Datuminterval_text,
60+
Oidpart_atttype,
61+
Oid*interval_type);
5962

6063

6164
#endif/* PATHMAN_UTILS_H */

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp