18
Finding and Reporting Postgres Bug #8257 BY: LLOYD ALBIN 8/6/2013

Finding and Reporting Postgres Bug #8257

  • Upload
    winka

  • View
    85

  • Download
    0

Embed Size (px)

DESCRIPTION

Finding and Reporting Postgres Bug #8257. By: Lloyd Albin 8/6/2013. The Bug. This presentation will describe the finding of a bug in the multi-threaded restores and how the fix was implemented. The PG_RESTORE command. - PowerPoint PPT Presentation

Citation preview

Page 1: Finding and Reporting Postgres Bug #8257

Finding and ReportingPostgres Bug #8257

BY: LLOYD ALBIN8/6/2013

Page 2: Finding and Reporting Postgres Bug #8257

The Bug

This presentation will describe the finding of a bug in the multi-threaded restores and how the fix was implemented.

Page 3: Finding and Reporting Postgres Bug #8257

The PG_RESTORE command

When doing a multi-threaded restore, you specify the –j option and specify the number of jobs / threads / cores. In this example there will be three threads spawned by the parent process.

You may only use the –j option with custom or directory backups.

pg_restore -C -j 3 -d postgres -Fc test_db.dump

Page 4: Finding and Reporting Postgres Bug #8257

Create a test database

Here we create a test database for our test.

CREATE DATABASE test_db WITH OWNER = postgres ENCODING = 'UTF8' TEMPLATE = template0;

Page 5: Finding and Reporting Postgres Bug #8257

Creating our test table

We will create a basic table with just a primary key. We don’t need any other fields for this example.

CREATE TABLE public.tbl_test ( pkey TEXT NOT NULL, CONSTRAINT tbl_test_pkey PRIMARY KEY(pkey));

Page 6: Finding and Reporting Postgres Bug #8257

Creating an index on the Primary Key

We can now add a comment to the index. This is the whole root of the problem for the multi-threaded restore. If you don’t have any comment on automatically created index’s, then you won’t have any issues with the multi-threaded restore.

COMMENT ON INDEX public.tbl_test_pkeyIS 'Index Comment';

Page 7: Finding and Reporting Postgres Bug #8257

Backing up the database

We don’t need any data for our example, so we are now ready to backup the database. Once the database if backed up we can go ahead and drop it.

pg_dump -Fc test_db > test_db.dump

dropdb test_db

Page 8: Finding and Reporting Postgres Bug #8257

Showing the problem

What happed is the restore tried to add the comment before the index was created.

I have tested this with Postgres 9.0.12 & 9.2.4

pg_restore -C -j 3 -d postgres -Fc test_db.dump

pg_restore: [archiver (db)] Error while PROCESSING TOC:pg_restore: [archiver (db)] Error from TOC entry 2525; 0 0 COMMENT INDEXtbl_test_pkey postgrespg_restore: [archiver (db)] could not execute query: ERROR: relation"tbl_test_pkey" does not exist Command was: COMMENT ON INDEX tbl_test_pkey IS 'Index Comment';

pg_restore: [archiver] worker process failed: exit code 1

Page 9: Finding and Reporting Postgres Bug #8257

Submitting the bug

The first thing you should do is to search the pgsql-bugs and pgsql-hackers mailing lists for your problem. If you don’t find it, then go ahead and submit a bug ticket.

http://www.postgresql.org/support/submitbug

The form will ask you for the following information:NameEmailPostgreSQL versionOperating SystemShort DescriptionLong Description

Page 10: Finding and Reporting Postgres Bug #8257

The submitted bug

To see the full bug submission:

http://www.postgresql.org/message-id/[email protected]

From: lalbin(at)fhcrc(dot)orgTo: pgsql-bugs(at)postgresql(dot)orgSubject: BUG #8257: Multi-Core Restore fails when containing index commentsDate: 2013-06-26 23:43:00

The following bug has been logged on the website:

Bug reference: 8257Logged by: Lloyd AlbinEmail address: lalbin(at)fhcrc(dot)orgPostgreSQL version: 9.2.4Operating system: SUSE Linux (64-bit)

Page 11: Finding and Reporting Postgres Bug #8257

Internals of the dump indexThe problem is that pg_dump makes the comment depend on the index instead of the constraint:

There is no object 1832 in the dump since that was ommitted in favor of the constraint 1833 which internally creates the index.

Andres FreundPostgreSQL Development2nd Quadrant

; Selected TOC Entries:...170; 1259 69261 TABLE public tbl_test andres; depends on: 61941; 0 69261 TABLE DATA public tbl_test andres; depends on: 1701833; 2606 69268 CONSTRAINT public tbl_test_pkey andres; depends on: 170 1701950; 0 0 COMMENT public INDEX tbl_test_pkey andres; depends on: 1832

Page 12: Finding and Reporting Postgres Bug #8257

With the patchSo what we need to do is to make the comment depend on the constraint instead.

Unsurprisingly after that restore completes.

Andres FreundPostgreSQL Development2nd Quadrant

http://www.postgresql.org/message-id/[email protected]

170; 1259 69261 TABLE public tbl_test andres; depends on: 61941; 0 69261 TABLE DATA public tbl_test andres; depends on: 1701833; 2606 69268 CONSTRAINT public tbl_test_pkey andres; depends on: 170 1701950; 0 0 COMMENT public INDEX tbl_test_pkey andres; depends on: 1833

Page 13: Finding and Reporting Postgres Bug #8257

Reviewing the patch #1

Tom Lane points out that the fix will only fix new dumps and not any previous dumps.

http://www.postgresql.org/message-id/[email protected]

Andres Freund <andres(at)2ndquadrant(dot)com> writes:> The problem is that pg_dump makes the comment depend on the index instead of the constraint:

Yeah, I figured that out yesterday, but hadn't gotten to writing a patchyet.

> ... So what we need to do is to make the comment depend on the constraint instead.

Your proposed patch will only fix the problem for dumps created afterit ships. In the past, we've tried to deal with this type of issue byhaving pg_restore fix up the dependencies when reading a dump, so thatit would still work on existing dumps.

I'm afraid there may be no way to do that in this case --- it doesn'tlook like there's enough info in the dump to tell where the dependencylink should have led. But we should think about it a little beforetaking the easy way out.

Page 14: Finding and Reporting Postgres Bug #8257

Reviewing the patch #2

Andres Freund points out that unfortunately this is the best fix.

http://www.postgresql.org/message-id/[email protected]

On 2013-06-27 10:29:14 -0400, Tom Lane wrote:> > ... So what we need to do is to make the comment depend on the constraint instead.

> Your proposed patch will only fix the problem for dumps created after it ships. In the past, we've tried to deal with this type of issue by having pg_restore fix up the dependencies when reading a dump, so that it would still work on existing dumps.

Yes :(. On the other hand, it's probably not too common to create comments on indexes that haven't been created explicitly.

> I'm afraid there may be no way to do that in this case --- it doesn't look like there's enough info in the dump to tell where the dependency link should have led. But we should think about it a little before taking the easy way out.

The only thing I could think of - but which I thought to be too kludgey - was to simply delay the creation of all comments and restore them together with ACLs. I don't think we can have dependencies towards comments.

Page 15: Finding and Reporting Postgres Bug #8257

Reviewing the patch #3Tom Lane, says that Andres was correct with patching pg_dump but wished that pg_restore could also have been patched to support the bad dumps but agrees that the only way to fix it is not a good workaround.

http://www.postgresql.org/message-id/[email protected]

Andres Freund <andres(at)2ndquadrant(dot)com> writes:> On 2013-06-27 10:29:14 -0400, Tom Lane wrote:>> Your proposed patch will only fix the problem for dumps created after it ships. In the past, we've tried to deal with this type of issue by having pg_restore fix up the dependencies when reading a dump, so that it would still work on existing dumps.

> Yes :(. On the other hand, it's probably not too common to create comments on indexes that haven't been created explicitly.

Perhaps. The lack of previous complaints does suggest this situation isn't so common.

>> I'm afraid there may be no way to do that in this case --- it doesn't look like there's enough info in the dump to tell where the dependency link should have led. But we should think about it a little before taking the easy way out.

> The only thing I could think of - but which I thought to be too kludgey - was to simply delay the creation of all comments and restore them together with ACLs.

I don't like that either, though we may be forced into it if we find more bugs in comment dependencies.

Anyway, fixing pg_dump's logic is not wrong; I was just hoping we could also think of a workaround on the pg_restore side.

Page 16: Finding and Reporting Postgres Bug #8257

Patch approved by Tom Lane

Tom Lane approved the patch.

http://www.postgresql.org/message-id/[email protected]

Andres Freund <andres(at)2ndquadrant(dot)com> writes:> There is no object 1832 in the dump since that was ommitted in favor of the constraint 1833 which internally creates the index. So what we need to do is to make the comment depend on the constraint instead.> With the attached patch we get: [ the right thing ]

Applied with minor cosmetic changes.

Page 17: Finding and Reporting Postgres Bug #8257

Patch committed

You can view all the committed patched on the pgsql-committers mailing list.

Each branch patch is a separate email on the pgsql-committers mailing list.

Mark index-constraint comments with correct dependency in pg_dump.

When there's a comment on an index that was created with UNIQUE or PRIMARY KEY constraint syntax, we need to label the comment as depending on the constraint not the index, since only the constraint object actually appears in the dump. This incorrect dependency can lead to parallel pg_restore trying to restore the comment before the index has been created, per bug #8257 from Lloyd Albin.

This patch fixes pg_dump to produce the right dependency in dumps made in the future. Usually we also try to hack pg_restore to work around bogus dependencies, so that existing (wrong) dumps can still be restored in parallel mode; but that doesn't seem practical here since there's no easy way to relate the constraint dump entry to the comment after the fact.

Andres Freund

Branch------REL9_3_STABLEREL9_2_STABLEREL9_1_STABLEREL9_0_STABLEREL8_4_STABLEmaster

Page 18: Finding and Reporting Postgres Bug #8257

Affected Versions

These are the affected versions:

8.4.179.0.139.1.99.2.49.3 Beta 1 and possibly Beta 2 since it was released the same day as the patches were committed.

It is affecting all current versions 8.4+ as of 8/6/2013.