Re: Refactor handling of "-only" options in pg_dump, pg_restore

From: Steven Niu <niushiji(at)gmail(dot)com>
To: jian he <jian(dot)universality(at)gmail(dot)com>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Refactor handling of "-only" options in pg_dump, pg_restore
Date: 2026-03-02 09:32:30
Message-ID: MN2PR15MB3021734DD0328760C54A4E96A77EA@MN2PR15MB3021.namprd15.prod.outlook.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

From: jian he <jian(dot)universality(at)gmail(dot)com>
Sent: Monday, March 02, 2026 12:57
To: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Refactor handling of "-only" options in pg_dump, pg_restore

Hi.

-------------------------------<<<<<<<
    /* reject conflicting "-only" options */
    if (data_only && schema_only)
        pg_fatal("options %s and %s cannot be used together",
                 "-s/--schema-only", "-a/--data-only");
    if (schema_only && statistics_only)
        pg_fatal("options %s and %s cannot be used together",
                 "-s/--schema-only", "--statistics-only");
    if (data_only && statistics_only)
        pg_fatal("options %s and %s cannot be used together",
                 "-a/--data-only", "--statistics-only");

    /* reject conflicting "-only" options */
    if (data_only && with_statistics)
        pg_fatal("options %s and %s cannot be used together",
                 "-a/--data-only", "--statistics");
    if (schema_only && with_statistics)
        pg_fatal("options %s and %s cannot be used together",
                 "-s/--schema-only", "--statistics");
-------------------------------<<<<<<<
The above is from src/bin/pg_dump/pg_dump.c, this is too much.

We can just use two IF statements:
    if (data_only && (schema_only || with_statistics || statistics_only))
        pg_fatal("options %s and %s cannot be used together",
                 "-a/--data-only",
                 schema_only ? "-s/--schema-only" :
                 with_statistics ? "--statistics" :
                 "--statistics-only");

    if (schema_only && (with_statistics || statistics_only))
        pg_fatal("options %s and %s cannot be used together",
                 "-s/--schema-only",
                 with_statistics ? "--statistics" :
                 "--statistics-only");

First "if (data_only && (schema_only" implies that the second IF check
won't have a combination
of `` if (schema_only && (data_only``.
Maybe we can use ELSE IF here.

We can do the same thing for pg_restore.c

--
jian
https://www.enterprisedb.com/

Hi, jian,

Your change makes the code cleaner and easier to read. I like the idea.
Also I applied your patch locally and the make passed, "make check" in the src/bin/pg_dump also passed.

Regards,
Steven

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Chao Li 2026-03-02 09:32:55 Question: rebuilding frontend tools after libpgfeutils.a changes?
Previous Message Chao Li 2026-03-02 09:22:29 Re: DOC: fixes multiple errors in alter table doc