Parsing libpq PQtrace files - Mailing list pgsql-general

From Dominique Devienne
Subject Parsing libpq PQtrace files
Date
Msg-id CAFCRh-8OPoe=0j9v4wth7qU-x4jvjVc8DoOLV66qdnC9Do0ymw@mail.gmail.com
Whole thread Raw
Responses Re: Parsing libpq PQtrace files  (Alvaro Herrera <alvherre@alvh.no-ip.org>)
List pgsql-general
Hi. I'm tracing my client-side connection(s) for the first time.

The format seems to be line oriented.
Selected text columns with embedded newlines have those newlines
encoded as \x0a to remain single-line in the trace. So far so good.

But then on Parse, the query text, which also contains embedded newlines
but also embedded double-quotes, for column aliases, or table names, or whatever,
even why not a single char literal like '"' (i.e. single-quote, double-quote, single-quote),
seems to be double-quoted only. So how am I supposed to know when the SQL ends?

The message length (320 in the sample trace fragment below) does not seems to correspond
to the length in the trace file itself (perhaps was the original length before encoding in the trace?).

So how am I supposed to parse the trace given those multi-line-SQL Parse entries?
Which could have an arbitrary number of embedded double-quotes, supposed to be
the end of the SQL to parse?

I'm probably missing something...
As I suppose to trace format is supposed to be easily machine parseable, no?

I'd appreciate any insights. Thanks, --DD

PS: And on the off chance there's code out there that parses these traces,
  if someone knows a public source of similar code, I'd appreciate a link or two.

```
2023-10-17 15:27:36.334749 B 5 ReadyForQuery I
2023-10-17 15:27:36.334881 F 320 Parse "" "

select ..., ... as "name-mismatch", ...
  from "Name needing quoting".ztable1 s
  full outer join " Name needing quoting".ztable2 m using(col1)
 where ...
 order by ...

" 0
2023-10-17 15:27:36.334889 F 14 Bind "" "" 0 0 1 1
2023-10-17 15:27:36.334895 F 6 Describe P ""
2023-10-17 15:27:36.334900 F 9 Execute "" 0
2023-10-17 15:27:36.334904 F 4 Sync
2023-10-17 15:27:36.338536 B 4 ParseComplete
2023-10-17 15:27:36.338579 B 4 BindComplete
2023-10-17 15:27:36.338601 B 146 RowDescription 5 "col2" 333038706 9 25 65535 -1 1 "col3" 0 0 25 65535 -1 1 "name-mismatch" 0 0 16 1 -1 1 "col4" 0 0 25 65535 -1 1 "col5" 333038718 2 19 64 -1 1
2023-10-17 15:27:36.338659 B 108 DataRow 5 -1 7 ...
2023-10-17 15:27:36.338690 B 108 DataRow 5 -1 7 ...
2023-10-17 14:46:40.576933 B 226 DataRow 5 ... 139 'line1\x0aline2' 1 '\x01' 1 '\x00'
...
2023-10-17 15:27:36.338958 B 14 CommandComplete "SELECT 10"
2023-10-17 15:27:36.338971 B 5 ReadyForQuery I
```

pgsql-general by date:

Previous
From: Avi Weinberg
Date:
Subject: Logical Replication - Adding Not-Null Column Without Default Value
Next
From: Alvaro Herrera
Date:
Subject: Re: Parsing libpq PQtrace files