Skip to content

Commit

Permalink
addressing mihnea's comment
Browse files Browse the repository at this point in the history
  • Loading branch information
deeps1991 committed Jul 22, 2020
1 parent 825faad commit 71d4033
Show file tree
Hide file tree
Showing 17 changed files with 4,687 additions and 243 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
public class TestPgRegressPartitions extends BasePgSQLTest {
@Override
public int getTestMethodTimeoutSec() {
return 1800;
return 7200;
}

@Test
Expand Down
9 changes: 5 additions & 4 deletions src/postgres/src/backend/catalog/partition.c
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ has_partition_attrs(Relation rel, Bitmapset *attnums, bool *used_in_expr)
if (partattno != 0)
{
if (bms_is_member(partattno - YBGetFirstLowInvalidAttributeNumber(rel),
attnums))
attnums))
{
if (used_in_expr)
*used_in_expr = false;
Expand All @@ -240,10 +240,11 @@ has_partition_attrs(Relation rel, Bitmapset *attnums, bool *used_in_expr)
Bitmapset *expr_attrs = NULL;

/* Find all attributes referenced */
// pull_varattnos_min_attr offsets attributes by min_attr + 1. Therefore pass
// YBGetFirstLowInvalidAttributeNumber + 1.
/* pull_varattnos_min_attr offsets attributes by min_attr - 1. Therefore pass
* YBGetFirstLowInvalidAttributeNumber + 1.
*/
pull_varattnos_min_attr(expr, 1, &expr_attrs,
YBGetFirstLowInvalidAttributeNumber(rel) + 1);
YBGetFirstLowInvalidAttributeNumber(rel) + 1);
partexprs_item = lnext(partexprs_item);

if (bms_overlap(attnums, expr_attrs))
Expand Down
4 changes: 2 additions & 2 deletions src/postgres/src/backend/executor/nodeModifyTable.c
Original file line number Diff line number Diff line change
Expand Up @@ -1127,9 +1127,9 @@ ExecUpdate(ModifyTableState *mtstate,
if (resultRelationDesc->rd_att->constr)
ExecConstraints(resultRelInfo, slot, estate, mtstate);

/*
/*
* Verify that the update does not violate partition constraints.
*/
*/
ExecPartitionCheck(resultRelInfo, slot, estate, true /* emitError */);

RangeTblEntry *rte = rt_fetch(resultRelInfo->ri_RangeTableIndex,
Expand Down
12 changes: 6 additions & 6 deletions src/postgres/src/backend/optimizer/path/pathkeys.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,12 @@ make_pathkey_from_sortinfo(PlannerInfo *root,
List *opfamilies;
EquivalenceClass *eclass;

strategy = reverse_sort ? BTGreaterStrategyNumber : BTLessStrategyNumber;
if (is_hash_index) {
/* We are picking a hash index. The strategy can only be BTEqualStrategyNumber */
strategy = BTEqualStrategyNumber;
} else {
strategy = reverse_sort ? BTGreaterStrategyNumber : BTLessStrategyNumber;
}

/*
* EquivalenceClasses need to contain opfamily lists based on the family
Expand Down Expand Up @@ -221,11 +226,6 @@ make_pathkey_from_sortinfo(PlannerInfo *root,
return NULL;
}

/* We are picking a hash index. The strategy can only be BTEqualStrategyNumber */
if (is_hash_index) {
strategy = BTEqualStrategyNumber;
}

/* And finally we can find or create a PathKey node */
return make_canonical_pathkey(root, eclass, opfamily,
strategy, nulls_first);
Expand Down
21 changes: 11 additions & 10 deletions src/postgres/src/backend/optimizer/prep/prepunion.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ static void make_inh_translation_list(Relation oldrelation,
Index newvarno,
List **translated_vars);
static Bitmapset *translate_col_privs(Relation parentrel,
const Bitmapset *parent_privs,
List *translated_vars);
const Bitmapset *parent_privs,
List *translated_vars);
static Node *adjust_appendrel_attrs_mutator(Node *node,
adjust_appendrel_attrs_context *context);
static Relids adjust_child_relids(Relids relids, int nappinfos,
Expand Down Expand Up @@ -1843,14 +1843,14 @@ expand_single_inheritance_child(PlannerInfo *root, RangeTblEntry *parentrte,
if (childOID != parentOID)
{
childrte->selectedCols = translate_col_privs(parentrel,
parentrte->selectedCols,
appinfo->translated_vars);
parentrte->selectedCols,
appinfo->translated_vars);
childrte->insertedCols = translate_col_privs(parentrel,
parentrte->insertedCols,
appinfo->translated_vars);
parentrte->insertedCols,
appinfo->translated_vars);
childrte->updatedCols = translate_col_privs(parentrel,
parentrte->updatedCols,
appinfo->translated_vars);
parentrte->updatedCols,
appinfo->translated_vars);
}
}

Expand Down Expand Up @@ -2033,10 +2033,11 @@ translate_col_privs(Relation parentrel,
continue;
if (whole_row ||
bms_is_member(attno - firstLowInvalidAttrNumber,
parent_privs)) {
parent_privs))
{
child_privs = bms_add_member(child_privs,
var->varattno - firstLowInvalidAttrNumber);
}
}
}

return child_privs;
Expand Down
4 changes: 2 additions & 2 deletions src/postgres/src/backend/optimizer/util/var.c
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ pull_varattnos_walker(Node *node, pull_varattnos_context *context)

/*
* The same as pull_varattnos() but attribute numbers are offset by
* (rel->min_attr + 1) instead of FirstLowInvalidHeapAttributeNumber.
* (rel->min_attr - 1) instead of FirstLowInvalidHeapAttributeNumber.
*/
void
pull_varattnos_min_attr(Node *node, Index varno, Bitmapset **varattnos, AttrNumber min_attr)
Expand All @@ -272,7 +272,7 @@ pull_varattnos_min_attr(Node *node, Index varno, Bitmapset **varattnos, AttrNumb

/*
* The same as pull_varattnos_walker() but attribute numbers are offset by
* (rel->min_attr + 1) instead of FirstLowInvalidHeapAttributeNumber.
* (rel->min_attr - 1) instead of FirstLowInvalidHeapAttributeNumber.
*/
static bool
pull_varattnos_walker_min_attr(Node *node, pull_varattnos_context *context, AttrNumber min_attr)
Expand Down
74 changes: 37 additions & 37 deletions src/postgres/src/backend/utils/cache/relcache.c
Original file line number Diff line number Diff line change
Expand Up @@ -1740,43 +1740,43 @@ void YBPreloadRelCache()
systable_endscan(scandesc);

/* Start scan for pg_partitioned_table */
Relation pg_partitioned_table_desc;
pg_partitioned_table_desc = heap_open(PartitionedRelationId, AccessShareLock);

scandesc = systable_beginscan(pg_partitioned_table_desc,
PartitionedRelationId,
false /* indexOk */,
NULL,
0,
NULL);

HeapTuple pg_partition_tuple;
while (HeapTupleIsValid(pg_partition_tuple = systable_getnext(scandesc)))
{
pg_partition_tuple = heap_copytuple(pg_partition_tuple);
Form_pg_partitioned_table part_table_form;

part_table_form = (Form_pg_partitioned_table) GETSTRUCT(pg_partition_tuple);

Relation relation;
RelationIdCacheLookup(part_table_form->partrelid, relation);

if (!relation) {
continue;
}

/* Initialize key and partition descriptor info */
if (relation->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
{
RelationBuildPartitionKey(relation);
RelationBuildPartitionDesc(relation);
}
}

systable_endscan(scandesc);

/* Close all the three relations that were opened */
heap_close(pg_partitioned_table_desc, AccessShareLock);
Relation pg_partitioned_table_desc;
pg_partitioned_table_desc = heap_open(PartitionedRelationId, AccessShareLock);

scandesc = systable_beginscan(pg_partitioned_table_desc,
PartitionedRelationId,
false /* indexOk */,
NULL,
0,
NULL);

HeapTuple pg_partition_tuple;
while (HeapTupleIsValid(pg_partition_tuple = systable_getnext(scandesc)))
{
pg_partition_tuple = heap_copytuple(pg_partition_tuple);
Form_pg_partitioned_table part_table_form;

part_table_form = (Form_pg_partitioned_table) GETSTRUCT(pg_partition_tuple);

Relation relation;
RelationIdCacheLookup(part_table_form->partrelid, relation);

if (!relation) {
continue;
}

/* Initialize key and partition descriptor info */
if (relation->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
{
RelationBuildPartitionKey(relation);
RelationBuildPartitionDesc(relation);
}
}

systable_endscan(scandesc);

/* Close all the three relations that were opened */
heap_close(pg_partitioned_table_desc, AccessShareLock);

heap_close(pg_attribute_desc, AccessShareLock);

Expand Down
6 changes: 3 additions & 3 deletions src/postgres/src/backend/utils/misc/pg_yb_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ CheckIsYBSupportedRelationByKind(char relkind)
relkind == RELKIND_VIEW || relkind == RELKIND_SEQUENCE ||
relkind == RELKIND_COMPOSITE_TYPE || relkind == RELKIND_PARTITIONED_TABLE ||
relkind == RELKIND_PARTITIONED_INDEX))

ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("This feature is not supported in YugaByte.")));
Expand All @@ -102,8 +102,8 @@ IsYBRelation(Relation relation)
/* Currently only support regular tables and indexes.
* Temp tables and views are supported, but they are not YB relations. */
return (relkind == RELKIND_RELATION || relkind == RELKIND_INDEX || relkind == RELKIND_PARTITIONED_TABLE ||
relkind == RELKIND_PARTITIONED_INDEX) &&
relation->rd_rel->relpersistence != RELPERSISTENCE_TEMP;
relkind == RELKIND_PARTITIONED_INDEX) &&
relation->rd_rel->relpersistence != RELPERSISTENCE_TEMP;
}

bool
Expand Down
37 changes: 28 additions & 9 deletions src/postgres/src/test/regress/expected/yb_pg_foreign_key.out
Original file line number Diff line number Diff line change
Expand Up @@ -1797,12 +1797,6 @@ CREATE TABLE fk_partitioned_fk_5 (a int, b int,
FOREIGN KEY (a, b) REFERENCES fk_notpartitioned_pk(a, b) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE,
FOREIGN KEY (a, b) REFERENCES fk_notpartitioned_pk(a, b) MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE)
PARTITION BY RANGE (a);
ERROR: DEFERRABLE constraint not supported yet
LINE 2: ...oned_pk(a, b) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE...
^
HINT: See https://github.com/YugaByte/yugabyte-db/issues/1129. Click '+' on the description to raise its priority
-- Uncomment below tests after deferrable constraints are supported.
/*
CREATE TABLE fk_partitioned_fk_5_1 (a int, b int, FOREIGN KEY (a, b) REFERENCES fk_notpartitioned_pk);
ALTER TABLE fk_partitioned_fk ATTACH PARTITION fk_partitioned_fk_5 FOR VALUES IN (4500);
ALTER TABLE fk_partitioned_fk_5 ATTACH PARTITION fk_partitioned_fk_5_1 FOR VALUES FROM (0) TO (10);
Expand All @@ -1811,11 +1805,35 @@ ALTER TABLE fk_partitioned_fk ATTACH PARTITION fk_partitioned_fk_5 FOR VALUES IN
-- this one has two constraints, similar but not quite the one in the parent,
-- so it gets a new one
\d fk_partitioned_fk_5
Table "public.fk_partitioned_fk_5"
Column | Type | Collation | Nullable | Default
--------+---------+-----------+----------+---------
a | integer | | |
b | integer | | |
Partition of: fk_partitioned_fk FOR VALUES IN (4500)
Partition key: RANGE (a)
Foreign-key constraints:
"fk_partitioned_fk_5_a_fkey" FOREIGN KEY (a, b) REFERENCES fk_notpartitioned_pk(a, b) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE
"fk_partitioned_fk_5_a_fkey1" FOREIGN KEY (a, b) REFERENCES fk_notpartitioned_pk(a, b) MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE
"fk_partitioned_fk_a_fkey" FOREIGN KEY (a, b) REFERENCES fk_notpartitioned_pk(a, b) ON UPDATE CASCADE ON DELETE CASCADE
Number of partitions: 1 (Use \d+ to list them.)

-- verify that it works to reattaching a child with multiple candidate
-- constraints
ALTER TABLE fk_partitioned_fk_5 DETACH PARTITION fk_partitioned_fk_5_1;
ALTER TABLE fk_partitioned_fk_5 ATTACH PARTITION fk_partitioned_fk_5_1 FOR VALUES FROM (0) TO (10);
\d fk_partitioned_fk_5_1
Table "public.fk_partitioned_fk_5_1"
Column | Type | Collation | Nullable | Default
--------+---------+-----------+----------+---------
a | integer | | |
b | integer | | |
Partition of: fk_partitioned_fk_5 FOR VALUES FROM (0) TO (10)
Foreign-key constraints:
"fk_partitioned_fk_5_1_a_fkey" FOREIGN KEY (a, b) REFERENCES fk_notpartitioned_pk(a, b)
"fk_partitioned_fk_5_a_fkey" FOREIGN KEY (a, b) REFERENCES fk_notpartitioned_pk(a, b) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE
"fk_partitioned_fk_5_a_fkey1" FOREIGN KEY (a, b) REFERENCES fk_notpartitioned_pk(a, b) MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE
"fk_partitioned_fk_a_fkey" FOREIGN KEY (a, b) REFERENCES fk_notpartitioned_pk(a, b) ON UPDATE CASCADE ON DELETE CASCADE

-- verify that attaching a table checks that the existing data satisfies the
-- constraint
Expand All @@ -1825,10 +1843,11 @@ CREATE TABLE fk_partitioned_fk_2_2 PARTITION OF fk_partitioned_fk_2 FOR VALUES F
INSERT INTO fk_partitioned_fk_2 VALUES (1600, 601), (1600, 1601);
ALTER TABLE fk_partitioned_fk ATTACH PARTITION fk_partitioned_fk_2
FOR VALUES IN (1600);
ERROR: insert or update on table "fk_partitioned_fk_2" violates foreign key constraint "fk_partitioned_fk_a_fkey"
DETAIL: Key (a, b)=(1600, 601) is not present in table "fk_notpartitioned_pk".
INSERT INTO fk_notpartitioned_pk VALUES (1600, 601), (1600, 1601);
ALTER TABLE fk_partitioned_fk ATTACH PARTITION fk_partitioned_fk_2
FOR VALUES IN (1600);
*/
-- leave these tables around intentionally
-- Test creating a constraint at the parent that already exists in partitions.
-- There should be no duplicated constraints, and attempts to drop the
Expand All @@ -1844,8 +1863,8 @@ create schema fkpart0
partition by list (a)
create table fk_part_23_2 partition of fk_part_23 for values in (2);
ERROR: CREATE SCHEMA with elements not supported yet
LINE 34: create table pkey (a int primary key)
^
LINE 2: create table pkey (a int primary key)
^
HINT: Please report the issue on https://github.com/YugaByte/yugabyte-db/issues
/*
alter table fkpart0.fk_part add foreign key (a) references fkpart0.pkey;
Expand Down
Loading

0 comments on commit 71d4033

Please sign in to comment.