我的故事评价自this :
我创建了一个函数 create dynamic partition table with Table_Year_Month format such as table_2018_04, table_2018_05 .... 创建分区函数的参数是 bigint
例如 create_partition_function(1518164237,1520583437) ;
。之后,我将 bigint 转换为日期,以便可以从时间戳中获取 year
和 month
。但是检查函数 ( check(timestamp >= date))
不起作用
我无法在 sql 中比较 bigint
>= date
。有什么运营商可以比得上他们?
我尝试使用 sql 的 UNIX_TIMESTAMP
函数将时间戳转换为日期时间,但不起作用
CREATE OR REPLACE FUNCTION create_partition_function( DATE, DATE )
returns void AS $$
DECLARE
create_query text;
index_query text;
BEGIN
FOR create_query, index_query IN SELECT
'create table user_event_firebase_'
|| TO_CHAR( d, 'YYYY_MM' )
|| ' ( check( timestamp >= UNIX_TIMESTAMP(date,'%Y %M %D' )'''
|| TO_CHAR( d, 'YYYY-MM-DD' )
|| ''' and timestamp < UNIX_TIMESTAMP(date,'%Y %M %D' ) '''
|| TO_CHAR( d + INTERVAL '1 month', 'YYYY-MM-DD' )
|| ''' ) ) inherits ( user_event_firebase );',
'create index user_event_firebase_'
|| TO_CHAR( d, 'YYYY_MM' )
|| '_time on user_event_firebase_'
|| TO_CHAR( d, 'YYYY_MM' )
|| ' ( timestamp );'
FROM generate_series( to_timestamp($1), to_timestamp($2), '1 month'::interval ) AS d
LOOP
EXECUTE create_query;
EXECUTE index_query;
END LOOP;
END;
$$
language plpgsql;
p/s : bigint
和date
是sql中的数据类型。
ERROR: invalid input syntax for integer: "2018-02-09"
CONTEXT: SQL statement "create table user_event_firebase_2018_02 ( check( timestamp >= bigint '2018-02-09' and timestamp < bigint '2018-03-09' ) ) inherits ( user_event_firebase );"
PL/pgSQL function create_partition_function(date,date) line 21 at EXECUTE
最佳答案
您可以使用将日期/时间戳转换为秒
SELECT extract(epoch from '2016-05-03'::date)
--result: 1462233600
SELECT to_timestamp(1462233600)::date;
--result: '2016-05-03'
关于sql - 在 postgresql 中比较 `bigint` 和 `date` 的运算符到底是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49751570/