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



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

因此,我需要以编程方式从 PostgreSQL 中清除已确定的时间间隔(例如,超过 3 个月的传感器数据(中的旧数据。我已经设法使用 pgAdmin 在 PostgreSQL 中成功运行查询,但以编程方式使用 NHibernateIQuery.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:gitsmartmineServerPlatformKernelPersistenceManagerQueryclExecuteQuery.cs:line 184
at Platform.Server.Kernel.Persistence.PersistenceManager.ExecuteSQL(String SqlCommand, Object[] ParametersValues) in C:gitsmartmineServerPlatformKernelPersistenceManagerPersistenceManager.cs:line 362

内部异常 说:

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)

我尝试过的另一种方法是使用 NHibernateIQuery.List()它返回

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:gitsmartmineServerPlatformKernelPersistenceManagerQueryclExecuteQuery.cs:line 476
at Platform.Server.Kernel.Persistence.clExecuteQuery.ExecuteQuery(clQueryItem QueryItem, IStatelessSession Session) in C:gitsmartmineServerPlatformKernelPersistenceManagerQueryclExecuteQuery.cs:line 247
at Platform.Server.Kernel.Persistence.PersistenceManager.SelectSQL(String SQL_Expression, Object[] ParametersValues) in C:gitsmartmineServerPlatformKernelPersistenceManagerPersistenceManager.cs:line 674
at Platform.Server.Kernel.Persistence.PersistenceManager.PurgeTimescaleExpiredChunks(Type EntityType, DateTime PurgeItemsOlderThanThisDate) in C:gitsmartmineServerPlatformKernelPersistenceManagerPersistenceManager.cs:line 1098
at Platform.Server.SystemBase.TemporalData.AttributeValueHandler.PurgeExpiredItems(String ExpiringProperty, DateTime PurgeItemsOlderThanThisDate) in C:gitsmartmineServerPlatformSystemBaseTemporalDataManagerAttributeValueAttributeValueHandler.cs:line 615
at Platform.Server.SystemBase.Data.clDataManager.ExecutePurge(DateTime PurgeStart) in C:gitsmartmineServerPlatformSystemBaseDataManagerclDataManager.cs:line 976

TimescaleDB API 版本:0.9.2 文档说 https://docs.timescale.com/v0.9/api#drop_chunks,这显然不包括从 NHibernate 运行的东西。我很难找到有关此查询 API 的 NHibernate 文档,因为我发现的只是"SELECT * FROM"标准可用性(如此处 https://nhibernate.info/doc/nhibernate-reference/querysql.html(。

同样,这可能是一个初学者问题,对不起,如果我误解了什么。任何帮助都非常感谢,提前感谢!

事实证明,我预计在此过程中会出现有关日期时间转换的问题。基本上,我遇到了TimescaleDB错误

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

因为,正如错误所述,我的基本超级表的日期类型实际上是

timestamp without timezone

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

'2020-05-15 17:59:30.636'::timestamp

我已经预料到日期转换的错误,并通过添加.ToString(( 调用和其他过度设计的过程。

通过简单地将粗略日期作为参数传递即可解决问题,因此代码看起来就像

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

其中 :p 0 是 DateTime 变量,:p 1 是表名,:p 2 是架构名称,当然,所有这些都区分大小写。

最新更新