Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: pg_catalog.pg_statistic_ext mistakenly tries to report rows for dropped tables and fails with an error #108813

Closed
knz opened this issue Aug 15, 2023 · 3 comments · Fixed by #108818
Assignees
Labels
A-sql-table-stats Table statistics (and their automatic refresh). A-sql-vtables Virtual tables - pg_catalog, information_schema etc C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. db-cy-23 T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions)

Comments

@knz
Copy link
Contributor

knz commented Aug 15, 2023

Describe the problem

CREATE TABLE t(x) AS SELECT generate_series(1,100);
CREATE STATISTICS st ON x FROM t;
DROP TABLE t;
TABLE pg_catalog.pg_statistic_ext;

Observe the last query fail:

(error encountered after some results were delivered)
ERROR: descriptor is being dropped

If the client waits some time after DROP and before looking at pg_statistic_ext, the following error is reported instead:

ERROR: relation "[54]" does not exist
SQLSTATE: 42P01

This is especially visible because it breaks \d in the SQL shell starting in v23.1.

Expected behavior

pg_statistic_ext must not contain rows for dropped tables.

Workaround

Until this bug is fixed, the following SQL statement can be run with the admin role to clear the SQL stats for dropped tables:

DELETE FROM system.table_statistics AS ts
      WHERE NOT EXISTS(
                    SELECT 1
                      FROM crdb_internal.tables AS tb
                     WHERE     ts."tableID" = tb.table_id
                           AND tb.state != 'DROP'
                );

Jira issue: CRDB-30650

Epic CRDB-27601

@knz knz added C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. A-sql-vtables Virtual tables - pg_catalog, information_schema etc A-sql-table-stats Table statistics (and their automatic refresh). T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions) labels Aug 15, 2023
@knz
Copy link
Contributor Author

knz commented Aug 15, 2023

Note: this issue is a side effect of #100482 -- when a node crashes or gets restarted, any pending stats updates caused by table creation/drop are not processed any more (the pending stats updated were stored in a Go channel, and are not persisted). With table drops in particular, the stats for the dropped table are never deleted.

This may be a separate bug to file? Unsure. cc @michae2

@nexdrew
Copy link
Contributor

nexdrew commented Aug 15, 2023

Possibly related to #108482

@michae2
Copy link
Collaborator

michae2 commented Aug 15, 2023

Note: this issue is a side effect of #100482 -- when a node crashes or gets restarted, any pending stats updates caused by table creation/drop are not processed any more (the pending stats updated were stored in a Go channel, and are not persisted). With table drops in particular, the stats for the dropped table are never deleted.

This may be a separate bug to file? Unsure. cc @michae2

For drops in particular, this was actually fixed recently in #105364, which is in v23.1.5 behind the cluster setting sql.stats.garbage_collection_interval.

That being said, even with garbage collection turned on there will still be a lag between DROP TABLE and deletion of rows from system.table_statistics. I think one way to make pg_statistic_ext more robust to this would be to change the following query to join to crdb_internal.tables to make sure we're only reading stats for live tables:

query := `SELECT "tableID", name, "columnIDs", "statisticID", '{d}'::"char"[] FROM system.table_statistics;`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-sql-table-stats Table statistics (and their automatic refresh). A-sql-vtables Virtual tables - pg_catalog, information_schema etc C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. db-cy-23 T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants