question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Surface cursor.rowcount in PostgresOperator

See original GitHub issue

Description

The Cursor object in psycopg2 provides a useful feature of logging how many rows were affected by the last execute. https://www.psycopg.org/docs/cursor.html#cursor.rowcount This can be added as a parameter to PostgresOperator and passed through to DbApiHook.run to log affected row after each query.

The operator parameter could be a verbose flag that when enabled logs the SQL statement before running it and the rows affected after.

Example of conn.cursor logging:

>>> sql_statements = ['select 1;',
...                   'insert into jeklund.rowtest (player_id) select 1;',
...                   'select now()::date - generate_series(0, 59);']
>>> with closing(conn.cursor()) as cur:
...     for sql_statement in sql_statements:
...             print('Running {}'.format(sql_statement))
...             cur.execute(sql_statement)
...             print('Affected rows: {}'.format(cur.rowcount))
...
Running select 1;
Affected rows: 1
Running insert into jeklund.rowtest (player_id) select 1;
Affected rows: 1
Running select now()::date - generate_series(0, 59);
Affected rows: 60

Use case / motivation

Having rows affected in Airflow task logs is useful for monitoring and debugging multi step ETLs. Making it easy to spot cases where e.g. an INSERT query unexpectedly inserted 0 rows.

Related Issues

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
cjseklcommented, Jul 15, 2020

Hi @cjsekl would you like to work on this feature?

Yes, I was planning on using this for the workshop.

0reactions
turbaszekcommented, Jul 20, 2020

Closing as this was done in #9841

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cursor constantly disappearing using Surface 6 touchpad
Cursor keeps disappearing on my Surface Pro 6 touchpad. Often I need to restart the computer but it keeps happening.
Read more >
How-to Guide for PostgresOperator - Apache Airflow
The purpose of Postgres Operator is to define tasks involving interactions with a PostgreSQL database. In Airflow-2.0 , the PostgresOperator class resides ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found