Skip to content

Commit

Permalink
[#22028] YSQL: Add yb_reset_analyze_statistics function
Browse files Browse the repository at this point in the history
Summary:
Add function that resets the stats collected by ANALYZE command.

Function Description:
```
yb_reset_analyze_statistics ( table_oid ) → void
      Resets the statistics of a table collected by the ANALYZE command.  if table_oid is NULL, this function resets the statistics for all the tables in the current database that the user can analyze.
```

What this function resets:
* `reltuple`s of the table and the indexes in `pg_class` are set to 0.
* The rows in `pg_statistic` that corresponds to each table column are deleted.
* The values of extended statistics in `pg_statistic_ext`, if created, are set to NULL.  (The rows in `pg_statistic_ext` are managed by `CREATE/ALTER/DROP STATISTICS` commands, and their statistic values are populated by the ANALYZE command.)
* After modifying the system catalog entries described above, this function increment the YB catalog version of the current database for propagation.

What this function does not reset:
* Each sequence object has fixed value of 1 in `reltuples`.  This is not collected by or updated by the ANALYZE command, and this function does not reset it, either.

Who can reset what:
* Only superusers can reset the stats for the tables shared across all the databases (e.g.: `pg_database`).
* Non-superuser current database owner can reset the stats for any non-shared table in the database including the ones in `pg_catalog` and `information_schema` namespaces.

Limitation and potential follow-on work:
* Invalid or irrelevant table_oid argument will be silently ignored.  If more informative error handling is required, this would have to be implemented as either a PL/PgSQL function (then load it after the load_plpgsql() call in initdb) or a C function.
Jira: DB-10945

Test Plan:
  ./yb_build.sh --java-test 'org.yb.pgsql.TestPgRegressAnalyze'
  ./yb_build.sh --java-test 'org.yb.pgsql.TestYsqlUpgrade'

Reviewers: tnayak

Reviewed By: tnayak

Subscribers: yql, smishra, mihnea

Differential Revision: https://phorge.dev.yugabyte.com/D35829
  • Loading branch information
mtakahar committed Jul 8, 2024
1 parent fb19c9f commit 9f2f70e
Showing 8 changed files with 1,102 additions and 3 deletions.
73 changes: 73 additions & 0 deletions src/postgres/src/backend/catalog/yb_system_views.sql
Original file line number Diff line number Diff line change
@@ -1323,6 +1323,79 @@ LANGUAGE INTERNAL
STRICT STABLE PARALLEL SAFE
AS 'yb_is_database_colocated';

CREATE OR REPLACE FUNCTION
yb_reset_analyze_statistics(table_oid oid)
RETURNS void AS
$$
UPDATE pg_class c SET reltuples = 0
WHERE
relkind IN ('r', 'm', 'i')
AND reltuples > 0
AND NOT EXISTS (
SELECT 0 FROM pg_namespace ns
WHERE ns.oid = relnamespace
AND nspname IN ('pg_toast', 'pg_toast_temp')
)
AND (table_oid IS NULL
OR c.oid = table_oid
OR c.oid IN (SELECT indexrelid FROM pg_index WHERE indrelid = table_oid))
AND ((SELECT rolsuper FROM pg_roles r WHERE rolname = session_user)
OR (NOT relisshared
AND (relowner = session_user::regrole
OR ((SELECT datdba
FROM pg_database d
WHERE datname = current_database())
= session_user::regrole)
)
)
);
DELETE
FROM pg_statistic
WHERE
EXISTS (
SELECT 0 FROM pg_class c
WHERE c.oid = starelid
AND (table_oid IS NULL OR c.oid = table_oid)
AND ((SELECT rolsuper FROM pg_roles r
WHERE rolname = session_user)
OR (NOT relisshared
AND (relowner = session_user::regrole
OR ((SELECT datdba
FROM pg_database d
WHERE datname = current_database())
= session_user::regrole)
)
)
)
);
UPDATE pg_statistic_ext SET (stxndistinct, stxdependencies) = (NULL, NULL)
WHERE
EXISTS (
SELECT 0 FROM pg_class c
WHERE c.oid = stxrelid
AND (table_oid IS NULL OR c.oid = table_oid)
AND ((SELECT rolsuper FROM pg_roles r
WHERE rolname = session_user)
OR (NOT relisshared
AND (relowner = session_user::regrole
OR ((SELECT datdba
FROM pg_database d
WHERE datname = current_database())
= session_user::regrole)
)
)
)
);
UPDATE pg_yb_catalog_version SET current_version = current_version + 1
WHERE db_oid = (SELECT oid FROM pg_database
WHERE datname = current_database());
$$
LANGUAGE SQL
CALLED ON NULL INPUT
VOLATILE
SECURITY DEFINER
SET yb_non_ddl_txn_for_sys_tables_allowed = ON;

--
-- The default permissions for functions mean that anyone can execute them.
-- A number of functions shouldn't be executable by just anyone, but rather
2 changes: 1 addition & 1 deletion src/postgres/src/include/catalog/catalog.h
Original file line number Diff line number Diff line change
@@ -29,7 +29,7 @@
* If you increment it, make sure you didn't forget to add a new SQL migration
* (see pg_yb_migration.dat and src/yb/yql/pgwrapper/ysql_migrations/README.md)
*/
#define YB_LAST_USED_OID 8066
#define YB_LAST_USED_OID 8067

extern bool IsSystemRelation(Relation relation);
extern bool IsToastRelation(Relation relation);
5 changes: 5 additions & 0 deletions src/postgres/src/include/catalog/pg_proc.dat
Original file line number Diff line number Diff line change
@@ -10496,4 +10496,9 @@
proargnames => '{tablet_id,table_id,table_type,namespace_name,ysql_schema_name,table_name,partition_key_start,partition_key_end}',
proargmodes => '{o,o,o,o,o,o,o,o}',
prosrc => 'yb_local_tablets'},

{ oid => '8067',
descr => 'Reset the statistics collected by the ANALYZE command.',
proname => 'yb_reset_analyze_statistics', prolang => '14', provolatile => 'v',
prorettype => 'void', proargtypes => 'oid', prosrc => 'see yb_system_views.sql' },
]
4 changes: 2 additions & 2 deletions src/postgres/src/include/catalog/pg_yb_migration.dat
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@
[

# For better version control conflict detection, list latest migration filename
# here: V51__21861__ysql_dbid_in_yb_active_session_history.sql
{ major => '51', minor => '0', name => '<baseline>', time_applied => '_null_' }
# here: V52__22028__yb_reset_analyze_statistics.sql
{ major => '52', minor => '0', name => '<baseline>', time_applied => '_null_' }

]
Loading

0 comments on commit 9f2f70e

Please sign in to comment.