1

If you worked with postgresql, you may know it stores various stats in internal views. I want to get stats with some interval and store it into a binary file. I see it in the following way:

  1. read stats with PQexec() from libpq.
  2. using info about the number of rows and columns from result, allocate a 3d pointer array.
  3. put result to the 3d array (which is already a part of struct)
  4. write struct with 3d array to binary file
  5. go to step 1 and repeat

Next, it's possible to read this binary file, read struct in to the similar 3d array and then sort values, view and so on.

And everything looks fine, until number of rows is constant, but in postgresql there are views where number of rows are changed permanently. Thus sizes of written structs will be always different and I don't know how to properly read these structs from file (because I need to know the size of struct).

Could you advise me or propose a better way to do it?

2
  • It sounds like you just need to write some metadata as a header in your file before the actual data. Then you'll have no problem encoding the element sizes. Commented Feb 18, 2017 at 5:29
  • yep, I thought about header and there is a one moment which confuses me -- Header is in the file has fixed size, thus we can put into the file a limited number of stats snapshots. Maybe it's better to user dedicated header for every stats snap. And firstly read row header (where size is located) and then read the date. Commented Feb 18, 2017 at 6:18

2 Answers 2

2

Basically you need to adopt a file format. The most basic format for something like this would be to write the size first and then the data. Then the reader would read the size, allocate the memory and then read the data.

Sign up to request clarification or add additional context in comments.

Comments

0

You're going to need a variant on TLV — Type, Length, Value — encoding. You store (fixed size) information about the variable length data that comes next. When reading, you read the fixed size info and use that to allocate the right space for reading the data that follows.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.