{"record":{"id":"fec67ffe3d6b3a49","repo":"stanfordnlp/CoreNLP","slug":"getting-table-count-is-not-working-e","errorCode":null,"errorMessage":"getting table count is not working! ${e}","messagePattern":"getting table count is not working! (.+?)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/util/GoogleNGramsSQLBacked.java","lineNumber":191,"sourceCode":"  }\n\n  /** Note that this is really really slow for ngram > 1\n   * TODO: make this fast (if we had been using mysql we could have)\n   * **/\n  public static int getTotalCount(int ngram){\n    try{\n      connect();\n      Statement stmt = connection.createStatement();\n      String table = tablenamePrefix + ngram;\n      String q = \"select count(*) from \" + table+\";\";\n      ResultSet s = stmt.executeQuery(q);\n      if(s.next()){\n        return s.getInt(1);\n      } else\n        throw new RuntimeException(\"getting table count is not working!\");\n    }\n    catch(SQLException e){\n      throw new RuntimeException(\"getting table count is not working! \" + e);\n    }\n  }\n\n  /** Return rank of 1 gram in google ngeams if it is less than 20k. Otherwise -1. */\n  public static int get1GramRank(String str) {\n    String query = null;\n    try{\n      connect();\n      str = str.trim();\n      if(str.contains(\"'\")){\n        str = StringUtils.escapeString(str, new char[]{'\\''},'\\'');\n      }\n\n      int ngram = str.split(\"\\\\s+\").length;\n      if(ngram > 1)\n        return -1;\n      String table =  \"googlengrams_1_ranked20k\";\n","sourceCodeStart":173,"sourceCodeEnd":209,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/util/GoogleNGramsSQLBacked.java#L173-L209","documentation":"getTotalCount wraps SQL failures in a RuntimeException with this message plus the SQLException details. The count query could not be executed against the n-gram table (bad table, connection, or SQL error).","triggerScenarios":"Calling getTotalCount when the underlying 'select count(*) from GoogleNgrams_<ngram>' throws a SQLException — e.g. table does not exist, connection dropped, or insufficient privileges.","commonSituations":"Querying an n-gram size whose table was never created; database restart or timeout mid-query; wrong credentials/schema.","solutions":["Read the appended SQLException message for the root cause and fix it accordingly","Create the missing GoogleNgrams_<NGRAM> table and index in psql if that is the cause","Check the database connection settings and that the server is reachable"],"exampleFix":"// before\nint n = GoogleNGramsSQLBacked.getTotalCount(5); // GoogleNgrams_5 missing\n// after\npsql -c \"create table GoogleNgrams_5 (phrase text primary key not null, count bigint not null);\"\nint n = GoogleNGramsSQLBacked.getTotalCount(5);","handlingStrategy":"try-catch","validationCode":"select to_regclass('GoogleNgrams_1') is not null as tableExists;","typeGuard":null,"tryCatchPattern":"try { return GoogleNGramsSQLBacked.getTotalCount(ngram); } catch (RuntimeException e) { throw new IllegalStateException(\"DB count failed for ngram \" + ngram, e); }","preventionTips":["Create all GoogleNgrams_<NGRAM> tables per class docs before querying","Watch connection health/timeout settings for long-running batches","Inspect the appended SQLException text for the true root cause"],"tags":["sql","postgres","database","sqlexception"],"backgroundTag":"sql-query-failed","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}