无法 运行 TimescaleDB drop_chunks 使用 C# NHibernate 查询

Unable to run TimescaleDB drop_chunks query with C# NHibernate

这可能是一个初学者问题。

因此,我需要从已确定的时间间隔以编程方式从 PostgreSQL 中清除旧数据(例如,超过 3 个月的传感器数据)。我已经成功地 运行 使用 pgAdmin 在 PostgreSQL 中进行查询,但是以编程方式使用 NHibernate IQuery.ExecuteUpdate() 命令,它给了我错误消息

{"could not execute native bulk manipulation query:SELECT drop_chunks('2020-05-15 17:59:30.636'::timestamp, 'attribute_value', 'hm_attr');[SQL: SQL not available]"}

这是完整的堆栈跟踪:

    at NHibernate.Engine.Query.NativeSQLQueryPlan.PerformExecuteUpdate(QueryParameters queryParameters, ISessionImplementor session)
   at NHibernate.Impl.StatelessSessionImpl.ExecuteNativeUpdate(NativeSQLQuerySpecification nativeSQLQuerySpecification, QueryParameters queryParameters)
   at NHibernate.Impl.SqlQueryImpl.ExecuteUpdate()
   at Platform.Server.Kernel.Persistence.clExecuteQuery.ExecuteSqlCommand(clQueryItem QueryItem, IStatelessSession Session) in C:\git\smartmine\Server\Platform\Kernel\PersistenceManager\Query\clExecuteQuery.cs:line 184
   at Platform.Server.Kernel.Persistence.PersistenceManager.ExecuteSQL(String SqlCommand, Object[] ParametersValues) in C:\git\smartmine\Server\Platform\Kernel\PersistenceManager\PersistenceManager.cs:line 362

InnerException 说:

    The given key was not present in the dictionary.

   at System.ThrowHelper.ThrowKeyNotFoundException()
   at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
   at NHibernate.Param.NamedParameterSpecification.SetEffectiveType(QueryParameters queryParameters)
   at NHibernate.Param.ParametersBackTrackExtensions.ResetEffectiveExpectedType(IEnumerable`1 parameterSpecs, QueryParameters queryParameters)
   at NHibernate.Engine.Query.NativeSQLQueryPlan.PerformExecuteUpdate(QueryParameters queryParameters, ISessionImplementor session)

我尝试过的另一种方法是使用 NHibernate IQuery.List() returns

Not all named parameters have been set: [':timestamp'] [SELECT drop_chunks('2020-05-15 18:13:08.841'::timestamp, 'attribute_value', 'hm_attr');]

具有以下堆栈跟踪:

   at NHibernate.Impl.AbstractQueryImpl.VerifyParameters(Boolean reserveFirstParameter)
   at NHibernate.Impl.SqlQueryImpl.VerifyParameters()
   at NHibernate.Impl.SqlQueryImpl.List()
   at Platform.Server.Kernel.Persistence.clExecuteQuery.ExecuteQueryInternal(Object[] Parameters, IQuery Query) in C:\git\smartmine\Server\Platform\Kernel\PersistenceManager\Query\clExecuteQuery.cs:line 476
   at Platform.Server.Kernel.Persistence.clExecuteQuery.ExecuteQuery(clQueryItem QueryItem, IStatelessSession Session) in C:\git\smartmine\Server\Platform\Kernel\PersistenceManager\Query\clExecuteQuery.cs:line 247
   at Platform.Server.Kernel.Persistence.PersistenceManager.SelectSQL(String SQL_Expression, Object[] ParametersValues) in C:\git\smartmine\Server\Platform\Kernel\PersistenceManager\PersistenceManager.cs:line 674
   at Platform.Server.Kernel.Persistence.PersistenceManager.PurgeTimescaleExpiredChunks(Type EntityType, DateTime PurgeItemsOlderThanThisDate) in C:\git\smartmine\Server\Platform\Kernel\PersistenceManager\PersistenceManager.cs:line 1098
   at Platform.Server.SystemBase.TemporalData.AttributeValueHandler.PurgeExpiredItems(String ExpiringProperty, DateTime PurgeItemsOlderThanThisDate) in C:\git\smartmine\Server\Platform\SystemBase\TemporalDataManager\AttributeValue\AttributeValueHandler.cs:line 615
   at Platform.Server.SystemBase.Data.clDataManager.ExecutePurge(DateTime PurgeStart) in C:\git\smartmine\Server\Platform\SystemBase\DataManager\clDataManager.cs:line 976

TimescaleDB API 版本:0.9.2 文档说 https://docs.timescale.com/v0.9/api#drop_chunks, which obviously does not cover running things from NHibernate. I'm having a hard time to find NHibernate docs about this query API, because all I've found comes from the "SELECT * FROM" standard usability (like here https://nhibernate.info/doc/nhibernate-reference/querysql.html).

再一次,这可能是一个初学者问题,如果我误解了什么,抱歉。非常感谢任何帮助,提前致谢!

原来我预料到过程中会出现有关 DateTime 转换的问题。基本上,我遇到了 TimescaleDB 错误

Cannot call drop_chunks with a date on hypertables with a time type of: timestamp without time zone

因为,正如错误所述,我的基础 hypertable 的日期类型实际上是

timestamp without timezone

不知何故,在我通过将日期转换为

来解决问题之后
'2020-05-15 17:59:30.636'::timestamp

我已经预料到日期转换会出错,并通过添加 .ToString() 调用和其他过度工程程序弄乱了格式。

通过简单地将原始日期作为参数传递问题就解决了,所以代码看起来很简单

SELECT drop_chunks(:p0, :p1, :p2)

其中 :p0 是 DateTime 变量,:p1 是 table 名称,而 :p2 是架构名称,当然,所有这些都区分大小写。