Skip to content

Commit

Permalink
BABEL: Support Table and Index partitioning in Babelfish (babelfish-f…
Browse files Browse the repository at this point in the history
…or-postgresql#399)

To support partitioned tables and indexes in Babelfish, this commit introduces the following changes:

Babelfish stores the creation date and original name of the table in the reloption, whereas PostgreSQL does not allow the usage of reloption for partitioned tables. This commit enables the usage of reloption for Babelfish partitioned tables to store the creation date and original name. This information will be utilized by various system views, such as information_schema_tsql.tables, sys.tables, etc., to display the table creation date and original name.

Babelfish supports column ordering and NULLs ordering in table constraints while PostgreSQL does not and to handle dump/restore of this we dump the underlying index of a UNIQUE/PRIMARY KEY constraint and make use of ALTER TABLE ADD CONSTRAINT USING syntax to support column ordering and NULLs ordering in table constraints. PostgreSQL disallows creation of an constraint using index like this for partitioned table so we will need to bypass that check during restore of Babelfish databases.

Signed-off-by: Sumit Jaiswal [email protected]
  • Loading branch information
sumitj824 authored and roshan0708 committed Oct 15, 2024
1 parent 7431d67 commit 3654908
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 4 deletions.
12 changes: 11 additions & 1 deletion src/backend/access/common/reloptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,8 @@ static void initialize_reloptions(void);
static void parse_one_reloption(relopt_value *option, char *text_str,
int text_len, bool validate);

pltsql_is_partitioned_table_reloptions_allowed_hook_type pltsql_is_partitioned_table_reloptions_allowed_hook = NULL;

/*
* Get the length of a string reloption (either default or the user-defined
* value). This is used for allocation purposes when building a set of
Expand Down Expand Up @@ -2001,10 +2003,18 @@ bytea *
partitioned_table_reloptions(Datum reloptions, bool validate)
{
if (validate && reloptions)
{
/*
* For babelfish partitioned table, allow usage of reloptions in TSQL dialect.
*/
if (pltsql_is_partitioned_table_reloptions_allowed_hook && pltsql_is_partitioned_table_reloptions_allowed_hook(reloptions))
return NULL;

ereport(ERROR,
errcode(ERRCODE_WRONG_OBJECT_TYPE),
errmsg("cannot specify storage parameters for a partitioned table"),
errhint("Specify storage parameters for its leaf partitions, instead."));
errhint("Specify storage parameters for its leaf partitions instead."));
}
return NULL;
}

Expand Down
4 changes: 3 additions & 1 deletion src/backend/commands/tablecmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -8857,6 +8857,7 @@ ATExecAddIndexConstraint(AlteredTableInfo *tab, Relation rel,
char constraintType;
ObjectAddress address;
bits16 flags;
const char *bbf_dump_restore = GetConfigOption("babelfishpg_tsql.dump_restore", true, false);

Assert(IsA(stmt, IndexStmt));
Assert(OidIsValid(index_oid));
Expand All @@ -8866,7 +8867,8 @@ ATExecAddIndexConstraint(AlteredTableInfo *tab, Relation rel,
* Doing this on partitioned tables is not a simple feature to implement,
* so let's punt for now.
*/
if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE &&
(!bbf_dump_restore || strcmp(bbf_dump_restore, "on") != 0)) /* For Babelfish databases, allow the restore of index for partitioned table. */
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("ALTER TABLE / ADD CONSTRAINT USING INDEX is not supported on partitioned tables")));
Expand Down
9 changes: 7 additions & 2 deletions src/backend/parser/parse_utilcmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -2282,8 +2282,13 @@ transformIndexConstraint(Constraint *constraint, CreateStmtContext *cxt)
errmsg("index \"%s\" does not belong to table \"%s\"",
index_name, RelationGetRelationName(heap_rel)),
parser_errposition(cxt->pstate, constraint->location)));

if (!index_form->indisvalid)
/*
* For Babelfish databases, allow the restore of index which is
* marked as not valid for partitioned table.
*/
if (!index_form->indisvalid &&
!(bbf_dump_restore && strcmp(bbf_dump_restore, "on") == 0 &&
RelationGetForm(heap_rel)->relkind == RELKIND_PARTITIONED_TABLE))
ereport(ERROR,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("index \"%s\" is not valid", index_name),
Expand Down
2 changes: 2 additions & 0 deletions src/include/access/reloptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,5 +243,7 @@ extern bytea *index_reloptions(amoptions_function amoptions, Datum reloptions,
extern bytea *attribute_reloptions(Datum reloptions, bool validate);
extern bytea *tablespace_reloptions(Datum reloptions, bool validate);
extern LOCKMODE AlterTableGetRelOptionsLockLevel(List *defList);
typedef bool (*pltsql_is_partitioned_table_reloptions_allowed_hook_type)();
extern PGDLLEXPORT pltsql_is_partitioned_table_reloptions_allowed_hook_type pltsql_is_partitioned_table_reloptions_allowed_hook;

#endif /* RELOPTIONS_H */
1 change: 1 addition & 0 deletions src/include/nodes/parsenodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,7 @@ typedef struct PartitionSpec
PartitionStrategy strategy;
List *partParams; /* List of PartitionElems */
int location; /* token location, or -1 if unknown */
char *tsql_partition_scheme; /* tsql partition scheme name */
} PartitionSpec;

/*
Expand Down

0 comments on commit 3654908

Please sign in to comment.