graflo.db.postgres.bulk¶
PostgreSQL bulk writer: COPY into tables whose keys are built after the load.
Vertex tables are created without primary keys and edge tables without foreign
keys or their unique key. Every chunk is one transaction: its COPY and a
row naming it in a load-log table, so a chunk in doubt is found there when
replayed. The primary keys are added once every vertex is written; the unique
edge keys, then foreign keys (NOT VALID, then validated), at the end.
Values are written as the text the upsert path's statements leave in each column, so both paths store the same graph.
Attributes¶
INDEX_BUILD_MEMORY = '1GB'
module-attribute
¶
LOAD_LOG = '_graflo_bulk_log'
module-attribute
¶
logger = logging.getLogger(__name__)
module-attribute
¶
Classes¶
PostgresBulkMixin
¶
Bases: PostgresTargetWriteMixin
The bulk-load protocol of :mod:graflo.db.bulk on PostgreSQL.
Source code in graflo/db/postgres/bulk.py
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 | |
Attributes¶
supports_bulk_load = True
class-attribute
instance-attribute
¶
Methods:¶
bulk_begin(schema, cfg, *, recreate=False, resume=False)
¶
Create the namespace, keyless tables and an empty load log.
Raises:
| Type | Description |
|---|---|
SchemaExistsError
|
The namespace holds a table of schema and neither recreate nor resume is set. |
Source code in graflo/db/postgres/bulk.py
bulk_finish(schema)
¶
Build the edge keys, indexes and foreign keys; count; drop the load log.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
An edge table holds two rows with one key. |
Error
|
An edge names a vertex that is not stored. |
Source code in graflo/db/postgres/bulk.py
bulk_prepare_edges(schema)
¶
Add the vertex primary keys, which the foreign keys will reference.
Raises:
| Type | Description |
|---|---|
Error
|
Two rows share an identity. |
Source code in graflo/db/postgres/bulk.py
bulk_write_edges(schema, chunk)
¶
Source code in graflo/db/postgres/bulk.py
bulk_write_vertices(schema, chunk)
¶
Source code in graflo/db/postgres/bulk.py
SessionText
¶
How one session prints values: its time zone and whether DateStyle is ISO.
Source code in graflo/db/postgres/bulk.py
Functions:¶
add_foreign_keys_sql(schema, table, keys)
¶
One ALTER TABLE adding keys unchecked, so they are validated apart.
Source code in graflo/db/postgres/bulk.py
column_text(value, *, array=False, tz=None, iso=True)
¶
The text COPY takes for value in a column, None for NULL.
It is what the column holds after the upsert path's INSERT of the
value psycopg2 adapts: numbers, booleans, dates and times printed as
PostgreSQL prints them, lists as array literals.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The value. |
required |
array
|
bool
|
The column is an array column. |
False
|
tz
|
tzinfo | None
|
The session time zone, for a datetime with a time zone. |
None
|
iso
|
bool
|
The session's DateStyle is ISO. |
True
|
Raises:
| Type | Description |
|---|---|
TypeError
|
psycopg2 cannot adapt the value. |
ValueError
|
A date or time the session would print differently. |
Source code in graflo/db/postgres/bulk.py
copy_sql(schema, table, columns)
¶
COPY of CSV rows into columns of table.
Source code in graflo/db/postgres/bulk.py
csv_text(rows)
¶
rows as CSV for COPY ... (FORMAT csv): every value quoted, NULL empty.
Source code in graflo/db/postgres/bulk.py
edge_copy_rows(chunk, arrays, session)
¶
Columns and text rows of an edge chunk: endpoint identity values, then properties.
Endpoints are stored by their identity values, as the upsert path stores them; an edge lacking one is dropped, as there.
Source code in graflo/db/postgres/bulk.py
load_log_ddl(schema)
¶
DROP and CREATE of the load-log table.
Source code in graflo/db/postgres/bulk.py
log_sql(schema)
¶
logged_sql(schema)
¶
should_write(replay, logged)
¶
vertex_copy_rows(chunk, arrays, session)
¶
Columns and text rows of a vertex chunk.
Columns are every field a row carries, less _-prefixed ones, as the
upsert path writes; a row lacking one writes NULL. A row lacking a key
field is dropped, as the upsert path drops it.