简体   繁体   English

未定义表:7 错误:关系“V5TableName”不存在

[英]Undefined table: 7 ERROR: relation “V5TableName” does not exist

I am using an MVC framework (Zend) for my application and I want to find the total size of a table in PostgreSQL (including index).我正在为我的应用程序使用 MVC 框架(Zend),我想在 PostgreSQL(包括索引)中找到一个表的总大小。 The table name is "V5TableName" - quotes included because table name is case sensitive.表名是“V5TableName” ——包括引号,因为表名区分大小写。 I have made sure that there is NO typo involved.我已确保不涉及任何错字。

My code to get the table size is shown below:我获取表格大小的代码如下所示:

public function getMyTableSize()
{
  $sql = "SELECT pg_size_pretty(pg_total_relation_size( '\"V5TableName\"' ) );";

  /* Custom_Db is a custom library in my application which makes the PostgreSQL connection 
     and queries the database
   */
  $tableSize = Custom_Db::query($sql)->fetchColumn(); 

  return $tableSize;
}

When my application calls this function it returns the following error in my logs:当我的应用程序调用此 function 时,它会在我的日志中返回以下错误:

 [22-Apr-2020 09:42:37] PID:30849 ERR: SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "V5TableName" does not exist LINE 1: SELECT pg_size_pretty(pg_total_relation_size( '"V5TableName... ^ query was: SELECT pg_size_pretty(pg_total_relation_size( '"V5TableName"' ) );

If I run this same query in pgAdmin4 it works perfectly fine returning the table size (for instance: 104Mb).如果我在 pgAdmin4 中运行相同的查询,它可以很好地返回表大小(例如:104Mb)。

I have tried:我努力了:

  1. Removing and adding quotes to the table name in the code.在代码中为表名删除和添加引号。
  2. Appending the schema as prefix to the table name (example: 'public."V5TableName"' ).将模式作为前缀附加到表名(例如: 'public."V5TableName"' )。

NONE of the above seem to work.以上似乎都不起作用。 I am not sure what is going wrong over here.我不确定这里出了什么问题。

I also tried to find the total database size in my application (db name: MyDbName - with mixed case spelling) and my query looked something like below:我还尝试在我的应用程序中找到总数据库大小(数据库名称: MyDbName - 混合大小写),我的查询如下所示:

$sql = "SELECT pg_size_pretty(pg_database_size('MyDbName'))"; // this DID NOT WORK

So I changed it to the one shown below: (it worked)所以我将其更改为如下所示的:(它有效)

$sql = "SELECT pg_size_pretty(pg_database_size( current_database() ))"; // this WORKED

I was wondering if there is something similar that could be done to find the table size.我想知道是否有类似的方法可以找到表格大小。

Your query should work.您的查询应该有效。 The use of double-quotes seems correct.双引号的使用似乎是正确的。

SELECT pg_size_pretty(pg_total_relation_size('"V5TableName"'));

First make sure you are connecting to the right database cluster (aka "server").首先确保您连接到正确的数据库集群(又名“服务器”)。 It's defined by its data directory, or equally unambiguous by hostname and port number.它由其数据目录定义,或者由主机名和端口号同样明确。 Read the manual here and here .此处此处阅读手册。

Then make sure you are connecting to the right database within that database cluster.然后确保您连接到该数据库集群中的正确数据库 A Postgres database cluster consists of 1-n databases.一个 Postgres 数据库集群由 1-n 个数据库组成。 When connecting without specifying the actual database, you end up in the maintenance database named postgres by default.在未指定实际数据库的情况下进行连接时,默认情况下会进入名为postgres的维护数据库。 That's the most likely explanation.这是最可能的解释。 Check with:检查:

SELECT current_database();

Then check for the right table and schema name :然后检查正确的表和模式名称

SELECT * FROM pg_tables
WHERE tablename ~* 'V5TableName';  -- ~* matches case-insensitive

The first riddle should be solved at this point.第一个谜应该在这一点上解决。

Check your DB spelling and possible near-duplicates with:检查您的数据库拼写和可能的近重复:

SELECT datname FROM pg_database;

The call is without double-quotes (like you tried correctly), but requires correct capitalization:该调用没有双引号(就像您尝试正确一样),但需要正确的大写:

SELECT pg_size_pretty(pg_database_size('MyDbName'));

Note the subtle difference (as documented in the manual ):请注意细微差别(如手册中所述):

  • pg_database_size() takes oid or name . pg_database_size()采用oidname So pass the case-sensitive database name without double-quotes .所以传递区分大小写的数据库名称,不要使用双引号
  • pg_total_relation_size() takes regclass . pg_total_relation_size()需要regclass So pass the case-sensitive relation name with double-quotes if you need to preserve capitalization.因此,如果您需要保留大小写,请使用双引号传递区分大小写的关系名称。

pg_database_size() has to differ because there is no dedicated object identifier type for databases ( no regdatabase ). pg_database_size()必须有所不同,因为数据库没有专用的 object 标识符类型(没有 regdatabase )。

The gist of it: avoid double-quoted identifiers in Postgres if at all possible.它的要点:尽可能避免在 Postgres 中使用双引号标识符。 It makes your life easier.它使您的生活更轻松。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Relation * tablename *不存在 - Relation *tablename* does not exist 未定义的表:7 错误:关系“费用”不存在 - Undefined table: 7 ERROR: relation "expenses" does not exist 表“ tablename”在穆迪中不存在 - Table “tablename” does not exist in moodle Illuminate\Database\QueryException 与消息'SQLSTATE [42P01]:未定义的表:7 错误:关系“联系人”不存在 - Illuminate\Database\QueryException with message 'SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "contacts" does not exist SQLSTATE[42P01]:未定义表:7 错误:关系“类别”不存在第 1 行:从“类别”中选择 * ^(SQL:从“类别”中选择 *) - SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "categories" does not exist LINE 1: select * from "categories" ^ (SQL: select * from "categories") 教义迁移错误-“关系不存在” - Doctrine Migration Error - “relation does not exist” MySQL表不存在错误,但确实存在 - MySQL Table does not exist error, but it does exist 不能简单地使用 PostgreSQL 表名(“关系不存在”) - Cannot simply use PostgreSQL table name ("relation does not exist") 未定义的列:7错误:列不存在 - Undefined column: 7 ERROR: column does not exist pg_prepare():查询失败:错误:关系不存在 - pg_prepare(): Query failed: ERROR: Relation does not exist
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM