CREATE STATISTICS
 
 
  CREATE STATISTICS
  7
  SQL - Language Statements
 
 
  CREATE STATISTICS
  define extended statistics
 
 
CREATE STATISTICS [ IF NOT EXISTS ] statistics_name
    WITH ( option [= value] [, ... ] )
    ON ( column_name, column_name [, ...])
    FROM table_name
 
 
  Description
  
   CREATE STATISTICS will create a new extended statistics
   object on the specified table, foreign table or materialized view.
   The statistics will be created in the current database and
   will be owned by the user issuing the command.
  
  
   If a schema name is given (for example, CREATE STATISTICS
   myschema.mystat ...>) then the statistics is created in the specified
   schema.  Otherwise it is created in the current schema.  The name of
   the statistics must be distinct from the name of any other statistics in the
   same schema.
  
 
 
  Parameters
  
   
    IF NOT EXISTS>
    
     
      Do not throw an error if a statistics with the same name already exists.
      A notice is issued in this case.  Note that only the name of the
      statistics object is considered here. The definition of the statistics is
      not considered.
     
    
   
   
    statistics_name
    
     
      The name (optionally schema-qualified) of the statistics to be created.
     
    
   
   
    column_name
    
     
      The name of a column to be included in the statistics.
     
    
   
   
    table_name
    
     
      The name (optionally schema-qualified) of the table the statistics should
      be created on.
     
    
   
  
  
   Parameters
 
  statistics parameters
 
   
    The WITH> clause can specify options>
    for the statistics. Available options are listed below.
   
   
   
    dependencies> (boolean>)
    
     
      Enables functional dependencies for the statistics.
     
    
   
   
    ndistinct> (boolean>)
    
     
      Enables ndistinct coefficients for the statistics.
     
    
   
   
  
 
 
  Notes
  
   You must be the owner of a table to create or change statistics on it.
  
 
 
  Examples
  
   Create table t1> with two functionally dependent columns, i.e.
   knowledge of a value in the first column is sufficient for determining the
   value in the other column. Then functional dependencies are built on those
   columns:
CREATE TABLE t1 (
    a   int,
    b   int
);
INSERT INTO t1 SELECT i/100, i/500
                 FROM generate_series(1,1000000) s(i);
CREATE STATISTICS s1 WITH (dependencies) ON (a, b) FROM t1;
ANALYZE t1;
-- valid combination of values
EXPLAIN ANALYZE SELECT * FROM t1 WHERE (a = 1) AND (b = 0);
-- invalid combination of values
EXPLAIN ANALYZE SELECT * FROM t1 WHERE (a = 1) AND (b = 1);
  
 
 
  Compatibility
  
   There's no CREATE STATISTICS command in the SQL standard.
  
 
 
  See Also