@@ -150,10 +150,10 @@ const (
150150const specialMetricPgbouncer = "^pgbouncer_(stats|pools)$"
151151
152152var (
153- regexIsPgbouncerMetrics = regexp .MustCompile (specialMetricPgbouncer )
154- forceRecreatePGMetricPartitions = false // to signal override PG metrics storage cache
155- partitionMapMetric = make (map [string ]ExistingPartitionInfo ) // metric = min/max bounds
156- partitionMapMetricDbname = make (map [string ]map [string ]ExistingPartitionInfo ) // metric[dbname = min/max bounds]
153+ regexIsPgbouncerMetrics = regexp .MustCompile (specialMetricPgbouncer )
154+ forceRecreatePartitions = false // to signal override PG metrics storage cache
155+ partitionMapMetric = make (map [string ]ExistingPartitionInfo ) // metric = min/max bounds
156+ partitionMapMetricDbname = make (map [string ]map [string ]ExistingPartitionInfo ) // metric[dbname = min/max bounds]
157157)
158158
159159// SyncMetric ensures that tables exist for newly added metrics and/or sources
@@ -333,15 +333,16 @@ func (pgw *PostgresWriter) flush(msgs []metrics.MeasurementEnvelope) {
333333 }
334334 }
335335
336- if pgw .metricSchema == DbStorageSchemaPostgres {
337- err = pgw .EnsureMetricDbnameTime (pgPartBoundsDbName , forceRecreatePGMetricPartitions )
338- } else if pgw .metricSchema == DbStorageSchemaTimescale {
339- err = pgw .EnsureMetricTimescale (pgPartBounds , forceRecreatePGMetricPartitions )
340- } else {
336+ switch pgw .metricSchema {
337+ case DbStorageSchemaPostgres :
338+ err = pgw .EnsureMetricDbnameTime (pgPartBoundsDbName , forceRecreatePartitions )
339+ case DbStorageSchemaTimescale :
340+ err = pgw .EnsureMetricTimescale (pgPartBounds , forceRecreatePartitions )
341+ default :
341342 logger .Fatal ("unknown storage schema..." )
342343 }
343- if forceRecreatePGMetricPartitions {
344- forceRecreatePGMetricPartitions = false
344+ if forceRecreatePartitions {
345+ forceRecreatePartitions = false
345346 }
346347 if err != nil {
347348 pgw .lastError <- err
@@ -385,8 +386,8 @@ func (pgw *PostgresWriter) flush(msgs []metrics.MeasurementEnvelope) {
385386
386387 if _ , err = pgw .sinkDb .CopyFrom (context .Background (), getTargetTable (), getTargetColumns (), pgx .CopyFromRows (rows )); err != nil {
387388 l .Error (err )
388- forceRecreatePGMetricPartitions = strings .Contains (err .Error (), "no partition" )
389- if forceRecreatePGMetricPartitions {
389+ forceRecreatePartitions = strings .Contains (err .Error (), "no partition" )
390+ if forceRecreatePartitions {
390391 logger .Warning ("Some metric partitions might have been removed, halting all metric storage. Trying to re-create all needed partitions on next run" )
391392 }
392393 }
@@ -404,7 +405,7 @@ func (pgw *PostgresWriter) flush(msgs []metrics.MeasurementEnvelope) {
404405// EnsureMetricTime creates special partitions if Timescale used for realtime metrics
405406func (pgw * PostgresWriter ) EnsureMetricTime (pgPartBounds map [string ]ExistingPartitionInfo , force bool ) error {
406407 logger := log .GetLogger (pgw .ctx )
407- sqlEnsure := `select * from admin.ensure_partition_metric_time($1, $2)`
408+ sqlEnsure := `select part_available_from, part_available_to from admin.ensure_partition_metric_time($1, $2)`
408409 for metric , pb := range pgPartBounds {
409410 if ! strings .HasSuffix (metric , "_realtime" ) {
410411 continue
@@ -415,17 +416,18 @@ func (pgw *PostgresWriter) EnsureMetricTime(pgPartBounds map[string]ExistingPart
415416
416417 partInfo , ok := partitionMapMetric [metric ]
417418 if ! ok || (ok && (pb .StartTime .Before (partInfo .StartTime ))) || force {
418- err := pgw .sinkDb .QueryRow (pgw .ctx , sqlEnsure , metric , pb .StartTime ).Scan (& partInfo )
419+ err := pgw .sinkDb .QueryRow (pgw .ctx , sqlEnsure , metric , pb .StartTime ).
420+ Scan (& partInfo .StartTime , & partInfo .EndTime )
419421 if err != nil {
420- logger .Error ("Failed to create partition on 'metrics':" , err )
422+ logger .Error ("Failed to create partition on 'metrics': " , err )
421423 return err
422424 }
423425 partitionMapMetric [metric ] = partInfo
424426 }
425427 if pb .EndTime .After (partInfo .EndTime ) || force {
426- err := pgw .sinkDb .QueryRow (pgw .ctx , sqlEnsure , metric , pb .EndTime ).Scan (& partInfo .EndTime )
428+ err := pgw .sinkDb .QueryRow (pgw .ctx , sqlEnsure , metric , pb .EndTime ).Scan (nil , & partInfo .EndTime )
427429 if err != nil {
428- logger .Error ("Failed to create partition on 'metrics':" , err )
430+ logger .Error ("Failed to create partition on 'metrics': " , err )
429431 return err
430432 }
431433 partitionMapMetric [metric ] = partInfo
0 commit comments