2022-07-12 11:18:16 +03:00
|
|
|
package postgres
|
|
|
|
|
2022-09-09 07:55:36 +03:00
|
|
|
import (
|
2022-09-09 15:16:48 +03:00
|
|
|
"fmt"
|
2022-10-31 14:02:08 +03:00
|
|
|
|
2022-10-28 13:09:09 +03:00
|
|
|
"github.com/wader/fq/format/postgres/common/pg_wal/pgproee"
|
2022-10-28 13:13:33 +03:00
|
|
|
"github.com/wader/fq/format/postgres/common/pg_wal/postgres"
|
2022-10-27 12:08:40 +03:00
|
|
|
|
2022-09-09 07:55:36 +03:00
|
|
|
"github.com/wader/fq/format"
|
|
|
|
"github.com/wader/fq/pkg/decode"
|
|
|
|
"github.com/wader/fq/pkg/interp"
|
2022-09-16 17:15:56 +03:00
|
|
|
|
2022-09-09 15:16:48 +03:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2022-09-09 07:55:36 +03:00
|
|
|
)
|
|
|
|
|
2022-10-26 10:13:50 +03:00
|
|
|
// partial parsing of WAL
|
2022-09-02 16:14:24 +03:00
|
|
|
|
2022-09-09 07:55:36 +03:00
|
|
|
func init() {
|
|
|
|
interp.RegisterFormat(decode.Format{
|
|
|
|
Name: format.PG_WAL,
|
|
|
|
Description: "PostgreSQL write-ahead log file",
|
2022-10-26 10:10:47 +03:00
|
|
|
DecodeFn: decodePGWAL,
|
2022-11-07 12:05:33 +03:00
|
|
|
DecodeInArg: format.PostgresIn{
|
2022-09-23 13:50:54 +03:00
|
|
|
Flavour: PG_FLAVOUR_POSTGRES14,
|
2022-09-09 07:55:36 +03:00
|
|
|
},
|
2022-10-28 13:55:38 +03:00
|
|
|
RootArray: true,
|
|
|
|
RootName: "pages",
|
2022-09-09 07:55:36 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-09-09 15:16:48 +03:00
|
|
|
func ParseLsn(lsn string) (uint32, error) {
|
|
|
|
// check for 0/4E394440
|
|
|
|
str1 := lsn
|
|
|
|
if strings.Contains(lsn, "/") {
|
|
|
|
parts := strings.Split(lsn, "/")
|
|
|
|
if len(parts) != 2 {
|
2022-09-16 17:15:56 +03:00
|
|
|
return 0, fmt.Errorf("invalid lsn = %s", lsn)
|
2022-09-09 15:16:48 +03:00
|
|
|
}
|
|
|
|
str1 = parts[1]
|
|
|
|
}
|
|
|
|
// parse hex to coded file name + file offset
|
|
|
|
r1, err := strconv.ParseInt(str1, 16, 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return uint32(r1), err
|
|
|
|
}
|
|
|
|
|
2022-10-26 10:10:47 +03:00
|
|
|
func decodePGWAL(d *decode.D, in any) any {
|
2022-09-09 07:55:36 +03:00
|
|
|
d.Endian = decode.LittleEndian
|
|
|
|
|
2022-11-07 12:05:33 +03:00
|
|
|
pgIn, ok := in.(format.PostgresIn)
|
2022-09-09 07:55:36 +03:00
|
|
|
if !ok {
|
|
|
|
d.Fatalf("DecodeInArg must be PostgresIn!\n")
|
|
|
|
}
|
|
|
|
|
2022-10-28 13:09:09 +03:00
|
|
|
switch pgIn.Flavour {
|
|
|
|
case PG_FLAVOUR_POSTGRES10,
|
|
|
|
PG_FLAVOUR_POSTGRES11,
|
|
|
|
PG_FLAVOUR_POSTGRES12,
|
|
|
|
PG_FLAVOUR_POSTGRES13,
|
|
|
|
PG_FLAVOUR_POSTGRES14,
|
|
|
|
PG_FLAVOUR_PGPRO10,
|
|
|
|
PG_FLAVOUR_PGPRO11,
|
|
|
|
PG_FLAVOUR_PGPRO12,
|
|
|
|
PG_FLAVOUR_PGPRO13,
|
|
|
|
PG_FLAVOUR_PGPRO14:
|
2022-11-07 12:05:33 +03:00
|
|
|
return postgres.DecodePGWAL(d)
|
2022-10-28 13:09:09 +03:00
|
|
|
|
|
|
|
case PG_FLAVOUR_PGPROEE10,
|
|
|
|
PG_FLAVOUR_PGPROEE11,
|
|
|
|
PG_FLAVOUR_PGPROEE12,
|
|
|
|
PG_FLAVOUR_PGPROEE13,
|
|
|
|
PG_FLAVOUR_PGPROEE14:
|
2022-11-07 12:05:33 +03:00
|
|
|
return pgproee.DecodePGWAL(d)
|
2022-10-28 13:09:09 +03:00
|
|
|
|
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2022-11-07 12:05:33 +03:00
|
|
|
return postgres.DecodePGWAL(d)
|
2022-09-09 07:55:36 +03:00
|
|
|
}
|