Github user danielvdende commented on a diff in the pull request:

    https://github.com/apache/spark/pull/20057#discussion_r168943159
  
    --- Diff: 
sql/core/src/main/scala/org/apache/spark/sql/jdbc/PostgresDialect.scala ---
    @@ -85,15 +85,24 @@ private object PostgresDialect extends JdbcDialect {
         s"SELECT 1 FROM $table LIMIT 1"
       }
     
    +  override def isCascadingTruncateTable(): Option[Boolean] = Some(false)
    +
       /**
    -  * The SQL query used to truncate a table. For Postgres, the default 
behaviour is to
    -  * also truncate any descendant tables. As this is a (possibly unwanted) 
side-effect,
    -  * the Postgres dialect adds 'ONLY' to truncate only the table in question
    -  * @param table The name of the table.
    -  * @return The SQL query to use for truncating a table
    -  */
    -  override def getTruncateQuery(table: String): String = {
    -    s"TRUNCATE TABLE ONLY $table"
    +   * The SQL query used to truncate a table. For Postgres, the default 
behaviour is to
    +   * also truncate any descendant tables. As this is a (possibly unwanted) 
side-effect,
    +   * the Postgres dialect adds 'ONLY' to truncate only the table in 
question
    +   * @param table The table to truncate
    +   * @param cascade Whether or not to cascade the truncation. Default 
value is the value of
    +   *                isCascadingTruncateTable()
    +   * @return The SQL query to use for truncating a table
    +   */
    +  override def getTruncateQuery(
    +      table: String,
    +      cascade: Option[Boolean] = isCascadingTruncateTable): String = {
    +    cascade match {
    +      case Some(true) => s"TRUNCATE TABLE ONLY $table CASCADE"
    --- End diff --
    
    Sure, I made a quick example, as you can see using `TRUNCATE TABLE ONLY 
$table CASCADE` will truncate the foreign key-ed table, but leave the 
inheritance relationship intact:
    ```postgres=# CREATE SCHEMA test;
    CREATE SCHEMA
    postgres=# CREATE TABLE parent(a INT);
    CREATE TABLE
    postgres=# ALTER TABLE parent ADD CONSTRAINT some_constraint PRIMARY KEY(a);
    ALTER TABLE
    postgres=# CREATE TABLE child(b INT) INHERITS (parent);
    CREATE TABLE
    postgres=# CREATE TABLE forkey(c INT);
    CREATE TABLE
    postgres=# ALTER TABLE forkey ADD FOREIGN KEY(c) REFERENCES parent(a);
    ALTER TABLE
    
    postgres=# INSERT INTO parent VALUES(1);
    INSERT 0 1
    postgres=# select * from parent;
     a
    ---
     1
    (1 row)
    
    postgres=# select * from child;
     a | b
    ---+---
    (0 rows)
    
    postgres=# INSERT INTO child VALUES(2);
    INSERT 0 1
    postgres=# select * from child;
     a | b
    ---+---
     2 |
    (1 row)
    
    postgres=# select * from parent;
     a
    ---
     1
     2
    (2 rows)
    
    postgres=# INSERT INTO forkey VALUES(1);
    INSERT 0 1
    postgres=# select * from forkey;
     c
    ---
     1
    (1 row)
    
    postgres=# TRUNCATE TABLE ONLY parent CASCADE;
    NOTICE:  truncate cascades to table "forkey"
    TRUNCATE TABLE
    postgres=# select * from parent;
     a
    ---
     2
    (1 row)
    
    postgres=# select * from child;
     a | b
    ---+---
     2 |
    (1 row)
    
    postgres=# select * from forkey;
     c
    ---
    (0 rows)
    ```


---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to