pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Avoid useless retrieval of defaults and check constraints in pg_dump -a
11+ messages / 5 participants
[nested] [flat]

Commits from this thread:
  2020-09-16 5423853fee  Avoid retrieval of CHECK constraints and DEFAULT exprs in data-only dump
* Avoid useless retrieval of defaults and check constraints in pg_dump -a
@ 2020-07-12 05:48 Julien Rouhaud <rjuju123@gmail.com>
  2020-07-12 14:29 ` Re: Avoid useless retrieval of defaults and check constraints in pg_dump -a Justin Pryzby <pryzby@telsasoft.com>
  2020-09-10 12:31 ` Re: Avoid useless retrieval of defaults and check constraints in pg_dump -a Daniel Gustafsson <daniel@yesql.se>
  0 siblings, 2 replies; 11+ messages in thread

From: Julien Rouhaud @ 2020-07-12 05:48 UTC (permalink / raw)
  To: pgsql-hackers@lists.postgresql.org

Hi,

Currently, getTableAttrs() always retrieves info about columns defaults and
check constraints, while this will never be used if --data-only option if used.
This seems like a waste of resources, so here's a patch to skip those parts
when the DDL won't be generated.

Attachments:

v1-pg_dump_data_only.diff (text/plain, 689B) download diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c index e758b5c50a..e4ea604dfe 100644 --- a/src/bin/pg_dump/pg_dump.c +++ b/src/bin/pg_dump/pg_dump.c @@ -8590,7 +8590,7 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables) /* * Get info about column defaults */ - if (hasdefaults) + if (!dopt->dataOnly && hasdefaults) { AttrDefInfo *attrdefs; int numDefaults; @@ -8677,7 +8677,7 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables) /* * Get info about table CHECK constraints */ - if (tbinfo->ncheck > 0) + if (!dopt->dataOnly && tbinfo->ncheck > 0) { ConstraintInfo *constrs; int numConstrs;
^ permalink raw reply [nested|flat] 11+ messages in thread

* Re: Avoid useless retrieval of defaults and check constraints in pg_dump -a
  2020-07-12 05:48 Avoid useless retrieval of defaults and check constraints in pg_dump -a Julien Rouhaud <rjuju123@gmail.com>
@ 2020-07-12 14:29 ` Justin Pryzby <pryzby@telsasoft.com>
  2020-07-14 09:14   ` Re: Avoid useless retrieval of defaults and check constraints in pg_dump -a Julien Rouhaud <rjuju123@gmail.com>
  1 sibling, 1 reply; 11+ messages in thread

From: Justin Pryzby @ 2020-07-12 14:29 UTC (permalink / raw)
  To: Julien Rouhaud <rjuju123@gmail.com>; +Cc: pgsql-hackers@lists.postgresql.org

On Sun, Jul 12, 2020 at 07:48:50AM +0200, Julien Rouhaud wrote:
> Currently, getTableAttrs() always retrieves info about columns defaults and
> check constraints, while this will never be used if --data-only option if used.
> This seems like a waste of resources, so here's a patch to skip those parts
> when the DDL won't be generated.

Note that the speed of default and constraint handling has come up before:
https://www.postgresql.org/message-id/flat/CAMkU%3D1xPqHP%3D7YPeChq6n1v_qd4WGf%2BZvtnR-b%2BgyzFqtJqM...
https://www.postgresql.org/message-id/CAMkU=1x-e+maqefhM1yMeSiJ8J9Z+SJHgW7c9bqo3E3JMG4iJA@mail.gmail...

I'd pointed out that a significant fraction of our pg_upgrade time was in
pg_dump, due to having wide tables with many child tables, and "default 0" on
every column.  (I've since dropped our defaults so this is no longer an issue
here).

It appears your patch would avoid doing unnecessary work in the --data-only
case, but it wouldn't help the pg_upgrade case.

-- 
Justin





^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: Avoid useless retrieval of defaults and check constraints in pg_dump -a
  2020-07-12 05:48 Avoid useless retrieval of defaults and check constraints in pg_dump -a Julien Rouhaud <rjuju123@gmail.com>
  2020-07-12 14:29 ` Re: Avoid useless retrieval of defaults and check constraints in pg_dump -a Justin Pryzby <pryzby@telsasoft.com>
@ 2020-07-14 09:14   ` Julien Rouhaud <rjuju123@gmail.com>
  2020-09-15 02:33     ` Re: Avoid useless retrieval of defaults and check constraints in pg_dump -a Michael Paquier <michael@paquier.xyz>
  0 siblings, 1 reply; 11+ messages in thread

From: Julien Rouhaud @ 2020-07-14 09:14 UTC (permalink / raw)
  To: Justin Pryzby <pryzby@telsasoft.com>; +Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>

On Sun, Jul 12, 2020 at 4:29 PM Justin Pryzby <pryzby@telsasoft.com> wrote:
>
> On Sun, Jul 12, 2020 at 07:48:50AM +0200, Julien Rouhaud wrote:
> > Currently, getTableAttrs() always retrieves info about columns defaults and
> > check constraints, while this will never be used if --data-only option if used.
> > This seems like a waste of resources, so here's a patch to skip those parts
> > when the DDL won't be generated.
>
> Note that the speed of default and constraint handling has come up before:
> https://www.postgresql.org/message-id/flat/CAMkU%3D1xPqHP%3D7YPeChq6n1v_qd4WGf%2BZvtnR-b%2BgyzFqtJqM...
> https://www.postgresql.org/message-id/CAMkU=1x-e+maqefhM1yMeSiJ8J9Z+SJHgW7c9bqo3E3JMG4iJA@mail.gmail...

Oh, I wasn't aware of that.

> I'd pointed out that a significant fraction of our pg_upgrade time was in
> pg_dump, due to having wide tables with many child tables, and "default 0" on
> every column.  (I've since dropped our defaults so this is no longer an issue
> here).
>
> It appears your patch would avoid doing unnecessary work in the --data-only
> case, but it wouldn't help the pg_upgrade case.

Indeed.  Making the schema part faster would probably require a bigger
refactoring.  I'm wondering if we could introduce some facility to
temporarily deny any DDL change, so that the initial pg_dump -s done
by pg_upgrade can be performed before shutting down the instance.

Note that those extraneous queries were found while trying to dump
data out of a corrupted database.  The issue wasn't an excessive
runtime but corrupted catalog entries, so bypassing this code (since I
was only interested in the data anyway) was simpler than trying to
recover yet other corrupted rows.





^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: Avoid useless retrieval of defaults and check constraints in pg_dump -a
  2020-07-12 05:48 Avoid useless retrieval of defaults and check constraints in pg_dump -a Julien Rouhaud <rjuju123@gmail.com>
  2020-07-12 14:29 ` Re: Avoid useless retrieval of defaults and check constraints in pg_dump -a Justin Pryzby <pryzby@telsasoft.com>
  2020-07-14 09:14   ` Re: Avoid useless retrieval of defaults and check constraints in pg_dump -a Julien Rouhaud <rjuju123@gmail.com>
@ 2020-09-15 02:33     ` Michael Paquier <michael@paquier.xyz>
  2020-09-15 02:56       ` Re: Avoid useless retrieval of defaults and check constraints in pg_dump -a Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 11+ messages in thread

From: Michael Paquier @ 2020-09-15 02:33 UTC (permalink / raw)
  To: Julien Rouhaud <rjuju123@gmail.com>; +Cc: Justin Pryzby <pryzby@telsasoft.com>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>

On Tue, Jul 14, 2020 at 11:14:50AM +0200, Julien Rouhaud wrote:
> Note that those extraneous queries were found while trying to dump
> data out of a corrupted database.  The issue wasn't an excessive
> runtime but corrupted catalog entries, so bypassing this code (since I
> was only interested in the data anyway) was simpler than trying to
> recover yet other corrupted rows.

Yeah, I don't see actually why this argument can prevent us from doing
a micro optimization if it proves to work correctly.
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: Avoid useless retrieval of defaults and check constraints in pg_dump -a
  2020-07-12 05:48 Avoid useless retrieval of defaults and check constraints in pg_dump -a Julien Rouhaud <rjuju123@gmail.com>
  2020-07-12 14:29 ` Re: Avoid useless retrieval of defaults and check constraints in pg_dump -a Justin Pryzby <pryzby@telsasoft.com>
  2020-07-14 09:14   ` Re: Avoid useless retrieval of defaults and check constraints in pg_dump -a Julien Rouhaud <rjuju123@gmail.com>
  2020-09-15 02:33     ` Re: Avoid useless retrieval of defaults and check constraints in pg_dump -a Michael Paquier <michael@paquier.xyz>
@ 2020-09-15 02:56       ` Tom Lane <tgl@sss.pgh.pa.us>
  2020-09-15 03:08         ` Re: Avoid useless retrieval of defaults and check constraints in pg_dump -a Michael Paquier <michael@paquier.xyz>
  0 siblings, 1 reply; 11+ messages in thread

From: Tom Lane @ 2020-09-15 02:56 UTC (permalink / raw)
  To: Michael Paquier <michael@paquier.xyz>; +Cc: Julien Rouhaud <rjuju123@gmail.com>; Justin Pryzby <pryzby@telsasoft.com>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>

Michael Paquier <michael@paquier.xyz> writes:
> On Tue, Jul 14, 2020 at 11:14:50AM +0200, Julien Rouhaud wrote:
>> Note that those extraneous queries were found while trying to dump
>> data out of a corrupted database.  The issue wasn't an excessive
>> runtime but corrupted catalog entries, so bypassing this code (since I
>> was only interested in the data anyway) was simpler than trying to
>> recover yet other corrupted rows.

> Yeah, I don't see actually why this argument can prevent us from doing
> a micro optimization if it proves to work correctly.

The main thing I'm wondering about is whether not fetching these objects
could lead to failing to detect an important dependency chain.  IIRC,
pg_dump simply ignores pg_depend entries that mention objects it has not
loaded, so there is a possible mechanism for that.  However, it's hard to
see how a --data-only dump could end up choosing an invalid dump order on
that basis.  It doesn't seem like safe load orders for the table data
objects could depend on what is referenced by defaults or CHECK
constraints.

But ... I've only spent a few minutes thinking about it, so maybe
I'm missing something.

(Note that we disallow sub-queries in CHECK constraints, and also
disclaim responsibility for what happens if you cheat by hiding
the subquery in a function.  So while it's certainly possible to
build CHECK constraints that only work if table X is loaded before
table Y, pg_dump already doesn't guarantee that'll work, --data-only
or otherwise.)

			regards, tom lane





^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: Avoid useless retrieval of defaults and check constraints in pg_dump -a
  2020-07-12 05:48 Avoid useless retrieval of defaults and check constraints in pg_dump -a Julien Rouhaud <rjuju123@gmail.com>
  2020-07-12 14:29 ` Re: Avoid useless retrieval of defaults and check constraints in pg_dump -a Justin Pryzby <pryzby@telsasoft.com>
  2020-07-14 09:14   ` Re: Avoid useless retrieval of defaults and check constraints in pg_dump -a Julien Rouhaud <rjuju123@gmail.com>
  2020-09-15 02:33     ` Re: Avoid useless retrieval of defaults and check constraints in pg_dump -a Michael Paquier <michael@paquier.xyz>
  2020-09-15 02:56       ` Re: Avoid useless retrieval of defaults and check constraints in pg_dump -a Tom Lane <tgl@sss.pgh.pa.us>
@ 2020-09-15 03:08         ` Michael Paquier <michael@paquier.xyz>
  0 siblings, 0 replies; 11+ messages in thread

From: Michael Paquier @ 2020-09-15 03:08 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Julien Rouhaud <rjuju123@gmail.com>; Justin Pryzby <pryzby@telsasoft.com>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>

On Mon, Sep 14, 2020 at 10:56:01PM -0400, Tom Lane wrote:
> (Note that we disallow sub-queries in CHECK constraints, and also
> disclaim responsibility for what happens if you cheat by hiding
> the subquery in a function.  So while it's certainly possible to
> build CHECK constraints that only work if table X is loaded before
> table Y, pg_dump already doesn't guarantee that'll work, --data-only
> or otherwise.)

Yep, exactly what I was thinking upthread by cheating with a schema
having cross-table references in a check constraint.
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: Avoid useless retrieval of defaults and check constraints in pg_dump -a
  2020-07-12 05:48 Avoid useless retrieval of defaults and check constraints in pg_dump -a Julien Rouhaud <rjuju123@gmail.com>
@ 2020-09-10 12:31 ` Daniel Gustafsson <daniel@yesql.se>
  2020-09-15 02:48   ` Re: Avoid useless retrieval of defaults and check constraints in pg_dump -a Michael Paquier <michael@paquier.xyz>
  1 sibling, 1 reply; 11+ messages in thread

From: Daniel Gustafsson @ 2020-09-10 12:31 UTC (permalink / raw)
  To: Julien Rouhaud <rjuju123@gmail.com>; +Cc: pgsql-hackers@lists.postgresql.org

> On 12 Jul 2020, at 07:48, Julien Rouhaud <rjuju123@gmail.com> wrote:

> Currently, getTableAttrs() always retrieves info about columns defaults and
> check constraints, while this will never be used if --data-only option if used.
> This seems like a waste of resources, so here's a patch to skip those parts
> when the DDL won't be generated.

Given how unintrusive this optimization is, +1 from me to go ahead with this
patch.  pg_dump tests pass.  Personally I would've updated the nearby comments
to reflect why the check for dataOnly is there, but MMV there.  I'm moving this
patch to Ready for Committer.

I'm fairly sure there is a lot more we can do to improve the performance of
data-only dumps, but this nicely chips away at the problem.

cheers ./daniel




^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: Avoid useless retrieval of defaults and check constraints in pg_dump -a
  2020-07-12 05:48 Avoid useless retrieval of defaults and check constraints in pg_dump -a Julien Rouhaud <rjuju123@gmail.com>
  2020-09-10 12:31 ` Re: Avoid useless retrieval of defaults and check constraints in pg_dump -a Daniel Gustafsson <daniel@yesql.se>
@ 2020-09-15 02:48   ` Michael Paquier <michael@paquier.xyz>
  2020-09-15 08:46     ` Re: Avoid useless retrieval of defaults and check constraints in pg_dump -a Julien Rouhaud <rjuju123@gmail.com>
  0 siblings, 1 reply; 11+ messages in thread

From: Michael Paquier @ 2020-09-15 02:48 UTC (permalink / raw)
  To: Daniel Gustafsson <daniel@yesql.se>; +Cc: Julien Rouhaud <rjuju123@gmail.com>; pgsql-hackers@lists.postgresql.org

On Thu, Sep 10, 2020 at 02:31:32PM +0200, Daniel Gustafsson wrote:
> Given how unintrusive this optimization is, +1 from me to go ahead with this
> patch.  pg_dump tests pass.  Personally I would've updated the nearby comments
> to reflect why the check for dataOnly is there, but MMV there.  I'm moving this
> patch to Ready for Committer.

We need two comments here.  I would suggest something like:
"Skip default/check for a data-only dump, as this is only needed for
dumps of the table schema."

Better wording is of course welcome.

> I'm fairly sure there is a lot more we can do to improve the performance of
> data-only dumps, but this nicely chips away at the problem.

I was looking at that, and wondered about cases like the following,
artistic, thing:
CREATE FUNCTION check_data_zz() RETURNS boolean
    LANGUAGE sql STABLE STRICT
    AS $$select count(a) > 0 from zz$$;
CREATE TABLE public.yy (
    a integer,
    CONSTRAINT yy_check CHECK (check_data_zz())
);
CREATE TABLE zz (a integer);
INSERT INTO zz VALUES (1);
INSERT INTO yy VALUES (1);

Even on HEAD, this causes the data load to fail because yy's data is
inserted before zz, so keeping track of the CHECK dependency could
have made sense for --data-only if we were to make a better work at
detecting the dependency between both tables and made sure that zz
data needs to appear before yy, but it is not like this would happen
easily in pg_dump, and we document it this way (see the warning about
dump/reload in ddl.sgml for check constraints).  In short, I think
that this patch looks like a good thing to have.
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: Avoid useless retrieval of defaults and check constraints in pg_dump -a
  2020-07-12 05:48 Avoid useless retrieval of defaults and check constraints in pg_dump -a Julien Rouhaud <rjuju123@gmail.com>
  2020-09-10 12:31 ` Re: Avoid useless retrieval of defaults and check constraints in pg_dump -a Daniel Gustafsson <daniel@yesql.se>
  2020-09-15 02:48   ` Re: Avoid useless retrieval of defaults and check constraints in pg_dump -a Michael Paquier <michael@paquier.xyz>
@ 2020-09-15 08:46     ` Julien Rouhaud <rjuju123@gmail.com>
  2020-09-16 13:06       ` Re: Avoid useless retrieval of defaults and check constraints in pg_dump -a Michael Paquier <michael@paquier.xyz>
  0 siblings, 1 reply; 11+ messages in thread

From: Julien Rouhaud @ 2020-09-15 08:46 UTC (permalink / raw)
  To: Michael Paquier <michael@paquier.xyz>; +Cc: Daniel Gustafsson <daniel@yesql.se>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>

On Tue, Sep 15, 2020 at 4:48 AM Michael Paquier <michael@paquier.xyz> wrote:
>
> On Thu, Sep 10, 2020 at 02:31:32PM +0200, Daniel Gustafsson wrote:
> > Given how unintrusive this optimization is, +1 from me to go ahead with this
> > patch.  pg_dump tests pass.  Personally I would've updated the nearby comments
> > to reflect why the check for dataOnly is there, but MMV there.  I'm moving this
> > patch to Ready for Committer.
>
> We need two comments here.  I would suggest something like:
> "Skip default/check for a data-only dump, as this is only needed for
> dumps of the table schema."
>
> Better wording is of course welcome.

FWIW I'm fine with those news comments!





^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: Avoid useless retrieval of defaults and check constraints in pg_dump -a
  2020-07-12 05:48 Avoid useless retrieval of defaults and check constraints in pg_dump -a Julien Rouhaud <rjuju123@gmail.com>
  2020-09-10 12:31 ` Re: Avoid useless retrieval of defaults and check constraints in pg_dump -a Daniel Gustafsson <daniel@yesql.se>
  2020-09-15 02:48   ` Re: Avoid useless retrieval of defaults and check constraints in pg_dump -a Michael Paquier <michael@paquier.xyz>
  2020-09-15 08:46     ` Re: Avoid useless retrieval of defaults and check constraints in pg_dump -a Julien Rouhaud <rjuju123@gmail.com>
@ 2020-09-16 13:06       ` Michael Paquier <michael@paquier.xyz>
  2020-09-16 13:34         ` Re: Avoid useless retrieval of defaults and check constraints in pg_dump -a Julien Rouhaud <rjuju123@gmail.com>
  0 siblings, 1 reply; 11+ messages in thread

From: Michael Paquier @ 2020-09-16 13:06 UTC (permalink / raw)
  To: Julien Rouhaud <rjuju123@gmail.com>; +Cc: Daniel Gustafsson <daniel@yesql.se>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>

On Tue, Sep 15, 2020 at 10:46:56AM +0200, Julien Rouhaud wrote:
> FWIW I'm fine with those news comments!

Okay, I got again on this one today and finished by committing the
patch as of 5423853.
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: Avoid useless retrieval of defaults and check constraints in pg_dump -a
  2020-07-12 05:48 Avoid useless retrieval of defaults and check constraints in pg_dump -a Julien Rouhaud <rjuju123@gmail.com>
  2020-09-10 12:31 ` Re: Avoid useless retrieval of defaults and check constraints in pg_dump -a Daniel Gustafsson <daniel@yesql.se>
  2020-09-15 02:48   ` Re: Avoid useless retrieval of defaults and check constraints in pg_dump -a Michael Paquier <michael@paquier.xyz>
  2020-09-15 08:46     ` Re: Avoid useless retrieval of defaults and check constraints in pg_dump -a Julien Rouhaud <rjuju123@gmail.com>
  2020-09-16 13:06       ` Re: Avoid useless retrieval of defaults and check constraints in pg_dump -a Michael Paquier <michael@paquier.xyz>
@ 2020-09-16 13:34         ` Julien Rouhaud <rjuju123@gmail.com>
  0 siblings, 0 replies; 11+ messages in thread

From: Julien Rouhaud @ 2020-09-16 13:34 UTC (permalink / raw)
  To: Michael Paquier <michael@paquier.xyz>; +Cc: Daniel Gustafsson <daniel@yesql.se>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>

On Wed, Sep 16, 2020 at 3:06 PM Michael Paquier <michael@paquier.xyz> wrote:
>
> On Tue, Sep 15, 2020 at 10:46:56AM +0200, Julien Rouhaud wrote:
> > FWIW I'm fine with those news comments!
>
> Okay, I got again on this one today and finished by committing the
> patch as of 5423853.

Thanks Michael!





^ permalink  raw  reply  [nested|flat] 11+ messages in thread


end of thread, other threads:[~2020-09-16 13:34 UTC | newest]

Thread overview: 11+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-07-12 05:48 Avoid useless retrieval of defaults and check constraints in pg_dump -a Julien Rouhaud <rjuju123@gmail.com>
2020-07-12 14:29 ` Justin Pryzby <pryzby@telsasoft.com>
2020-07-14 09:14   ` Julien Rouhaud <rjuju123@gmail.com>
2020-09-15 02:33     ` Michael Paquier <michael@paquier.xyz>
2020-09-15 02:56       ` Tom Lane <tgl@sss.pgh.pa.us>
2020-09-15 03:08         ` Michael Paquier <michael@paquier.xyz>
2020-09-10 12:31 ` Daniel Gustafsson <daniel@yesql.se>
2020-09-15 02:48   ` Michael Paquier <michael@paquier.xyz>
2020-09-15 08:46     ` Julien Rouhaud <rjuju123@gmail.com>
2020-09-16 13:06       ` Michael Paquier <michael@paquier.xyz>
2020-09-16 13:34         ` Julien Rouhaud <rjuju123@gmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox