Unix Technical Forum

SEO

vBulletin Search Engine Optimization


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > Pgsql Patches

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-18-2008, 10:07 AM
Greg Smith
 
Posts: n/a
Default Re: COPY-able csv log outputs

I got a chance to review this patch over the weekend. Basic API seems
good, met all my requirements, no surprises with how the GUC variable
controlled the feature.

The most fundamental issue I have with the interface is that using COPY
makes it difficult to put any unique index on the resulting table. I like
to have a unique index on my imported log table because it rejects the
dupe records if you accidentally import the same section of log file
twice. COPY tosses the whole thing if there's an index violation, which
is a problem during a regular import because you will occasionally come
across lines with the same timestamp that are similar in every way except
for their statment; putting an index on the timestamp+statement seems
impractical.

I've had a preference for INSERT from the beginning here that this
reinforces. I'm planning to just work around this issue by doing the COPY
into a temporary table and then INSERTing from there. I didn't want to
just let the concern pass by without mentioning it though. It crosses my
mind that inserting some sort of unique log file line ID number would
prevent the dupe issue and make for better ordering (it's possible to have
two lines with the same timestamp show up in the wrong order now), not
sure that's a practical idea to consider.

The basic coding of the patch seemed OK to me, but someone who is much
more familiar than myself with the mechanics of pipes should take a look
at that part of the patch before committing; it's complicated code and I
can't comment on it. There are some small formatting issues that need to
be fixed, particularly in the host+port mapping. I can fix those myself
and submit a slightly updated patch. There's some documentation
improvements I want to make before this goes in as well.

The patch is actually broken fairly hard right now because of the switch
from INSERT to COPY FROM CSV as the output format at the last minute. It
outputs missing fields as NULL (fine for INSERT) that chokes the CSV
import when the session_start timestamp is missing. All of those NULL
values need to be just replaced with nothing for proper CSV syntax; there
should just the comma for the next field. I worked around this with

copy pglog from '/opt/pgsql/testlog.csv' with CSV null as 'NULL';

I can fix that too when I'm revising. I plan to have a version free of
obvious bugs to re-submit ready by next weekend.

--
* Greg Smith gsmith@gregsmith.com http://www.gregsmith.com Baltimore, MD

---------------------------(end of broadcast)---------------------------
TIP 1: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-18-2008, 10:07 AM
Andrew Dunstan
 
Posts: n/a
Default Re: COPY-able csv log outputs



Greg Smith wrote:
> I got a chance to review this patch over the weekend. Basic API seems
> good, met all my requirements, no surprises with how the GUC variable
> controlled the feature.
>
> The most fundamental issue I have with the interface is that using
> COPY makes it difficult to put any unique index on the resulting
> table. I like to have a unique index on my imported log table because
> it rejects the dupe records if you accidentally import the same
> section of log file twice. COPY tosses the whole thing if there's an
> index violation, which is a problem during a regular import because
> you will occasionally come across lines with the same timestamp that
> are similar in every way except for their statment; putting an index
> on the timestamp+statement seems impractical.


Does the format not include the per-process line number? (I know i
briefly looked at this patch previously, but I forget the details.) One
reason I originally included line numbers in log_line-prefix was to
handle this sort of problem.

>
> I've had a preference for INSERT from the beginning here that this
> reinforces.


COPY is our standard bulk insert mechanism. I think arguing against it
would be a very hard sell.

> I'm planning to just work around this issue by doing the COPY into a
> temporary table and then INSERTing from there. I didn't want to just
> let the concern pass by without mentioning it though. It crosses my
> mind that inserting some sort of unique log file line ID number would
> prevent the dupe issue and make for better ordering (it's possible to
> have two lines with the same timestamp show up in the wrong order
> now), not sure that's a practical idea to consider.


I guess that answers my question. We should definitely provide a unique
line key.
>
> The basic coding of the patch seemed OK to me, but someone who is much
> more familiar than myself with the mechanics of pipes should take a
> look at that part of the patch before committing; it's complicated
> code and I can't comment on it. There are some small formatting
> issues that need to be fixed, particularly in the host+port mapping.
> I can fix those myself and submit a slightly updated patch. There's
> some documentation improvements I want to make before this goes in as
> well.
>
> The patch is actually broken fairly hard right now because of the
> switch from INSERT to COPY FROM CSV as the output format at the last
> minute. It outputs missing fields as NULL (fine for INSERT) that
> chokes the CSV import when the session_start timestamp is missing.
> All of those NULL values need to be just replaced with nothing for
> proper CSV syntax; there should just the comma for the next field. I
> worked around this with
>
> copy pglog from '/opt/pgsql/testlog.csv' with CSV null as 'NULL';
>
>


I missed that before - yes it should be fixed.

cheers

andrew

---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faq

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-18-2008, 10:07 AM
Tom Lane
 
Posts: n/a
Default Re: COPY-able csv log outputs

Greg Smith <gsmith@gregsmith.com> writes:
> The most fundamental issue I have with the interface is that using COPY
> makes it difficult to put any unique index on the resulting table. I like
> to have a unique index on my imported log table because it rejects the
> dupe records if you accidentally import the same section of log file
> twice. COPY tosses the whole thing if there's an index violation, which
> is a problem during a regular import because you will occasionally come
> across lines with the same timestamp that are similar in every way except
> for their statment; putting an index on the timestamp+statement seems
> impractical.


Essentially the above is arguing that you want a unique index but you
can't be bothered to invent an actually-unique key. This doesn't seem
a sound argument to me. If we need a unique key, let's find one.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-18-2008, 10:08 AM
Greg Smith
 
Posts: n/a
Default Re: COPY-able csv log outputs

On Sun, 20 May 2007, Andrew Dunstan wrote:

> Does the format not include the per-process line number?


It does not, and I never noticed that under the prefix
possibilities---never seemed import before! The combination of
timestamp/pid/line (%t %p %l) looks like a useful and unique key here, so
I'll add another column for the line number to the output. Thanks for
pointing that out, I can finish cleaning up all the functional
implementation work on this patch myself now.

--
* Greg Smith gsmith@gregsmith.com http://www.gregsmith.com Baltimore, MD

---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faq

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-18-2008, 10:09 AM
Greg Smith
 
Posts: n/a
Default Re: COPY-able csv log outputs

On Sun, 20 May 2007, Andrew Dunstan wrote:

>> I've had a preference for INSERT from the beginning here that this
>> reinforces.

> COPY is our standard bulk insert mechanism. I think arguing against it would
> be a very hard sell.


Let me say my final peace on this subject...if I considered this data to
be strictly bulk insert, then I'd completely agree here. Most of the
really interesting applications I was planning to build on top of this
mechanism are more interactive than that though. Here's a sample:

-Write a daemon that lives on the server, connects to a logging database,
and pops into an idle loop based on LISTEN.
-A client app wants to see the recent logs files. It uses NOTIFY to ask
the daemon for them and LISTENs for a response.
-The daemon wakes up, reads all the log files since it last did something,
and appends those records to the log file table. It sends out a NOTIFY to
say the log file table is current.

That enables remote clients to grab the log files from the server whenever
they please, so they can actually monitor themselves. Benchmarking is the
initial app I expect to call this, and with some types of tests I expect
the daemon to be importing every 10 minutes or so.

Assuming a unique index on the data to prevent duplication is a required
feature, I can build this using the COPY format logs as well, but that
requires I either a) am 100% perfect in making sure I never pass over the
same data twice, which is particularly annoying when the daemon gets
restarted, or b) break the COPY into single lines and insert them one at a
time, at which point I'm not bulk loading at all. If these were INSERT
statements instead, I'd have a lot more tolerance for error, because the
worst problem I'd ever run into is spewing some unique key violation
errors into the logs if I accidentally imported too much. With COPY, any
mistake or synchronization issue and I lose the whole import.

I don't mean to try and stir this back up again as an argument
(particularly not on this list). There are plenty of other apps where
COPY is clearly the best approach, you can easily make a case that my app
is a fringe application rather than a mainstream one, and on the balance
this job is still far easier than my current approach of parsing the logs.
I just wanted to give a sample of how using COPY impacts the dynamics of
how downstream applications will have to work with this data, so you can
see that my contrary preference isn't completely random here.

--
* Greg Smith gsmith@gregsmith.com http://www.gregsmith.com Baltimore, MD

---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faq

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 04-18-2008, 10:11 AM
Andrew Dunstan
 
Posts: n/a
Default Re: COPY-able csv log outputs



Greg Smith wrote:
> The attached patch fixes all the issues I found in the original
> version of this code and completes the review I wanted to do. Someone
> else will need to take this from here. As I already mentioned, I
> can't comment on the quality of the piping implementation used to add
> this feature other than to say it worked for me.


I'll take it from here.


>
>
> -Added a new documentation section to the logging chapter devoted just
> to the csvlog feature. It gives a sample table and import syntax. I
> also gave recommendations on how to configure some related log file
> parameters that can interact badly with this feature. For example, I
> noticed that if log_rotation_size was set to a value, it could split
> the CSV lines in two; the result was two CVS files you couldn't import
> because of the partial lines in each. Since the rotation size feature
> causes other issues anyway that make importing more complicated,
> documenting the issue seemed sufficient.
>
>


What are the other issues? I'm not happy about producing files with
split lines.

cheers

andrew

---------------------------(end of broadcast)---------------------------
TIP 7: You can help support the PostgreSQL project by donating at

http://www.postgresql.org/about/donate

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 04-18-2008, 10:11 AM
Greg Smith
 
Posts: n/a
Default Re: COPY-able csv log outputs

On Fri, 1 Jun 2007, Andrew Dunstan wrote:

> Greg Smith wrote:
>> Since the rotation size feature causes other issues anyway
>> that make importing more complicated, documenting the issue seemed
>> sufficient.

>
> What are the other issues? I'm not happy about producing files with split
> lines.


Just that it's fairly simple and predictable to know what your log files
are going to be named and when they'll rollover if you use something like
a date-based naming convention--but when you add size-based rotation into
the mix figuring out what files you need to import and when you should
import them gets more complicated.

Clearly fixing this issue altogether would be better, and I gather the
problem may extend to any time there is a switch to a new log file; my
"workaround" doesn't appear good enough anyway. I'm very glad I caught
and mentioned this now.

Because of the extra naming/import complexity, it still might be
worthwhile to suggest people not combine size-based rotation and the
csvlog though.

--
* Greg Smith gsmith@gregsmith.com http://www.gregsmith.com Baltimore, MD

---------------------------(end of broadcast)---------------------------
TIP 2: Don't 'kill -9' the postmaster

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 04-18-2008, 10:12 AM
Andrew Dunstan
 
Posts: n/a
Default Re: COPY-able csv log outputs



Greg Smith wrote:
> The attached patch fixes all the issues I found in the original
> version of this code and completes the review I wanted to do. Someone
> else will need to take this from here. As I already mentioned, I
> can't comment on the quality of the piping implementation used to add
> this feature other than to say it worked for me.


The code below strikes me as being seriously broken.

The comment says that it doubles all single quotes, double quotes and
backslashes. It doesn't (backslashes are not doubled) and in any case
doing that would simply be wrong (with or without backslashes). The ONLY
things that should be doubled are double quotes. Period.

That part is easy to fix.

But here's my question: why are we worrying at all about things like the
encoding? Especially, why are we worrying about the *client* encoding
for a server log file? We surely aren't going to switch encodings in the
middle of a log file! ISTM that this routine should simply copy the
input, byte for byte, apart from doubling the dquotes. Does that make
sense? I assume that this routine has been copied from somewhere else
without sufficient regard to the context (or the logic).

The code also illustrates something else that annoyed me elsewhere in
the patch and that I have eliminated, namely use of a macro placed in
the header file and then used in exactly one place in the code. The
macro didn't make anything clearer - quite the reverse. ISTM that a
macro used only in one file should be defined in that file, generally,
and if it's used only once is probably not much use anyway.

cheers

andrew


> +
> + /*
> + * Escapes special characters in the string to conform
> + * with the csv type output.
> + * Replaces " with "", ' with '' and \ with \\.
> + */
> + static size_t
> + escape_string_literal(char *to, const char *from)
> + {
> + const char *source = from;
> + char *target = to;
> + size_t remaining = 0;
> + int client_encoding = 0;
> +
> + if (from == NULL)
> + return remaining;
> +
> + remaining = strlen(from);
> + client_encoding = pg_get_client_encoding();
> +
> + while (remaining > 0 && *source != '\0')
> + {
> + char c = *source;
> + int len;
> + int i;
> +
> + /* Fast path for plain ASCII */
> + if (!IS_HIGHBIT_SET(c))
> + {
> + /* Apply quoting if needed */
> + if (CSV_STR_DOUBLE(c, false))
> + *target++ = c;
> + /* Copy the character */
> + *target++ = c;
> + source++;
> + remaining--;
> + continue;
> + }
> +
> + /* Slow path for possible multibyte characters */
> + len = pg_encoding_mblen(client_encoding, source);
> +
> + /* Copy the character */
> + for (i = 0; i < len; i++)
> + {
> + if (remaining == 0 || *source == '\0')
> + break;
> + *target++ = *source++;
> + remaining--;
> + }
> +
> + /*
> + * If we hit premature end of string (ie, incomplete multibyte
> + * character), try to pad out to the correct length with spaces.
> + * We may not be able to pad completely, but we will always be
> + * able to insert at least one pad space (since we'd not have
> + * quoted a multibyte character). This should be enough to make
> + * a string that the server will error out on.
> + */
> + if (i < len)
> + {
> + for (; i < len; i++)
> + {
> + if (((size_t) (target - to)) / 2 >= strlen(from))
> + break;
> + *target++ = ' ';
> + }
> + break;
> + }
> + }
> +
> + /* Write the terminating NUL character. */
> + *target = '\0';
> +
> + return target - to;
> + }
>
>


---------------------------(end of broadcast)---------------------------
TIP 1: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 01:03 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
UnixAdminTalk.com

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650