ALTER_FOREIGN_TABLE(7pg14)
π ALTER_FOREIGN_TABLE π
okay bestie, welcome to the PostgreSQL 14.24 manual page for ALTER FOREIGN TABLE β yes, the whole glow-up-your-foreign-table situation. we are getting into every flag, every subform, every warning. no skips. π«Ά
β¨ NAME β¨
ALTER_FOREIGN_TABLE β change the definition of a foreign table π
π οΈ SYNOPSIS π οΈ
okay so hereβs the exact shape, no cap:
ALTER FOREIGN TABLE [ IF EXISTS ] [ ONLY ] name [ * ]
action [, ... ]
ALTER FOREIGN TABLE [ IF EXISTS ] [ ONLY ] name [ * ]
RENAME [ COLUMN ] column_name TO new_column_name
ALTER FOREIGN TABLE [ IF EXISTS ] name
RENAME TO new_name
ALTER FOREIGN TABLE [ IF EXISTS ] name
SET SCHEMA new_schema
where action is one of:
ADD [ COLUMN ] [ IF NOT EXISTS ] column_name data_type [ COLLATE collation ] [ column_constraint [ ... ] ]
DROP [ COLUMN ] [ IF EXISTS ] column_name [ RESTRICT | CASCADE ]
ALTER [ COLUMN ] column_name [ SET DATA ] TYPE data_type [ COLLATE collation ]
ALTER [ COLUMN ] column_name SET DEFAULT expression
ALTER [ COLUMN ] column_name DROP DEFAULT
ALTER [ COLUMN ] column_name { SET | DROP } NOT NULL
ALTER [ COLUMN ] column_name SET STATISTICS integer
ALTER [ COLUMN ] column_name SET ( attribute_option = value [, ... ] )
ALTER [ COLUMN ] column_name RESET ( attribute_option [, ... ] )
ALTER [ COLUMN ] column_name SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN }
ALTER [ COLUMN ] column_name OPTIONS ( [ ADD | SET | DROP ] option ['value'] [, ... ])
ADD table_constraint [ NOT VALID ]
VALIDATE CONSTRAINT constraint_name
DROP CONSTRAINT [ IF EXISTS ] constraint_name [ RESTRICT | CASCADE ]
DISABLE TRIGGER [ trigger_name | ALL | USER ]
ENABLE TRIGGER [ trigger_name | ALL | USER ]
ENABLE REPLICA TRIGGER trigger_name
ENABLE ALWAYS TRIGGER trigger_name
SET WITHOUT OIDS
INHERIT parent_table
NO INHERIT parent_table
OWNER TO { new_owner | CURRENT_ROLE | CURRENT_USER | SESSION_USER }
OPTIONS ( [ ADD | SET | DROP ] option ['value'] [, ... ])
πΈ DESCRIPTION πΈ
ALTER FOREIGN TABLE changes the definition of an existing foreign table. there are several subforms, and yes, weβre listing them all. periodt. π
ADD [ COLUMN ] [ IF NOT EXISTS ]β this form adds a new column to the foreign table, using the same syntax asCREATE FOREIGN TABLE. IfIF NOT EXISTSis specified and a column already exists with this name, no error is thrown. unlike the case when adding a column to a regular table, nothing happens to the underlying storage: this action simply declares that some new column is now accessible through the foreign table. β¨DROP [ COLUMN ] [ IF EXISTS ]β this form drops a column from a foreign table. you will need to sayCASCADEif anything outside the table depends on the column; for example, views. IfIF EXISTSis specified and the column does not exist, no error is thrown. in this case a notice is issued instead. π«ΆSET DATA TYPEβ this form changes the type of a column of a foreign table. again, this has no effect on any underlying storage: this action simply changes the type that PostgreSQL believes the column to have. πSET/DROP DEFAULTβ these forms set or remove the default value for a column. default values only apply in subsequentINSERTorUPDATEcommands; they do not cause rows already in the table to change. πSET/DROP NOT NULLβ mark a column as allowing, or not allowing, null values. πΈSET STATISTICSβ this form sets the per-column statistics-gathering target for subsequentANALYZEoperations. see the similar form ofALTER TABLEfor more details. πSET ( attribute_option = value [, ... ] )RESET ( attribute_option [, ... ] )β this form sets or resets per-attribute options. see the similar form ofALTER TABLEfor more details. β¨SET STORAGEβ this form sets the storage mode for a column. see the similar form ofALTER TABLEfor more details. note that the storage mode has no effect unless the table's foreign-data wrapper chooses to pay attention to it. πADD table_constraint [ NOT VALID ]β this form adds a new constraint to a foreign table, using the same syntax asCREATE FOREIGN TABLE. currently onlyCHECKconstraints are supported. unlike the case when adding a constraint to a regular table, nothing is done to verify the constraint is correct; rather, this action simply declares that some new condition should be assumed to hold for all rows in the foreign table. (see the discussion inCREATE FOREIGN TABLE.) if the constraint is markedNOT VALID, then it isn't assumed to hold, but is only recorded for possible future use. πVALIDATE CONSTRAINTβ this form marks as valid a constraint that was previously marked asNOT VALID. no action is taken to verify the constraint, but future queries will assume that it holds. βDROP CONSTRAINT [ IF EXISTS ]β this form drops the specified constraint on a foreign table. ifIF EXISTSis specified and the constraint does not exist, no error is thrown. in this case a notice is issued instead. πDISABLE/ENABLE [ REPLICA | ALWAYS ] TRIGGERβ these forms configure the firing of trigger(s) belonging to the foreign table. see the similar form ofALTER TABLEfor more details. πSET WITHOUT OIDSβ backward compatibility syntax for removing theoidsystem column. asoidsystem columns cannot be added anymore, this never has an effect. βINHERIT parent_tableβ this form adds the target foreign table as a new child of the specified parent table. see the similar form ofALTER TABLEfor more details. π§NO INHERIT parent_tableβ this form removes the target foreign table from the list of children of the specified parent table. βοΈOWNERβ this form changes the owner of the foreign table to the specified user. πOPTIONS ( [ ADD | SET | DROP ] option ['value'] [, ... ] )β change options for the foreign table or one of its columns.ADD,SET, andDROPspecify the action to be performed.ADDis assumed if no operation is explicitly specified. duplicate option names are not allowed (although it's OK for a table option and a column option to have the same name). option names and values are also validated using the foreign data wrapper library. πRENAMEβ theRENAMEforms change the name of a foreign table or the name of an individual column in a foreign table. ποΈSET SCHEMAβ this form moves the foreign table into another schema. π
all the actions except RENAME and SET SCHEMA can be combined into a list of multiple alterations to apply in parallel. for example, it is possible to add several columns and/or alter the type of several columns in a single command. π₯
if the command is written as ALTER FOREIGN TABLE IF EXISTS ... and the foreign table does not exist, no error is thrown. a notice is issued in this case. π£
you must own the table to use ALTER FOREIGN TABLE. to change the schema of a foreign table, you must also have CREATE privilege on the new schema. to alter the owner, you must also be a direct or indirect member of the new owning role, and that role must have CREATE privilege on the table's schema. (these restrictions enforce that altering the owner doesn't do anything you couldn't do by dropping and recreating the table. however, a superuser can alter ownership of any table anyway.) to add a column or alter a column type, you must also have USAGE privilege on the data type. π
π¦ PARAMETERS π¦
nameβ the name (possibly schema-qualified) of an existing foreign table to alter. ifONLYis specified before the table name, only that table is altered. ifONLYis not specified, the table and all its descendant tables (if any) are altered. optionally,*can be specified after the table name to explicitly indicate that descendant tables are included. πcolumn_nameβ name of a new or existing column. π·οΈnew_column_nameβ new name for an existing column. β¨new_nameβ new name for the table. πdata_typeβ data type of the new column, or new data type for an existing column. π§¬table_constraintβ new table constraint for the foreign table. π§±constraint_nameβ name of an existing constraint to drop. ποΈCASCADEβ automatically drop objects that depend on the dropped column or constraint (for example, views referencing the column), and in turn all objects that depend on those objects (see Section 5.14). π₯RESTRICTβ refuse to drop the column or constraint if there are any dependent objects. this is the default behavior. πtrigger_nameβ name of a single trigger to disable or enable. πALLβ disable or enable all triggers belonging to the foreign table. (this requires superuser privilege if any of the triggers are internally generated triggers. the core system does not add such triggers to foreign tables, but add-on code could do so.) πUSERβ disable or enable all triggers belonging to the foreign table except for internally generated triggers. πββοΈparent_tableβ a parent table to associate or de-associate with this foreign table. π¨βπ©βπ§new_ownerβ the user name of the new owner of the table. π«Άnew_schemaβ the name of the schema to which the table will be moved. π
π NOTES π
the key word COLUMN is noise and can be omitted. π
consistency with the foreign server is not checked when a column is added or removed withADD COLUMNorDROP COLUMN, aNOT NULLorCHECKconstraint is added, or a column type is changed withSET DATA TYPE. it is the user's responsibility to ensure that the table definition matches the remote side. π
refer to CREATE FOREIGN TABLE for a further description of valid parameters. π
π EXAMPLES π
to mark a column as not-null:
ALTER FOREIGN TABLE distributors ALTER COLUMN street SET NOT NULL;
to change options of a foreign table:
ALTER FOREIGN TABLE myschema.distributors OPTIONS (ADD opt1 'value', SET opt2 'value2', DROP opt3);
π« COMPATIBILITY π«
the forms ADD, DROP, and SET DATA TYPE conform with the SQL standard. the other forms are PostgreSQL extensions of the SQL standard. also, the ability to specify more than one manipulation in a single ALTER FOREIGN TABLE command is an extension. β¨
ALTER FOREIGN TABLE DROP COLUMN can be used to drop the only column of a foreign table, leaving a zero-column table. this is an extension of SQL, which disallows zero-column foreign tables. π
π SEE ALSO π
CREATE FOREIGN TABLE (CREATE_FOREIGN_TABLE(7)), DROP FOREIGN TABLE (DROP_FOREIGN_TABLE(7)) π«Ά
mwah π β source/version: PostgreSQL 14.24, date: 2026. go forth and alter those foreign tables, bestie. πβ¨
π¦ where this page comes from
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms