使用PostgreSQL作为Microsoft Orleans集群存储报错:列'silohost'不存在
Orleans 7.2.7 PostgreSQL集群存储问题排查
我使用v7.2.7版本创建了Microsoft Orleans应用,并按照官方文档创建了PostgreSQL表,但遇到两个问题:
问题1:必须手动插入特定记录才能启动
应用启动时会抛出异常,除非手动执行以下SQL插入OrleansQuery表:
insert into OrleansQuery(QueryKey, QueryText) values ( 'CleanupDefunctSiloEntriesKey', 'delete from OrleansMembershipTable where DeploymentId = @DeploymentId and @SiloHost = HostName and @SiloPort = Port and Status = @NonActiveStatus and IAmAliveTime < @IAmAliveTime; select ROW_COUNT();');
问题2:执行该查询时触发列不存在异常
插入上述记录后,应用执行CleanupDefunctSiloEntriesKey查询时抛出以下异常:
Npgsql.PostgresException (0x80004005): 42703: column "silohost" does not exist Npgsql.Internal.NpgsqlConnector.ReadMessageLong(Boolean async, DAtaRowLoadingMode dataRowLoadingMode, Boolean readingNOtifications, Boolean isReadingPrependedMessage) POSITION: 79 ...(异常堆栈信息省略) Exception data: Severity: ERROR SqlState: 42703 MessageText: column "silohost" does not exist
我的集群配置代码如下:
private static void ConfigurePostgreSqlClustering(ISiloBuilder siloBuilder, IConfiguration configuration) { var config = configuration.GetSection("database").Get<DatabaseConfig>(); ArgumentNullException.ThrowIfNull(config); var connectionString = $"Host={config.Server}; Database={config.Name}; Username={config.Username}; Password={config.Password}; Timeout=600;Command Timeout=600;"; siloBuilder.UseAdoNetClustering(options => { options.ConnectionString = connectionString; options.Invariant = "Npgsql"; }); siloBuilder.AddAdoNetGrainStorage(Constants.Orleans.GrainStorage, options => { options.ConnectionString = connectionString; options.Invariant = "Npgsql"; }); }
请问该如何解决这些问题?
解决方案
1. 修正CleanupDefunctSiloEntriesKey的查询语句
手动插入的查询语句是MySQL语法,完全不兼容PostgreSQL,且条件逻辑写反,导致数据库误将参数名识别为列名。执行以下适配PostgreSQL的正确插入语句:
insert into OrleansQuery(QueryKey, QueryText) values ( 'CleanupDefunctSiloEntriesKey', 'delete from OrleansMembershipTable where DeploymentId = :DeploymentId and HostName = :SiloHost and Port = :SiloPort and Status = :NonActiveStatus and IAmAliveTime < :IAmAliveTime; SELECT row_count();');
关键修正点:
- 将
@参数名改为PostgreSQL支持的:参数名命名参数格式 - 修正条件判断顺序:原语句
@SiloHost = HostName是错误的,应改为HostName = :SiloHost(列名在前,参数在后) - 使用PostgreSQL原生的
row_count()函数获取受影响行数
2. 彻底避免手动插入问题
检查你使用的PostgreSQL初始化脚本,确认是否遗漏了CleanupDefunctSiloEntriesKey记录。建议直接使用Orleans 7.2.7版本对应的官方初始化脚本重新初始化数据库,确保所有必要的查询记录都已正确创建,无需手动补全。
3. 确认配置有效性
你的集群存储配置是正确的,Invariant = "Npgsql"符合PostgreSQL驱动要求,无需调整。
内容的提问来源于stack exchange,提问作者ca9163d9
相关产品推荐
相关产品推荐

