diff --git a/documentdb_errors.csv b/documentdb_errors.csv index a627f0be..eed6add8 100644 --- a/documentdb_errors.csv +++ b/documentdb_errors.csv @@ -510,4 +510,5 @@ OrdinalPosition,ErrorName 509,Location7555702 510,Location3041704 511,Location4341107 -512,InsufficientPrivilege \ No newline at end of file +512,InsufficientPrivilege +513,NotWritablePrimary \ No newline at end of file diff --git a/error_mappings.csv b/error_mappings.csv index c347bc33..de42e4ae 100644 --- a/error_mappings.csv +++ b/error_mappings.csv @@ -50,6 +50,7 @@ ErrorMapping,ErrorName 361,CollectionUUIDMismatch 8000,UserCountLimitExceeded 10065,Location10065 +10107,NotWritablePrimary 10334,BsonObjectTooLarge 11000,DuplicateKey 12587,BackgroundOperationInProgressForNamespace diff --git a/pg_documentdb/include/commands/commands_common.h b/pg_documentdb/include/commands/commands_common.h index ef1cdaf0..d6ef6b4a 100644 --- a/pg_documentdb/include/commands/commands_common.h +++ b/pg_documentdb/include/commands/commands_common.h @@ -14,6 +14,9 @@ #include #include #include +#include +#include +#include /* * Maximum size of a output bson document is 16MB. @@ -86,4 +89,55 @@ void SetExplicitStatementTimeout(int timeoutMilliseconds); void CommitWriteProcedureAndReacquireCollectionLock(MongoCollection *collection, bool setSnapshot); +extern bool SkipEnforceTransactionReadOnly; +extern bool SimulateRecoveryState; +extern bool DocumentDBPGReadOnlyForDiskFull; + +inline static void +ThrowIfServerOrTransactionReadOnly(void) +{ + if (!XactReadOnly) + { + return; + } + + if (SkipEnforceTransactionReadOnly) + { + return; + } + + if (RecoveryInProgress() || SimulateRecoveryState) + { + /* + * Skip these checks in recovery mode - let the system throw the appropriate + * error. + */ + return; + } + + if (DocumentDBPGReadOnlyForDiskFull) + { + ereport(ERROR, (errcode(ERRCODE_DISK_FULL), errmsg( + "Can't execute write operation, The database disk is full"))); + } + + /* Error is coming because the server has been put in a read-only state, but we're a writable node (primary) */ + if (DefaultXactReadOnly) + { + ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_NOTWRITABLEPRIMARY), + errmsg( + "cannot execute write operations when the server is in a read-only state."), + errdetail("the default transaction is read-only"), + errdetail_log( + "cannot execute write operations when default_transaction_read_only is set to true"))); + } + + /* Error is coming because the transaction has been in a readonly state */ + ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_OPERATIONNOTSUPPORTEDINTRANSACTION), + errmsg( + "cannot execute write operation when the transaction is in a read-only state."), + errdetail("the current transaction is read-only"))); +} + + #endif diff --git a/pg_documentdb/src/aggregation/bson_aggregation_pipeline.c b/pg_documentdb/src/aggregation/bson_aggregation_pipeline.c index 0bc7ad71..bca63b46 100644 --- a/pg_documentdb/src/aggregation/bson_aggregation_pipeline.c +++ b/pg_documentdb/src/aggregation/bson_aggregation_pipeline.c @@ -1334,6 +1334,7 @@ GenerateAggregationQuery(text *database, pgbson *aggregationSpec, QueryData *que else if (query->commandType == CMD_MERGE) { /* CMD_MERGE is case when pipeline has output stage ($merge or $out) result will be always single batch. */ + ThrowIfServerOrTransactionReadOnly(); queryData->cursorKind = QueryCursorType_SingleBatch; } else if (queryData->cursorKind == QueryCursorType_Unspecified) diff --git a/pg_documentdb/src/commands/create_collection_view.c b/pg_documentdb/src/commands/create_collection_view.c index 6ad3440e..5c570fb6 100644 --- a/pg_documentdb/src/commands/create_collection_view.c +++ b/pg_documentdb/src/commands/create_collection_view.c @@ -126,6 +126,7 @@ command_create_collection_view(PG_FUNCTION_ARGS) GetMongoCollectionOrViewByNameDatum( databaseDatum, createDatum, NoLock); + ThrowIfServerOrTransactionReadOnly(); if (collection != NULL) { /* Collection exists validate options */ diff --git a/pg_documentdb/src/commands/create_indexes.c b/pg_documentdb/src/commands/create_indexes.c index 384d9611..37184459 100644 --- a/pg_documentdb/src/commands/create_indexes.c +++ b/pg_documentdb/src/commands/create_indexes.c @@ -441,6 +441,7 @@ command_create_indexes_non_concurrently(PG_FUNCTION_ARGS) skip_check_collection_create = PG_GETARG_BOOL(2); } + ThrowIfServerOrTransactionReadOnly(); pgbson *arg = PgbsonDeduplicateFields(PG_GETARG_PGBSON(1)); CreateIndexesArg createIndexesArg = ParseCreateIndexesArg(dbNameDatum, arg); diff --git a/pg_documentdb/src/commands/create_indexes_background.c b/pg_documentdb/src/commands/create_indexes_background.c index 6af5d5e4..5706a710 100644 --- a/pg_documentdb/src/commands/create_indexes_background.c +++ b/pg_documentdb/src/commands/create_indexes_background.c @@ -580,6 +580,8 @@ command_create_indexes_background(PG_FUNCTION_ARGS) text *databaseDatum = PG_GETARG_TEXT_P(0); pgbson *indexSpec = PG_GETARG_PGBSON(1); + ThrowIfServerOrTransactionReadOnly(); + StringInfo submitIndexBuildRequestQuery = makeStringInfo(); appendStringInfo(submitIndexBuildRequestQuery, "SELECT %s.create_indexes_background_internal(%s,%s)", diff --git a/pg_documentdb/src/commands/delete.c b/pg_documentdb/src/commands/delete.c index c66c0467..6962549b 100644 --- a/pg_documentdb/src/commands/delete.c +++ b/pg_documentdb/src/commands/delete.c @@ -184,6 +184,8 @@ command_delete(PG_FUNCTION_ARGS) ereport(ERROR, (errmsg("return type must be a row type"))); } + ThrowIfServerOrTransactionReadOnly(); + bson_iter_t deleteCommandIter; PgbsonInitIterator(deleteSpec, &deleteCommandIter); @@ -995,6 +997,7 @@ command_delete_worker(PG_FUNCTION_ARGS) pgbsonsequence *specDocuments = PG_GETARG_MAYBE_NULL_PGBSON_SEQUENCE(4); text *transactionId = PG_ARGISNULL(5) ? NULL : PG_GETARG_TEXT_PP(5); + ThrowIfServerOrTransactionReadOnly(); AllowNestedDistributionInCurrentTransaction(); pgbsonelement commandElement; PgbsonToSinglePgbsonElement(deleteInternalSpec, &commandElement); diff --git a/pg_documentdb/src/commands/drop_collection.c b/pg_documentdb/src/commands/drop_collection.c index cc4a53d7..df55a58f 100644 --- a/pg_documentdb/src/commands/drop_collection.c +++ b/pg_documentdb/src/commands/drop_collection.c @@ -27,6 +27,7 @@ #include "utils/index_utils.h" #include "utils/guc_utils.h" #include "utils/version_utils.h" +#include "commands/commands_common.h" #include "api_hooks.h" @@ -49,6 +50,7 @@ command_drop_collection(PG_FUNCTION_ARGS) PG_RETURN_BOOL(false); } + ThrowIfServerOrTransactionReadOnly(); Datum databaseNameDatum = PG_GETARG_DATUM(0); Datum collectionNameDatum = PG_GETARG_DATUM(1); diff --git a/pg_documentdb/src/commands/find_and_modify.c b/pg_documentdb/src/commands/find_and_modify.c index f45e0cb1..3c70d898 100644 --- a/pg_documentdb/src/commands/find_and_modify.c +++ b/pg_documentdb/src/commands/find_and_modify.c @@ -160,6 +160,7 @@ command_find_and_modify(PG_FUNCTION_ARGS) ereport(ERROR, (errmsg("return type must be a row type"))); } + ThrowIfServerOrTransactionReadOnly(); FindAndModifySpec spec = ParseFindAndModifyMessage(message); Datum collectionNameDatum = CStringGetTextDatum(spec.collectionName); diff --git a/pg_documentdb/src/commands/insert.c b/pg_documentdb/src/commands/insert.c index 7fa67054..d1061db3 100644 --- a/pg_documentdb/src/commands/insert.c +++ b/pg_documentdb/src/commands/insert.c @@ -907,6 +907,8 @@ CommandInsertCore(PG_FUNCTION_ARGS, bool isTransactional, MemoryContext allocCon ereport(ERROR, (errmsg("insert document cannot be NULL"))); } + ThrowIfServerOrTransactionReadOnly(); + Datum databaseNameDatum = PG_GETARG_DATUM(0); pgbson *insertSpec = PG_GETARG_PGBSON(1); @@ -1098,6 +1100,7 @@ command_insert_worker(PG_FUNCTION_ARGS) "Explicit shardOid must be set - this is a server bug"))); } + ThrowIfServerOrTransactionReadOnly(); AllowNestedDistributionInCurrentTransaction(); pgbsonelement element = { 0 }; PgbsonToSinglePgbsonElement(insertInternalSpec, &element); diff --git a/pg_documentdb/src/commands/update.c b/pg_documentdb/src/commands/update.c index 317d818a..01a2dfb0 100644 --- a/pg_documentdb/src/commands/update.c +++ b/pg_documentdb/src/commands/update.c @@ -499,6 +499,7 @@ PerformUpdateCore(Datum databaseNameDatum, pgbson *updateSpec, TupleDesc resultTupDesc, bool isTransactional, MemoryContext allocContext) { + ThrowIfServerOrTransactionReadOnly(); bson_iter_t updateCommandIter; PgbsonInitIterator(updateSpec, &updateCommandIter); @@ -2119,6 +2120,8 @@ command_update_worker(PG_FUNCTION_ARGS) "update spec or update documents argument must be not null."))); } + ThrowIfServerOrTransactionReadOnly(); + WorkerUpdateParam params; memset(¶ms, 0, sizeof(WorkerUpdateParam)); DeserializeUpdateWorkerSpec(updateInternalSpec, ¶ms); diff --git a/pg_documentdb/src/configs/feature_flag_configs.c b/pg_documentdb/src/configs/feature_flag_configs.c index 38512a88..97ea3675 100644 --- a/pg_documentdb/src/configs/feature_flag_configs.c +++ b/pg_documentdb/src/configs/feature_flag_configs.c @@ -155,6 +155,9 @@ bool AlterCreationTimeInCompleteUpgrade = ALTER_CREATION_TIME_IN_COMPLETE_UPGRAD #define DEFAULT_ENABLE_USERNAME_PASSWORD_CONSTRAINTS true bool EnableUsernamePasswordConstraints = DEFAULT_ENABLE_USERNAME_PASSWORD_CONSTRAINTS; +#define DEFAULT_SKIP_ENFORCE_TRANSACTION_READ_ONLY false +bool SkipEnforceTransactionReadOnly = DEFAULT_SKIP_ENFORCE_TRANSACTION_READ_ONLY; + /* FEATURE FLAGS END */ void @@ -443,4 +446,12 @@ InitializeFeatureFlagConfigurations(const char *prefix, const char *newGucPrefix NULL, &EnableUsernamePasswordConstraints, DEFAULT_ENABLE_USERNAME_PASSWORD_CONSTRAINTS, PGC_USERSET, 0, NULL, NULL, NULL); + + DefineCustomBoolVariable( + psprintf("%s.skipEnforceTransactionReadOnly", newGucPrefix), + gettext_noop( + "Whether or not to skip enforcing transaction read only."), + NULL, &SkipEnforceTransactionReadOnly, + DEFAULT_SKIP_ENFORCE_TRANSACTION_READ_ONLY, + PGC_USERSET, 0, NULL, NULL, NULL); } diff --git a/pg_documentdb/src/configs/system_configs.c b/pg_documentdb/src/configs/system_configs.c index e9b0725f..4b481c52 100644 --- a/pg_documentdb/src/configs/system_configs.c +++ b/pg_documentdb/src/configs/system_configs.c @@ -125,6 +125,15 @@ bool EnableUserCrud = DEFAULT_ENABLE_USER_CRUD; #define DEFAULT_VECTOR_ITERATIVE_SCAN_MODE VectorIterativeScan_RELAXED_ORDER int VectorPreFilterIterativeScanMode = DEFAULT_VECTOR_ITERATIVE_SCAN_MODE; +/* Note that this is explicitly left disabled + * This is primarily because the operator that sets default_transaction_readonly + * would want to avoid new writes (perhaps due to high disk usage) and a background + * job that can go and delete documents can produce WAL files and can exacerbate + * the issue by putting disk load. Make this explicitly opt-in. + */ +#define DEFAULT_ENABLE_TTL_JOBS_ON_READ_ONLY false +bool EnableTtlJobsOnReadOnly = DEFAULT_ENABLE_TTL_JOBS_ON_READ_ONLY; + void InitializeSystemConfigurations(const char *prefix, const char *newGucPrefix) { @@ -341,6 +350,14 @@ InitializeSystemConfigurations(const char *prefix, const char *newGucPrefix) NULL, &EnableUserCrud, DEFAULT_ENABLE_USER_CRUD, PGC_USERSET, 0, NULL, NULL, NULL); + DefineCustomBoolVariable( + psprintf("%s.enableTTLJobsOnReadOnly", newGucPrefix), + gettext_noop( + "Enables TTL jobs on read-only nodes. This will override" + " the default_transaction_readonly on the TTL job only."), + NULL, &EnableTtlJobsOnReadOnly, DEFAULT_ENABLE_TTL_JOBS_ON_READ_ONLY, + PGC_USERSET, 0, NULL, NULL, NULL); + DefineCustomEnumVariable( psprintf("%s.vectorPreFilterIterativeScanMode", newGucPrefix), gettext_noop( diff --git a/pg_documentdb/src/test/regress/basic_schedule b/pg_documentdb/src/test/regress/basic_schedule index 7216bf8b..a9ab85e3 100644 --- a/pg_documentdb/src/test/regress/basic_schedule +++ b/pg_documentdb/src/test/regress/basic_schedule @@ -13,7 +13,7 @@ test: bson_aggregation_pipeline_tests_facet_group_explain_pg!MAJOR_VERSION! bson # Cannot run this concurrently due to currentOp tests test: bson_aggregation_pipeline_tests_coll_agnostic test: bson_aggregation_pipeline_tests_merge_objects_group bson_aggregation_cursor_tests -test: bson_aggregation_pipeline_tests_stddevpopsamp_group +test: bson_aggregation_pipeline_tests_stddevpopsamp_group readonly_transaction_tests test: commands_create_indexes_background commands_create_view_tests test: collection_management bson_aggregation_cursor_tests_txn test: bson_aggregation_object_operators_tests bson_aggregation_pipeline_diagnostic_command_tests bson_aggregation_functions_nested_tests diff --git a/pg_documentdb/src/test/regress/expected/readonly_transaction_tests.out b/pg_documentdb/src/test/regress/expected/readonly_transaction_tests.out new file mode 100644 index 00000000..8bdf5093 --- /dev/null +++ b/pg_documentdb/src/test/regress/expected/readonly_transaction_tests.out @@ -0,0 +1,83 @@ +SET search_path TO documentdb_api,documentdb_core,documentdb_api_catalog; +SET documentdb.next_collection_id TO 2100; +SET documentdb.next_collection_index_id TO 2100; +SELECT documentdb_api.create_collection_view('db', '{ "create": "test" }'); +NOTICE: creating collection + create_collection_view +---------------------------------------- + { "ok" : { "$numberDouble" : "1.0" } } +(1 row) + +SELECT documentdb_api.insert_one('db', 'test', '{ "_id": 1, "a": 1, "ttl": 0 }'); + insert_one +---------------------------------------------------------------------- + { "n" : { "$numberInt" : "1" }, "ok" : { "$numberDouble" : "1.0" } } +(1 row) + +SELECT documentdb_api_internal.create_indexes_non_concurrently('db', '{ "createIndexes": "test", "indexes": [ { "key": { "ttl": 1 }, "name": "ttlIndex", "expireAfterSeconds": 300 } ] }', TRUE); + create_indexes_non_concurrently +-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + { "raw" : { "defaultShard" : { "numIndexesBefore" : { "$numberInt" : "1" }, "numIndexesAfter" : { "$numberInt" : "2" }, "createdCollectionAutomatically" : false, "ok" : { "$numberInt" : "1" } } }, "ok" : { "$numberInt" : "1" } } +(1 row) + +-- set the default transaction mode. +set default_transaction_read_only = on; +-- should fail gracefully. +SELECT documentdb_api.create_collection_view('db', '{ "create": "collection" }'); +ERROR: cannot execute write operations when the server is in a read-only state. +DETAIL: the default transaction is read-only +SELECT documentdb_api.insert('db', '{ "insert": "test", "documents": [ { "a": 1 }] }'); +ERROR: cannot execute write operations when the server is in a read-only state. +DETAIL: the default transaction is read-only +SELECT documentdb_api.update('db', '{ "update": "test", "updates": [ { "q": { "a": 1 }, "u": { "$set": { "b": 1 }} }] }'); +ERROR: cannot execute write operations when the server is in a read-only state. +DETAIL: the default transaction is read-only +SELECT documentdb_api.delete('db', '{ "delete": "test", "deletes": [ { "q": { "a": 1 } }] }'); +ERROR: cannot execute write operations when the server is in a read-only state. +DETAIL: the default transaction is read-only +SELECT document FROM documentdb_api_catalog.bson_aggregation_pipeline('db', '{ "aggregate": "test", "pipeline": [ { "$merge": { "into": "test" } } ] }'); +ERROR: cannot execute write operations when the server is in a read-only state. +DETAIL: the default transaction is read-only +SELECT documentdb_api.drop_collection('db', 'collection'); +ERROR: cannot execute write operations when the server is in a read-only state. +DETAIL: the default transaction is read-only +-- same command works when transaction explicitly read-write. +BEGIN; +SET TRANSACTION READ WRITE; +SELECT documentdb_api.create_collection_view('db', '{ "create": "collection" }'); +NOTICE: creating collection + create_collection_view +---------------------------------------- + { "ok" : { "$numberDouble" : "1.0" } } +(1 row) + +SELECT documentdb_api.insert('db', '{ "insert": "collection", "documents": [ { "a": 1 }] }'); + insert +---------------------------------------------------------------------------------------- + ("{ ""n"" : { ""$numberInt"" : ""1"" }, ""ok"" : { ""$numberDouble"" : ""1.0"" } }",t) +(1 row) + +SELECT documentdb_api.update('db', '{ "update": "collection", "updates": [ { "q": { "a": 1 }, "u": { "$set": { "b": 1 }} }] }'); + update +-------------------------------------------------------------------------------------------------------------------------------------- + ("{ ""ok"" : { ""$numberDouble"" : ""1.0"" }, ""nModified"" : { ""$numberLong"" : ""1"" }, ""n"" : { ""$numberLong"" : ""1"" } }",t) +(1 row) + +SELECT documentdb_api.delete('db', '{ "delete": "collection", "deletes": [ { "q": { "a": 1 }, "limit": 1 }] }'); + delete +---------------------------------------------------------------------------------------- + ("{ ""n"" : { ""$numberInt"" : ""1"" }, ""ok"" : { ""$numberDouble"" : ""1.0"" } }",t) +(1 row) + +SELECT documentdb_api.drop_collection('db', 'collection'); + drop_collection +----------------- + t +(1 row) + +ROLLBACK; +-- fails gracefully when transaction is read-only. +CALL documentdb_api_internal.delete_expired_rows(); +INFO: TTL job skipping because transaction is read-only. +set documentdb.enableTTLJobsOnReadOnly to on; +CALL documentdb_api_internal.delete_expired_rows(); diff --git a/pg_documentdb/src/test/regress/sql/readonly_transaction_tests.sql b/pg_documentdb/src/test/regress/sql/readonly_transaction_tests.sql new file mode 100644 index 00000000..e758aef5 --- /dev/null +++ b/pg_documentdb/src/test/regress/sql/readonly_transaction_tests.sql @@ -0,0 +1,36 @@ +SET search_path TO documentdb_api,documentdb_core,documentdb_api_catalog; +SET documentdb.next_collection_id TO 2100; +SET documentdb.next_collection_index_id TO 2100; + +SELECT documentdb_api.create_collection_view('db', '{ "create": "test" }'); +SELECT documentdb_api.insert_one('db', 'test', '{ "_id": 1, "a": 1, "ttl": 0 }'); + +SELECT documentdb_api_internal.create_indexes_non_concurrently('db', '{ "createIndexes": "test", "indexes": [ { "key": { "ttl": 1 }, "name": "ttlIndex", "expireAfterSeconds": 300 } ] }', TRUE); + +-- set the default transaction mode. +set default_transaction_read_only = on; + +-- should fail gracefully. +SELECT documentdb_api.create_collection_view('db', '{ "create": "collection" }'); +SELECT documentdb_api.insert('db', '{ "insert": "test", "documents": [ { "a": 1 }] }'); +SELECT documentdb_api.update('db', '{ "update": "test", "updates": [ { "q": { "a": 1 }, "u": { "$set": { "b": 1 }} }] }'); +SELECT documentdb_api.delete('db', '{ "delete": "test", "deletes": [ { "q": { "a": 1 } }] }'); + +SELECT document FROM documentdb_api_catalog.bson_aggregation_pipeline('db', '{ "aggregate": "test", "pipeline": [ { "$merge": { "into": "test" } } ] }'); +SELECT documentdb_api.drop_collection('db', 'collection'); + +-- same command works when transaction explicitly read-write. +BEGIN; +SET TRANSACTION READ WRITE; +SELECT documentdb_api.create_collection_view('db', '{ "create": "collection" }'); +SELECT documentdb_api.insert('db', '{ "insert": "collection", "documents": [ { "a": 1 }] }'); +SELECT documentdb_api.update('db', '{ "update": "collection", "updates": [ { "q": { "a": 1 }, "u": { "$set": { "b": 1 }} }] }'); +SELECT documentdb_api.delete('db', '{ "delete": "collection", "deletes": [ { "q": { "a": 1 }, "limit": 1 }] }'); +SELECT documentdb_api.drop_collection('db', 'collection'); +ROLLBACK; + +-- fails gracefully when transaction is read-only. +CALL documentdb_api_internal.delete_expired_rows(); + +set documentdb.enableTTLJobsOnReadOnly to on; +CALL documentdb_api_internal.delete_expired_rows(); \ No newline at end of file diff --git a/pg_documentdb/src/ttl/ttl_index.c b/pg_documentdb/src/ttl/ttl_index.c index 2a215570..0e5b29f4 100644 --- a/pg_documentdb/src/ttl/ttl_index.c +++ b/pg_documentdb/src/ttl/ttl_index.c @@ -32,6 +32,7 @@ extern int MaxTTLDeleteBatchSize; extern int TTLPurgerLockTimeout; extern char *ApiGucPrefix; extern int SingleTTLTaskTimeBudget; +extern bool EnableTtlJobsOnReadOnly; bool UseV2TTLIndexPurger = true; @@ -370,6 +371,35 @@ delete_expired_rows(PG_FUNCTION_ARGS) continue; } + /* + * Before we begin the actual deletions, check if we need + * to handle if the transaction is read-only. + */ + if (XactReadOnly) + { + if (EnableTtlJobsOnReadOnly) + { + /* To enable read-write we need to be the first query + * in the transaction. Process_utility will have already + * set a snapshot on this transaction so we can't reset + * the transaction read-only flag. + * Consequently, commit this transaction that's read-only + * and start a new one so we can mark the read-only flag + * as false. + */ + PopAllActiveSnapshots(); + CommitTransactionCommand(); + StartTransactionCommand(); + SetGUCLocally("transaction_read_only", "false"); + } + else + { + ereport(INFO, errmsg( + "TTL job skipping because transaction is read-only.")); + continue; + } + } + volatile bool shouldStop = false; /* Delete records on all tables for this collection */ @@ -426,6 +456,12 @@ delete_expired_rows(PG_FUNCTION_ARGS) { goto end; } + + /* Before starting the next loop, set the transaction characteristics */ + if (XactReadOnly && EnableTtlJobsOnReadOnly) + { + SetGUCLocally("transaction_read_only", "false"); + } } if (IsTaskTimeBudgetExceeded(startTime, NULL)) diff --git a/pg_documentdb_core/include/utils/documentdb_codes.txt b/pg_documentdb_core/include/utils/documentdb_codes.txt index 35f1802c..a569fbfd 100644 --- a/pg_documentdb_core/include/utils/documentdb_codes.txt +++ b/pg_documentdb_core/include/utils/documentdb_codes.txt @@ -511,3 +511,4 @@ Location7555702,M00E4,509 Location3041704,M00E5,510 Location4341107,M00E6,511 InsufficientPrivilege,M00E7,512 +NotWritablePrimary,M00E8,513 diff --git a/pg_documentdb_core/include/utils/documentdb_errors.h b/pg_documentdb_core/include/utils/documentdb_errors.h index 08f822e5..c566c196 100644 --- a/pg_documentdb_core/include/utils/documentdb_errors.h +++ b/pg_documentdb_core/include/utils/documentdb_errors.h @@ -1118,4 +1118,6 @@ typedef int DocumentdbErrorEreportCode; #define ERRCODE_DOCUMENTDB_INSUFFICIENTPRIVILEGE MAKE_SQLSTATE('M', '0', '0', 'E', '7') +#define ERRCODE_DOCUMENTDB_NOTWRITABLEPRIMARY MAKE_SQLSTATE('M', '0', '0', 'E', '8') + #endif diff --git a/pg_documentdb_core/include/utils/external_error_mapping.csv b/pg_documentdb_core/include/utils/external_error_mapping.csv index e356fab3..622c5ff6 100644 --- a/pg_documentdb_core/include/utils/external_error_mapping.csv +++ b/pg_documentdb_core/include/utils/external_error_mapping.csv @@ -511,3 +511,4 @@ Location7555702,M00E4,7555702,509 Location3041704,M00E5,3041704,510 Location4341107,M00E6,4341107,511 InsufficientPrivilege,M00E7,42501,512 +NotWritablePrimary,M00E8,10107,513 diff --git a/pg_documentdb_gw/src/configuration/dynamic.rs b/pg_documentdb_gw/src/configuration/dynamic.rs index c7ac1c46..7f1db9d6 100644 --- a/pg_documentdb_gw/src/configuration/dynamic.rs +++ b/pg_documentdb_gw/src/configuration/dynamic.rs @@ -48,7 +48,6 @@ pub trait DynamicConfiguration: Send + Sync + Debug { async fn is_read_only_for_disk_full(&self) -> bool { self.get_bool("default_transaction_read_only", false).await - && self.get_bool("IsPgReadOnlyForDiskFull", false).await } async fn read_only(&self) -> bool { diff --git a/pg_documentdb_gw/src/error.rs b/pg_documentdb_gw/src/error.rs index a0b12823..42fe2982 100644 --- a/pg_documentdb_gw/src/error.rs +++ b/pg_documentdb_gw/src/error.rs @@ -175,6 +175,7 @@ pub enum ErrorCode { NoSuchTransaction = 251, TransactionCommitted = 256, OperationNotSupportedInTransaction = 263, + NotWritablePrimary = 10107, DuplicateKey = 11000, OutOfDiskSpace = 14031, UnknownBsonField = 40415, @@ -206,6 +207,7 @@ impl FromPrimitive for ErrorCode { 251 => Some(ErrorCode::NoSuchTransaction), 256 => Some(ErrorCode::TransactionCommitted), 263 => Some(ErrorCode::OperationNotSupportedInTransaction), + 10107 => Some(ErrorCode::NotWritablePrimary), 11000 => Some(ErrorCode::DuplicateKey), 14031 => Some(ErrorCode::OutOfDiskSpace), 40415 => Some(ErrorCode::UnknownBsonField), diff --git a/pg_documentdb_gw/src/responses/pg.rs b/pg_documentdb_gw/src/responses/pg.rs index 73e6d428..12d069c5 100644 --- a/pg_documentdb_gw/src/responses/pg.rs +++ b/pg_documentdb_gw/src/responses/pg.rs @@ -183,6 +183,11 @@ impl PgResponse { msg: &str, ) -> Option<(i32, Option, Option)> { if let Some(known) = PgResponse::known_error_code(state) { + if known == ErrorCode::NotWritablePrimary as i32 { + let updated_error = format!("{}: This may be due to the database disk being full", msg); + return Some((known, Some(updated_error), None)); + } + return Some((known, None, None)); }