dabla commented on issue #77606: URL: https://github.com/apache/airflow/issues/77606#issuecomment-7293991191
Thanks for writing this up. @eladkal pointed me here because it touches AIP-132, which I am working on. The use case is valid: `GenericTransfer` materialises rows on both sides ([docstring](https://github.com/apache/airflow/blob/94a793c2a667499c84b4a585ee697e44eca46906/providers/common/sql/src/airflow/providers/common/sql/operators/generic_transfer.py#L36-L42)), and for Postgres to Postgres nothing beats COPY. I would not add a `PostgresToPostgresOperator` for it though, and I think the name gives the wrong impression: that a fast transfer between two databases needs an operator of its own for that pair. That is how the providers ended up with 86 pair-specific transfer operators, each solving the same problem again and each to be fixed separately when a bug shows up, and a `PostgresToPostgresOperator` invites the MySQL to MySQL and MSSQL to MSSQL ones next. [AIP-132](https://cwiki.apache.org/confluence/spaces/AIRFLOW/pages/467436039) ([devlist thread](https://lists.apache.org/thread.html/5nl6g6oosr5sg69nrfy151fwy5b6j5zf)) goes the other way: the capability lives in the hook, and an operator only connects two hooks. What is missing here is not an operator but a capability of `PostgresHook`, and you can get there in two steps. The first step needs no change in Airflow. `PostgresHook` already runs COPY through [`copy_expert`](https://github.com/apache/airflow/blob/94a793c2a667499c84b4a585ee697e44eca46906/providers/postgres/src/airflow/providers/postgres/hooks/postgres.py#L391-L438), with [`bulk_load` and `bulk_dump`](https://github.com/apache/airflow/blob/94a793c2a667499c84b4a585ee697e44eca46906/providers/postgres/src/airflow/providers/postgres/hooks/postgres.py#L448-L454) on top, but those only take a file name. So for a direct stream a Python task takes the connections from the two hooks and runs your loop, roughly like this (a sketch, not run): ```python @task def copy_events(ds=None): source = PostgresHook(postgres_conn_id="src") destination = PostgresHook(postgres_conn_id="dst") with source.get_conn() as src, destination.get_conn() as dst: with ( src.cursor().copy(f"COPY (SELECT * FROM events WHERE dt = '{ds}') TO STDOUT") as reader, dst.cursor().copy("COPY staging.events FROM STDIN") as writer, ): for chunk in reader: writer.write(chunk) ``` The connections come from `get_conn` (psycopg 3 connections where the provider runs on psycopg 3), so IAM auth and connection extras keep working, which the libpq string built in the operator would lose. The truncate before and the move from staging to the main table after stay ordinary SQL tasks, each in its own transaction, as you do today. The second step is the quick win I would propose as a PR: move that loop into `PostgresHook` as a method, something like `copy_to` (the name and signature are for the PR to settle). The logic then lives in the hook, where it is tested and fixed once and every Dag author gets it, and the task becomes a single call: ```python @task def copy_events(ds=None): PostgresHook(postgres_conn_id="src").copy_to( PostgresHook(postgres_conn_id="dst"), sql=f"SELECT * FROM events WHERE dt = '{ds}'", table="staging.events", ) ``` An operator can follow once that exists, and it should not be Postgres-specific. `DbApiHook` already has [`bulk_dump` and `bulk_load`](https://github.com/apache/airflow/blob/94a793c2a667499c84b4a585ee697e44eca46906/providers/common/sql/src/airflow/providers/common/sql/hooks/sql.py#L1085-L1101) as a contract (MySQL implements them too), and [`SQLBulkLoadOperator`](https://github.com/apache/airflow/blob/94a793c2a667499c84b4a585ee697e44eca46906/providers/common/sql/src/airflow/providers/common/sql/operators/sql.py#L1942-L2012) is the generic operator on it since #69362: it delegates to the hook's native bulk mechanism, whatever the database. Today it loads a local file; it looks like a good fit to also take another hook as its source, so that a database to database bulk load becomes one more case of an operator that exists instead of a new one per pair. Which formats two different engines can agree on (raw COPY pass-through only works within the PostgreSQL family) is the part to sett le once in the AIP. For completeness: @aoelvp94 is working on an Arrow/ADBC path for `GenericTransfer`, which removes the row conversion between any two ADBC drivers. That does not help Postgres yet: PostgreSQL has no way to speak Arrow natively on the wire, so its ADBC driver still converts internally, and in my tests it was slower than psycopg 3 (@zeroshade can you confirm?). Until then, for Postgres COPY remains the faster route, so the two are complementary. Would you be up for the second step, the `copy_to` in `PostgresHook`? Happy to review it, and your production experience (Greenplum included) would be welcome in the AIP discussion on the operator. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
