2202H error code in PostgreSQL, which stands for
invalid_tablesample_argument, indicates that there is an issue with the argument provided to the TABLESAMPLE clause. The TABLESAMPLE clause is used to retrieve a random sample of rows from a table, which can be helpful for approximating query results on large tables.
Here are some steps to diagnose and fix this error:
- Check the Argument Types: Ensure that the arguments you are passing to the TABLESAMPLE clause are of the correct type. For example, the
SYSTEMsampling methods expect a percentage as an argument, which should be a numeric value between 0 and 100.
- Check the Argument Values: Confirm that the values provided to the TABLESAMPLE clause are within the valid range. Providing a negative number or a number greater than 100 for a percentage, for example, would trigger this error.
Here’s an example of correct usage with sample code:
-- Correct usage of TABLESAMPLE with the BERNOULLI method
SELECT * FROM your_table TABLESAMPLE BERNOULLI (10);
In this example,
10 represents 10 percent of the rows, which is a valid argument for the BERNOULLI method.
An example of incorrect usage that would cause the
2202H error might look like this:
-- Incorrect usage of TABLESAMPLE with an invalid argument
SELECT * FROM your_table TABLESAMPLE BERNOULLI (-10);
SELECT * FROM your_table TABLESAMPLE BERNOULLI (110);
In the incorrect examples,
-10 is invalid because it’s a negative number, and
110 is invalid because it exceeds the maximum allowed value of 100 percent.
If you encounter the
2202H error, review the query that caused it, and adjust the TABLESAMPLE arguments according to the rules mentioned above. If the error persists, consult the PostgreSQL documentation or community forums for further troubleshooting.