Skip to content
This repository was archived by the owner on Oct 23, 2026. It is now read-only.
This repository was archived by the owner on Oct 23, 2026. It is now read-only.

How to match multi-line text in mtail (e.g., for PostgreSQL CSV logs with multi-line statements) #970

Open

Description

@cobolbaby

I'm currently working on monitoring PostgreSQL logs in CSV format using mtail. One challenge is that SQL statements in the statement field may span multiple lines. This makes it difficult to extract meaningful labels or metrics using mtail, since it appears to process logs line-by-line.

For example, a single statement field in a PostgreSQL log might contain embedded newlines like this:

2025-06-26 01:47:40.578 CST,,,2348220,,685c3414.23d4bc,1,,2025-06-26 01:38:28 CST,509/2455864,0,LOG,00000,"automatic vacuum of table ""bdc.pg_catalog.pg_attribute"": index scans: 1
pages: 0 removed, 2420645 remain, 0 skipped due to pins, 2391644 skipped frozen
tuples: 18257 removed, 1605338 remain, 1419055 are dead but not yet removable, oldest xmin: 3862849561
buffer usage: 1441377 hits, 1443437 misses, 2468 dirtied
avg read rate: 22.069 MB/s, avg write rate: 0.038 MB/s
system usage: CPU: user: 9.42 s, system: 8.80 s, elapsed: 510.99 s",,,,,,,,,""

I’m wondering:

  • Does mtail support any way to accumulate or buffer multiple lines to allow multi-line matching?
  • If not directly supported, are there recommended workarounds for this scenario?

This would be very helpful when we want to extract meaningful metrics or tags (e.g., command type, table name) from full SQL statements for observability purposes.

Thanks in advance for your guidance!

Activity

  1. cobolbaby commented on Jun 29, 2025

    @cobolbaby
    Author

    @jaqx0r

    May I ask—does the version you support include the capability mentioned in the issue?

  2. jaqx0r commented on Jun 29, 2025

    @jaqx0r
    Contributor

    Hi, thanks for the question!

    mtail is a line-oriented log scanner. It doesn't do multi-line matching in the log stream reader.

    The way to handle this is to use internal state in the program, as described in https://github.com/google/mtail/blob/main/docs/state.md

    You would start a new state when you see the timestamp or some sort of session or request id, and then for subsequent matched lines reuse that by fetching from the hidden state variable.

    When writing in this pattern, remember that the entire program runs once per line read.

    The caveat is that your program will then depend on the log file being "atomically" written to by the generating program; in this case can you be sure that PostgresQL writes the whole block in one call? If not, then you may accidentally correlate events with the wrong timestamp or key.

  3. jaqx0r commented on Jun 29, 2025

    @jaqx0r
    Contributor

    I have no plans for adding multi-line support, because it's pretty tricky. It relies on being able to correlate lines with each other; JSON format is sufficient that we can look for the outer '{}' pair, but in this example from PostgresQL there's no indication about the boundaries of the entire record.

    A way to handle this in mtail might be to pass information to the log reader about how to batch lines together. This sounds to me, though, like a workaround and will fail in some edge cases.

  4. jaqx0r commented on Jun 29, 2025

    @jaqx0r
    Contributor

    Also, fyi, development here has ceased and moved to https://github.com/jaqx0r/mtail, per the bug #929

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions