Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[YSQL] Rare crash of TestPgRegressPgMiscIndependent#testPgRegressPgMiscIndependent #12185

Closed
d-uspenskiy opened this issue Apr 16, 2022 · 0 comments
Assignees
Labels
area/ysql Yugabyte SQL (YSQL) kind/bug This issue is a bug status/awaiting-triage Issue awaiting triage
Projects

Comments

@d-uspenskiy
Copy link
Contributor

Description

Sometimes the TestPgRegressPgMiscIndependent#testPgRegressPgMiscIndependent may crash with the following stack

Core was generated by `postgres: yugabyte yugabyte 127.0.0.1(55104) ALTER TABLE                      '.
Program terminated with signal SIGABRT, Aborted.
#0  __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
51	../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
[Current thread is 1 (Thread 0x7f3e63dc01c0 (LWP 1113326))]
(gdb) where
#0  __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
#1  0x00007f3e71d0c921 in __GI_abort () at abort.c:79
#2  0x0000000000c032c0 in ExceptionalCondition (conditionName=0xe1613f "!(IsTransactionState())", errorType=0xc93a14 "FailedAssertion", 
    fileName=0xe5c4c1 "../../../../../../../src/postgres/src/backend/utils/cache/relcache.c", lineNumber=2667) at ../../../../../../../src/postgres/src/backend/utils/error/assert.c:54
#3  0x0000000000be6947 in RelationIdGetRelation (relationId=16401) at ../../../../../../../src/postgres/src/backend/utils/cache/relcache.c:2667
#4  0x0000000000c42e82 in FetchUniqueConstraintName (relation_id=16401, dest=0x7fffc25c5010 "", max_size=254) at ../../../../../../../src/postgres/src/backend/utils/misc/pg_yb_utils.c:466
#5  0x00007f3e72bbee38 in yb::pggate::PgSession::PatchStatus (this=0x1e87580, status=..., relations=...) at ../../src/yb/yql/pggate/pg_session.cc:661
#6  0x00007f3e72bb775b in yb::pggate::PerformFuture::Get (this=0x21a10e0) at ../../src/yb/yql/pggate/pg_perform_future.cc:37
#7  0x00007f3e72c01d0a in yb::pggate::PgDocOp::~PgDocOp (this=0x21a1038) at ../../src/yb/yql/pggate/pg_doc_op.cc:212
#8  0x00007f3e72b722d3 in yb::pggate::PgDocReadOp::~PgDocReadOp (this=0x21a1038) at ../../src/yb/yql/pggate/pg_doc_op.h:413
#9  0x00007f3e72b71fa6 in std::__1::__shared_ptr_emplace<yb::pggate::PgDocReadOp, std::__1::allocator<yb::pggate::PgDocReadOp> >::__on_zero_shared (this=0x21a1020)
    at /opt/yb-build/thirdparty/yugabyte-db-thirdparty-v20211222064210-dd4872fe56-ubuntu1804-x86_64-clang11/installed/uninstrumented/libcxx/include/c++/v1/memory:3503
#10 0x00007f3e72b47d4d in std::__1::__shared_count::__release_shared (this=0x21a1020)
    at /opt/yb-build/thirdparty/yugabyte-db-thirdparty-v20211222064210-dd4872fe56-ubuntu1804-x86_64-clang11/installed/uninstrumented/libcxx/include/c++/v1/memory:3341
#11 0x00007f3e72b47cef in std::__1::__shared_weak_count::__release_shared (this=0x21a1020)
    at /opt/yb-build/thirdparty/yugabyte-db-thirdparty-v20211222064210-dd4872fe56-ubuntu1804-x86_64-clang11/installed/uninstrumented/libcxx/include/c++/v1/memory:3383
#12 0x00007f3e72b6ae5c in std::__1::shared_ptr<yb::pggate::PgDocOp>::~shared_ptr (this=0x1f4bcb0)
    at /opt/yb-build/thirdparty/yugabyte-db-thirdparty-v20211222064210-dd4872fe56-ubuntu1804-x86_64-clang11/installed/uninstrumented/libcxx/include/c++/v1/memory:4098
#13 0x00007f3e72bd7097 in yb::pggate::PgDml::~PgDml (this=0x1f4bb70) at ../../src/yb/yql/pggate/pg_dml.cc:63
#14 0x00007f3e72be7a43 in yb::pggate::PgDmlRead::~PgDmlRead (this=0x1f4bb70) at ../../src/yb/yql/pggate/pg_dml_read.cc:104
#15 0x00007f3e72bf31c8 in yb::pggate::PgSelect::~PgSelect (this=0x1f4bb70) at ../../src/yb/yql/pggate/pg_select.cc:37
#16 0x00007f3e72bf31ec in yb::pggate::PgSelect::~PgSelect (this=0x1f4bb70) at ../../src/yb/yql/pggate/pg_select.cc:36
#17 0x00007f3e72c2c91f in std::__1::default_delete<yb::pggate::PgMemctx::Registrable>::operator() (this=0x7fffc25c5488, __ptr=0x1f4bb70)
    at /opt/yb-build/thirdparty/yugabyte-db-thirdparty-v20211222064210-dd4872fe56-ubuntu1804-x86_64-clang11/installed/uninstrumented/libcxx/include/c++/v1/memory:2262
#18 0x00007f3e72c28006 in boost::intrusive::list_impl<boost::intrusive::bhtraits<yb::pggate::PgMemctx::Registrable, boost::intrusive::list_node_traits<void*>, (boost::intrusive::link_mode_type)1, boost::intrusive::dft_tag, 1u>, unsigned long, true, void>::clear_and_dispose<std::__1::default_delete<yb::pggate::PgMemctx::Registrable> > (this=0x21f4868, disposer=...)
    at /opt/yb-build/thirdparty/yugabyte-db-thirdparty-v20211222064210-dd4872fe56-ubuntu1804-x86_64-clang11/installed/uninstrumented/include/boost/intrusive/list.hpp:754
#19 0x00007f3e72c27478 in yb::pggate::PgMemctx::Clear (this=0x21f4838) at ../../src/yb/yql/pggate/pg_memctx.cc:80
#20 0x00007f3e72c273da in yb::pggate::PgMemctx::~PgMemctx (this=0x21f4838) at ../../src/yb/yql/pggate/pg_memctx.cc:29
#21 0x00007f3e72c2a1e6 in std::__1::__shared_ptr_emplace<yb::pggate::PgMemctx, std::__1::allocator<yb::pggate::PgMemctx> >::__on_zero_shared (this=0x21f4820)
    at /opt/yb-build/thirdparty/yugabyte-db-thirdparty-v20211222064210-dd4872fe56-ubuntu1804-x86_64-clang11/installed/uninstrumented/libcxx/include/c++/v1/memory:3503
#22 0x00007f3e72b47d4d in std::__1::__shared_count::__release_shared (this=0x21f4820)
    at /opt/yb-build/thirdparty/yugabyte-db-thirdparty-v20211222064210-dd4872fe56-ubuntu1804-x86_64-clang11/installed/uninstrumented/libcxx/include/c++/v1/memory:3341
#23 0x00007f3e72b47cef in std::__1::__shared_weak_count::__release_shared (this=0x21f4820)
    at /opt/yb-build/thirdparty/yugabyte-db-thirdparty-v20211222064210-dd4872fe56-ubuntu1804-x86_64-clang11/installed/uninstrumented/libcxx/include/c++/v1/memory:3383
#24 0x00007f3e72c27c9c in std::__1::shared_ptr<yb::pggate::PgMemctx>::~shared_ptr (this=0x1f552c8)
    at /opt/yb-build/thirdparty/yugabyte-db-thirdparty-v20211222064210-dd4872fe56-ubuntu1804-x86_64-clang11/installed/uninstrumented/libcxx/include/c++/v1/memory:4098
#25 0x00007f3e72c29c0e in std::__1::pair<yb::pggate::PgMemctx* const, std::__1::shared_ptr<yb::pggate::PgMemctx> >::~pair (this=0x1f552c0)
    at /opt/yb-build/thirdparty/yugabyte-db-thirdparty-v20211222064210-dd4872fe56-ubuntu1804-x86_64-clang11/installed/uninstrumented/libcxx/include/c++/v1/utility:297
#26 0x00007f3e72c29be9 in std::__1::allocator_traits<std::__1::allocator<std::__1::__hash_node<std::__1::__hash_value_type<yb::pggate::PgMemctx*, std::__1::shared_ptr<yb::pggate::PgMemctx> >, void*> > >::__destroy<std::__1::pair<yb::pggate::PgMemctx* const, std::__1::shared_ptr<yb::pggate::PgMemctx> > > (__p=0x1f552c0)
    at /opt/yb-build/thirdparty/yugabyte-db-thirdparty-v20211222064210-dd4872fe56-ubuntu1804-x86_64-clang11/installed/uninstrumented/libcxx/include/c++/v1/memory:1710
#27 0x00007f3e72c29afd in std::__1::allocator_traits<std::__1::allocator<std::__1::__hash_node<std::__1::__hash_value_type<yb::pggate::PgMemctx*, std::__1::shared_ptr<yb::pggate::PgMemctx> >, void*> > >::destroy<std::__1::pair<yb::pggate::PgMemctx* const, std::__1::shared_ptr<yb::pggate::PgMemctx> > > (__a=..., __p=0x1f552c0)
    at /opt/yb-build/thirdparty/yugabyte-db-thirdparty-v20211222064210-dd4872fe56-ubuntu1804-x86_64-clang11/installed/uninstrumented/libcxx/include/c++/v1/memory:1544
#28 0x00007f3e72c2bfb3 in std::__1::__hash_node_destructor<std::__1::allocator<std::__1::__hash_node<std::__1::__hash_value_type<yb::pggate::PgMemctx*, std::__1::shared_ptr<yb::pggate::PgMemctx> >, void*> > >::operator() (this=0x7fffc25c56d0, __p=0x1f552b0)
    at /opt/yb-build/thirdparty/yugabyte-db-thirdparty-v20211222064210-dd4872fe56-ubuntu1804-x86_64-clang11/installed/uninstrumented/libcxx/include/c++/v1/__hash_table:846
---Type <return> to continue, or q <return> to quit---
#29 0x00007f3e72c2bf4f in std::__1::unique_ptr<std::__1::__hash_node<std::__1::__hash_value_type<yb::pggate::PgMemctx*, std::__1::shared_ptr<yb::pggate::PgMemctx> >, void*>, std::__1::__hash_node_destructor<std::__1::allocator<std::__1::__hash_node<std::__1::__hash_value_type<yb::pggate::PgMemctx*, std::__1::shared_ptr<yb::pggate::PgMemctx> >, void*> > > >::reset (this=0x7fffc25c56c8, __p=0x0)
    at /opt/yb-build/thirdparty/yugabyte-db-thirdparty-v20211222064210-dd4872fe56-ubuntu1804-x86_64-clang11/installed/uninstrumented/libcxx/include/c++/v1/memory:2517
#30 0x00007f3e72c2b2b9 in std::__1::unique_ptr<std::__1::__hash_node<std::__1::__hash_value_type<yb::pggate::PgMemctx*, std::__1::shared_ptr<yb::pggate::PgMemctx> >, void*>, std::__1::__hash_node_destructor<std::__1::allocator<std::__1::__hash_node<std::__1::__hash_value_type<yb::pggate::PgMemctx*, std::__1::shared_ptr<yb::pggate::PgMemctx> >, void*> > > >::~unique_ptr (this=0x7fffc25c56c8)
    at /opt/yb-build/thirdparty/yugabyte-db-thirdparty-v20211222064210-dd4872fe56-ubuntu1804-x86_64-clang11/installed/uninstrumented/libcxx/include/c++/v1/memory:2471
#31 0x00007f3e72c2c4ff in std::__1::__hash_table<std::__1::__hash_value_type<yb::pggate::PgMemctx*, std::__1::shared_ptr<yb::pggate::PgMemctx> >, std::__1::__unordered_map_hasher<yb::pggate::PgMemctx*, std::__1::__hash_value_type<yb::pggate::PgMemctx*, std::__1::shared_ptr<yb::pggate::PgMemctx> >, std::__1::hash<yb::pggate::PgMemctx*>, true>, std::__1::__unordered_map_equal<yb::pggate::PgMemctx*, std::__1::__hash_value_type<yb::pggate::PgMemctx*, std::__1::shared_ptr<yb::pggate::PgMemctx> >, std::__1::equal_to<yb::pggate::PgMemctx*>, true>, std::__1::allocator<std::__1::__hash_value_type<yb::pggate::PgMemctx*, std::__1::shared_ptr<yb::pggate::PgMemctx> > > >::erase (
    this=0x7f3e72ef0708 <yb::pggate::(anonymous namespace)::postgres_process_memctxs>, __p=...)
    at /opt/yb-build/thirdparty/yugabyte-db-thirdparty-v20211222064210-dd4872fe56-ubuntu1804-x86_64-clang11/installed/uninstrumented/libcxx/include/c++/v1/__hash_table:2600
#32 0x00007f3e72c2c486 in std::__1::__hash_table<std::__1::__hash_value_type<yb::pggate::PgMemctx*, std::__1::shared_ptr<yb::pggate::PgMemctx> >, std::__1::__unordered_map_hasher<yb::pggate::PgMemctx*, std::__1::__hash_value_type<yb::pggate::PgMemctx*, std::__1::shared_ptr<yb::pggate::PgMemctx> >, std::__1::hash<yb::pggate::PgMemctx*>, true>, std::__1::__unordered_map_equal<yb::pggate::PgMemctx*, std::__1::__hash_value_type<yb::pggate::PgMemctx*, std::__1::shared_ptr<yb::pggate::PgMemctx> >, std::__1::equal_to<yb::pggate::PgMemctx*>, true>, std::__1::allocator<std::__1::__hash_value_type<yb::pggate::PgMemctx*, std::__1::shared_ptr<yb::pggate::PgMemctx> > > >::__erase_unique<yb::pggate::PgMemctx*> (
    this=0x7f3e72ef0708 <yb::pggate::(anonymous namespace)::postgres_process_memctxs>, __k=@0x7fffc25c5810: 0x21f4838)
    at /opt/yb-build/thirdparty/yugabyte-db-thirdparty-v20211222064210-dd4872fe56-ubuntu1804-x86_64-clang11/installed/uninstrumented/libcxx/include/c++/v1/__hash_table:2638
#33 0x00007f3e72c27d6d in std::__1::unordered_map<yb::pggate::PgMemctx*, std::__1::shared_ptr<yb::pggate::PgMemctx>, std::__1::hash<yb::pggate::PgMemctx*>, std::__1::equal_to<yb::pggate::PgMemctx*>, std::__1::allocator<std::__1::pair<yb::pggate::PgMemctx* const, std::__1::shared_ptr<yb::pggate::PgMemctx> > > >::erase (this=0x7f3e72ef0708 <yb::pggate::(anonymous namespace)::postgres_process_memctxs>, __k=@0x7fffc25c5810: 0x21f4838)
    at /opt/yb-build/thirdparty/yugabyte-db-thirdparty-v20211222064210-dd4872fe56-ubuntu1804-x86_64-clang11/installed/uninstrumented/libcxx/include/c++/v1/unordered_map:1197
#34 0x00007f3e72c2766d in yb::pggate::PgMemctx::Destroy (handle=0x21f4838) at ../../src/yb/yql/pggate/pg_memctx.cc:54
#35 0x00007f3e72b57ba3 in yb::pggate::PgApiImpl::DestroyMemctx (memctx=0x21f4838) at ../../src/yb/yql/pggate/pggate.cc:366
#36 0x00007f3e72b32320 in YBCPgDestroyMemctx (memctx=0x21f4838) at ../../src/yb/yql/pggate/ybc_pggate.cc:172
#37 0x0000000000c53f31 in MemoryContextDelete (context=0x21963e0) at ../../../../../../../src/postgres/src/backend/utils/mmgr/mcxt.c:306
#38 0x0000000000c57ac6 in PortalDrop (portal=0x1e56b80, isTopCommit=false) at ../../../../../../../src/postgres/src/backend/utils/mmgr/portalmem.c:599
#39 0x0000000000c587a0 in AtCleanup_Portals () at ../../../../../../../src/postgres/src/backend/utils/mmgr/portalmem.c:907
#40 0x00000000005843ce in CleanupTransaction () at ../../../../../../../src/postgres/src/backend/access/transam/xact.c:2854
#41 0x0000000000585b86 in AbortCurrentTransaction () at ../../../../../../../src/postgres/src/backend/access/transam/xact.c:3315
#42 0x0000000000a18713 in PostgresMain (argc=1, argv=0x1dd5f90, dbname=0x1dd5cc0 "yugabyte", username=0x1dd5c98 "yugabyte") at ../../../../../../src/postgres/src/backend/tcop/postgres.c:4977
#43 0x00000000009489af in BackendRun (port=0x1e0a140) at ../../../../../../src/postgres/src/backend/postmaster/postmaster.c:4470
#44 0x0000000000947bd3 in BackendStartup (port=0x1e0a140) at ../../../../../../src/postgres/src/backend/postmaster/postmaster.c:4136
#45 0x00000000009468da in ServerLoop () at ../../../../../../src/postgres/src/backend/postmaster/postmaster.c:1754
#46 0x0000000000943df2 in PostmasterMain (argc=19, argv=0x1dbade0) at ../../../../../../src/postgres/src/backend/postmaster/postmaster.c:1417
#47 0x0000000000848e47 in PostgresServerProcessMain (argc=19, argv=0x1dbade0) at ../../../../../../src/postgres/src/backend/main/main.c:234
#48 0x0000000000849312 in main ()

Possible reason is that ysql reads data from system catalog when processing the duplicate key value violates unique constraint error response to fetch constraints name.

@d-uspenskiy d-uspenskiy added kind/bug This issue is a bug area/ysql Yugabyte SQL (YSQL) status/awaiting-triage Issue awaiting triage labels Apr 16, 2022
@d-uspenskiy d-uspenskiy self-assigned this Apr 16, 2022
@d-uspenskiy d-uspenskiy added this to Backlog in YSQL via automation Apr 16, 2022
d-uspenskiy added a commit that referenced this issue Apr 29, 2022
…ggate to postgres

Summary:
In postgres the `duplicate key` error message contains constraint name.

```
ERROR:  duplicate key value violates unique constraint "t1_pkey"
```

Nowadays YSQL fetches constraint name in the `PgSession::PatchStatus` method by using the callback of postgres callback.
This strategy leads to undesired behavior because the `PgSession::PatchStatus` might be called from the stack after receiving response of flushed operations. And postgres callback might make a read from sys table (in case constraint name is not in a cache yet). And the read from sys table will flush the buffered operations once again before sending read RPC. As a result, patching status on receiving response of flushing operations might trigger flushing of the next buffer of operations. And second flushing might fail with another error (including another `duplicate key`). As a result initial error will be substituted with new one.

Here is the example scenario:
```
CREATE TABLE t1(k INT PRIMARY KEY);
CREATE TABLE t2(k INT PRIMARY KEY);
INSERT INTO t1 VALUES(1);
INSERT INTO t2 VALUES(1);
SET ysql_session_max_batch_size = 3;
DO $$
BEGIN
  INSERT INTO t1 VALUES(3), (2), (1); -- the 'duplicate key' error is raised here because k = 1 already exists in t1
  INSERT INTO t2 VALUES(1);           -- the 'duplicate key' error is raised here because k = 1 already exists in t2, but vanilla postgres will not reach this line
  INSERT INTO t1 VALUES(2);
END
$$;

ERROR:  duplicate key value violates unique constraint "t2_pkey"
```

Instead of error related to `t1_pkey` YSQL shows error related to `t2_pkey`.

Another problem of resolving constraint name in the `PgSession::PatchStatus` method is that this message can be called from cleanup procedures (destructors) and it is not possible to make a read from this stack.

Example:
```
PgDocOp::~PgDocOp() {
  // Wait for result in case request was sent.
  // Operation can be part of transaction it is necessary to complete it before transaction commit.
  if (response_.Valid()) {
    WARN_NOT_OK(ResultToStatus(response_.Get()), "Operation completion failed");  <-- the PgSession::PatchStatus method will be called in case of error
  }
}
```

This scenario is the root cause of the `org.yb.pgsql.TestPgRegressPgMiscIndependent#testPgRegressPgMiscIndependent` test failures.

Solution is to postpone constraint name resolving until the point where postgres uses error message (the `HandleYBStatusAtErrorLevel` function). For this purpose in the `PgSession::PatchStatus` method constraint's id is added to error status.

**Note:**
1. The `TestOperationBatching` unit test is removed. All its scenarios are re-implemented in newly created `PgOpBufferingTest` C++ unit test.
2. The `org.yb.pgsql.TestPgRegressPgMiscIndependent#testPgRegressPgMiscIndependent` unit test is fixed by current diff.

Test Plan:
New unit test was introduced

```
./yb_build.sh --gtest_filter PgOpBufferingTest.MultipleConstraintsConflict
```

Reviewers: nli, alex, pjain

Reviewed By: pjain

Subscribers: sergei, yql

Differential Revision: https://phabricator.dev.yugabyte.com/D16581
YSQL automation moved this from Backlog to Done Apr 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/ysql Yugabyte SQL (YSQL) kind/bug This issue is a bug status/awaiting-triage Issue awaiting triage
Projects
YSQL
  
Done
Development

No branches or pull requests

1 participant