Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit e2395cd

Browse files
committed
ALTER TABLE: rework determination of access method ID
Avoid setting an access method OID for relation kinds that don't take one. Code review for new feature added in 374c7a2. Author: Justin Pryzby <pryzby@telsasoft.com> Reported-by: Alexander Lakhin <exclusion@gmail.com> Discussion: https://postgr.es/m/e5516ac1-5264-c3c0-d822-9e6f614ea93b@gmail.com
1 parent be98a55 commit e2395cd

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

src/backend/commands/tablecmds.c

+15-11
Original file line numberDiff line numberDiff line change
@@ -958,22 +958,26 @@ DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId,
958958
}
959959

960960
/*
961-
* Select access method to use: an explicitly indicated one, or (in the
962-
* case of a partitioned table) the parent's, if it has one.
961+
* For relations with table AM and partitioned tables, select access
962+
* method to use: an explicitly indicated one, or (in the case of a
963+
* partitioned table) the parent's, if it has one.
963964
*/
964965
if (stmt->accessMethod != NULL)
965-
accessMethodId = get_table_am_oid(stmt->accessMethod, false);
966-
else if (stmt->partbound)
967966
{
968-
Assert(list_length(inheritOids) == 1);
969-
accessMethodId = get_rel_relam(linitial_oid(inheritOids));
967+
Assert(RELKIND_HAS_TABLE_AM(relkind) || relkind == RELKIND_PARTITIONED_TABLE);
968+
accessMethodId = get_table_am_oid(stmt->accessMethod, false);
970969
}
971-
else
972-
accessMethodId = InvalidOid;
970+
else if (RELKIND_HAS_TABLE_AM(relkind) || relkind == RELKIND_PARTITIONED_TABLE)
971+
{
972+
if (stmt->partbound)
973+
{
974+
Assert(list_length(inheritOids) == 1);
975+
accessMethodId = get_rel_relam(linitial_oid(inheritOids));
976+
}
973977

974-
/* still nothing? use the default */
975-
if (RELKIND_HAS_TABLE_AM(relkind) && !OidIsValid(accessMethodId))
976-
accessMethodId = get_table_am_oid(default_table_access_method, false);
978+
if (RELKIND_HAS_TABLE_AM(relkind) && !OidIsValid(accessMethodId))
979+
accessMethodId = get_table_am_oid(default_table_access_method, false);
980+
}
977981

978982
/*
979983
* Create the relation. Inherited defaults and constraints are passed in

src/test/regress/expected/create_am.out

+3
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,9 @@ CREATE TABLE i_am_a_failure() USING "I do not exist AM";
547547
ERROR: access method "I do not exist AM" does not exist
548548
CREATE TABLE i_am_a_failure() USING "btree";
549549
ERROR: access method "btree" is not of type TABLE
550+
-- Other weird invalid cases that cause problems
551+
CREATE FOREIGN TABLE fp PARTITION OF pg_am DEFAULT SERVER x;
552+
ERROR: "pg_am" is not partitioned
550553
-- Drop table access method, which fails as objects depends on it
551554
DROP ACCESS METHOD heap2;
552555
ERROR: cannot drop access method heap2 because other objects depend on it

src/test/regress/sql/create_am.sql

+3
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,9 @@ CREATE TABLE i_am_a_failure() USING i_do_not_exist_am;
348348
CREATE TABLE i_am_a_failure() USING "I do not exist AM";
349349
CREATE TABLE i_am_a_failure() USING "btree";
350350

351+
-- Other weird invalid cases that cause problems
352+
CREATE FOREIGN TABLE fp PARTITION OF pg_am DEFAULT SERVER x;
353+
351354
-- Drop table access method, which fails as objects depends on it
352355
DROP ACCESS METHOD heap2;
353356

0 commit comments

Comments
 (0)