我写了一个正则表达式来解析 PostgreSQL 错误,试图向用户显示哪个字段有重复数据。正则表达式是这个:
^DETAIL:.[^\(]+.(.[^\)]+).[^\(]+.(.[^\)]+). already exists
如果你像这样针对正确的消息运行它,它会非常快(https://regex101.com/r/GZuREV/1):
ERROR: duplicate key value violates unique constraint "uq_content_block_internal_name_store_id" DETAIL: Key (lower(internal_name::text), store_id)=(some content block-32067683, 0c6d20a7-d843-44f3-af9c-4a2cf2a47e4c) already exists.
但是,如果 PostgreSQL 发出另一条消息,如下所示,我的机器中 python 将花费大约 30 秒的时间来回答 ( https://regex101.com/r/GZuREV/2 )。
ERROR: null value in column "active" violates not-null constraint DETAIL: Failing row contains (2018-08-16 14:23:52.214591+00, 2018-08-16 14:23:52.214591+00, null, 6f6d1bc9-c47e-46f8-b220-dae49bd58090, bf24d26e-4871-4335-9f18-83c5a52f1b3a, Some Product-a1c03dde-2de9-401c-92d5-5c1500908984, {"de_DE": "Fugit tempore voluptas quos est vitae.", "en_GB": "Qu..., {"de_DE": "Fuga reprehenderit nobis reprehenderit natus magni es..., {"de_DE": "Fuga provident dolorum. Corrupti sunt in tempore quae..., my-product-53077578, SKU-53075778, 600, 4300dc25-04e2-4193-94c0-8ee97b636739, 52553d24-6d1c-4ce6-89f9-4ad765599040, null, 38089c3c-423f-430c-b211-ab7a57dbcc13, 7d7dc30e-b06b-48b7-b674-26d4f705583b, null, {}, 0, null, 9980, 100, 1, 5).
如果转到 regex101 链接,您可以看到,如果您切换到不同的语言,如 php 或 go,它们都很快返回说没有找到匹配项,但如果您选择 python 或 javascript,您将超时。
我的快速脏修复是这样的:
match = 'already exists' in error_message and compiled_regex.search(error_message)
你认为这可能是什么原因造成的?会不会是贪婪的运营商在我达到我想要的数据之前消费?
蝴蝶刀刀
阿波罗的战车
DIEA
繁花不似锦