草庐IT

出现错误时 PostgreSQL 不返回完整的响应

coder 2024-07-05 原文

当我运行导致错误的查询时,我遇到了一个问题。

例如,如果我运行以下查询

SELECT * FROM users WHERE account_id=1;

用户表没有 account_id 列。

正确的错误信息返回:

 00000000  45 00 00 00 6e 53 45 52  52 4f 52 00 56 45 52 52  |E...nSERROR.VERR|
 00000010  4f 52 00 43 34 32 37 30  33 00 4d 63 6f 6c 75 6d  |OR.C42703.Mcolum|
 00000020  6e 20 22 61 63 63 6f 75  6e 74 5f 69 64 22 20 64  |n "account_id" d|
 00000030  6f 65 73 20 6e 6f 74 20  65 78 69 73 74 00 50 32  |oes not exist.P2|
 00000040  37 00 46 70 61 72 73 65  5f 72 65 6c 61 74 69 6f  |7.Fparse_relatio|
 00000050  6e 2e 63 00 4c 33 32 39  33 00 52 65 72 72 6f 72  |n.c.L3293.Rerror|
 00000060  4d 69 73 73 69 6e 67 43  6f 6c 75 6d 6e 00 00 5a  |MissingColumn..Z|
 00000070  00 00 00 05 49                                    |....I|

注意响应的结尾。

但时不时地,我会从 PostgreSQL 收到以下响应。

 00000000  45 00 00 00 6e 53 45 52  52 4f 52 00 56 45 52 52  |E...nSERROR.VERR|
 00000010  4f 52 00 43 34 32 37 30  33 00 4d 63 6f 6c 75 6d  |OR.C42703.Mcolum|
 00000020  6e 20 22 61 63 63 6f 75  6e 74 5f 69 64 22 20 64  |n "account_id" d|
 00000030  6f 65 73 20 6e 6f 74 20  65 78 69 73 74 00 50 32  |oes not exist.P2|
 00000040  37 00 46 70 61 72 73 65  5f 72 65 6c 61 74 69 6f  |7.Fparse_relatio|
 00000050  6e 2e 63 00 4c 33 32 39  33 00 52 65 72 72 6f 72  |n.c.L3293.Rerror|
 00000060  4d 69 73 73 69 6e 67 43  6f 6c 75 6d 6e 00 00     |MissingColumn..|

然后客户端无法解析响应,因为它缺少结尾。

这是我做错了什么,还是这可能是 PostgreSQL 中的错误?

我在 Golang 的 net.Conn 中通过 Read() 收到来自 PostgreSQL 的响应

想法?

创建表的脚本:

 DROP TABLE IF EXISTS ready.public.users;

 CREATE TABLE ready.public.users
 (
   user_id             UUID             NOT NULL,
   email               VARCHAR(325)     NOT NULL,
   password            VARCHAR(2000)    NULL,
   first_name          VARCHAR(200)     NULL,
   last_name           VARCHAR(200)     NULL,
   global_permissions  BIGINT DEFAULT 1 NOT NULL,
   is_system           BOOLEAN          NOT NULL DEFAULT false,
   is_device_only      BOOLEAN          NOT NULL DEFAULT false,
   date_created        TIMESTAMP        NOT NULL,
   date_updated        TIMESTAMP        NULL,
   date_last_logged_in TIMESTAMP        NULL,
   date_last_seen      TIMESTAMP        NULL
 );

我的 golang 库中的代码。

 rConn.Write(buf.Bytes())

 data := make([]byte, 2048)
 n, err := rConn.Read(data)
 data = data[:n]
 if err != nil {
    if err != io.EOF {
        panic(err)
    } else {
        log.Printf("received err: %v", err)
    }
 }

buf.Bytes() 的值为:

 00000000  50 00 00 00 2e 00 53 45  4c 45 43 54 20 2a 20 46  |P.....SELECT * F|
 00000010  52 4f 4d 20 75 73 65 72  73 20 57 48 45 52 45 20  |ROM users WHERE |
 00000020  61 63 63 6f 75 6e 74 5f  69 64 3d 31 00 00 00 42  |account_id=1...B|
 00000030  00 00 00 0c 00 00 00 00  00 00 00 00 44 00 00 00  |............D...|
 00000040  06 50 00 45 00 00 00 09  00 00 00 01 f6 53 00 00  |.P.E.........S..|
 00000050  00 04                                             |..|

更改我的代码以读取来自 postgresql 的响应仍然会导致来自服务器的相同响应。

 rBuf := &bytes.Buffer{}
 for {
    data := make([]byte, 256)
    n, err := rConn.Read(data)
    if err != nil {
        panic(err)
    }
    rBuf.Write(data[:n])
    if n < 256 {
        break
    }
 }
 go log.Printf("received:\n%v", hex.Dump(rBuf.Bytes()))

最佳答案

是否遇到了 panic 并打印错误消息?是否在读取完成之前跳出循环?

在“更改的代码”部分,您甚至在 io.EOF 上都感到 panic ,并且在这种情况下您没有对读取的字节做任何事情。尝试删除 panic,并在错误处理之前使用读取的字节(就像在“之前”代码段中一样),然后循环直到出现读取错误。我至少想找出/测试:

buf := new(bytes.Buffer)
for {
    data := make([]byte, 2048)
    n, err := rConn.Read(data)
    if n > 0 {
        buf.Write(data[:n])
    }
    if err != nil {
        log.Printf("Error reading from connection: %s\n", err)
        break
    }
}
log.Printf("Received: %s\n", buf.String())

关于出现错误时 PostgreSQL 不返回完整的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51004292/

有关出现错误时 PostgreSQL 不返回完整的响应的更多相关文章

  1. ruby-on-rails - Rails 常用字符串(用于通知和错误信息等) - 2

    大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje

  2. ruby - 为什么 4.1%2 使用 Ruby 返回 0.0999999999999996?但是 4.2%2==0.2 - 2

    为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返

  3. ruby-on-rails - 迷你测试错误 : "NameError: uninitialized constant" - 2

    我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test

  4. ruby - 检查字符串是否包含散列中的任何键并返回它包含的键的值 - 2

    我有一个包含多个键的散列和一个字符串,该字符串不包含散列中的任何键或包含一个键。h={"k1"=>"v1","k2"=>"v2","k3"=>"v3"}s="thisisanexamplestringthatmightoccurwithakeysomewhereinthestringk1(withspecialcharacterslike(^&*$#@!^&&*))"检查s是否包含h中的任何键的最佳方法是什么,如果包含,则返回它包含的键的值?例如,对于上面的h和s的例子,输出应该是v1。编辑:只有字符串是用户定义的。哈希将始终相同。 最佳答案

  5. ruby-on-rails - 如何在 Rails View 上显示错误消息? - 2

    我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c

  6. 使用 ACL 调用 upload_file 时出现 Ruby S3 "Access Denied"错误 - 2

    我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file

  7. ruby-on-rails - 错误 : Error installing pg: ERROR: Failed to build gem native extension - 2

    我克隆了一个rails仓库,我现在正尝试捆绑安装背景:OSXElCapitanruby2.2.3p173(2015-08-18修订版51636)[x86_64-darwin15]rails-v在您的Gemfile中列出的或native可用的任何gem源中找不到gem'pg(>=0)ruby​​'。运行bundleinstall以安装缺少的gem。bundleinstallFetchinggemmetadatafromhttps://rubygems.org/............Fetchingversionmetadatafromhttps://rubygems.org/...Fe

  8. ruby - #之间? Cooper 的 *Beginning Ruby* 中的错误或异常 - 2

    在Cooper的书BeginningRuby中,第166页有一个我无法重现的示例。classSongincludeComparableattr_accessor:lengthdef(other)@lengthother.lengthenddefinitialize(song_name,length)@song_name=song_name@length=lengthendenda=Song.new('Rockaroundtheclock',143)b=Song.new('BohemianRhapsody',544)c=Song.new('MinuteWaltz',60)a.betwee

  9. ruby-on-rails - 每次我尝试部署时,我都会得到 - (gcloud.preview.app.deploy) 错误响应 : [4] DEADLINE_EXCEEDED - 2

    我是Google云的新手,我正在尝试对其进行首次部署。我的第一个部署是RubyonRails项目。我基本上是在关注thisguideinthegoogleclouddocumentation.唯一的区别是我使用的是我自己的项目,而不是他们提供的“helloworld”项目。这是我的app.yaml文件runtime:customvm:trueentrypoint:bundleexecrackup-p8080-Eproductionconfig.ruresources:cpu:0.5memory_gb:1.3disk_size_gb:10当我转到我的项目目录并运行gcloudprevie

  10. ruby-on-rails - Rails 5 Active Record 记录无效错误 - 2

    我有两个Rails模型,即Invoice和Invoice_details。一个Invoice_details属于Invoice,一个Invoice有多个Invoice_details。我无法使用accepts_nested_attributes_forinInvoice通过Invoice模型保存Invoice_details。我收到以下错误:(0.2ms)BEGIN(0.2ms)ROLLBACKCompleted422UnprocessableEntityin25ms(ActiveRecord:4.0ms)ActiveRecord::RecordInvalid(Validationfa

随机推荐