ALTER_FOREIGN_TABLE(7)
โจ ALTER FOREIGN TABLE โ the glow-up page โจ
okay bestie, welcome to the ALTER FOREIGN TABLE manual page ๐ this is from the PostgreSQL 18.6 Documentation, section 7 (that's the SQL commands section, iykyk), and it's literally all about changing the definition of a foreign table. no cap.
๐ธ NAME ๐ธ
ALTER_FOREIGN_TABLE โ change the definition of a foreign table ๐ฆ
๐ SYNOPSIS ๐
okay so here's the whole vibe, in code, exactly as it is:
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
and action is one of these lil guys:
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 | DEFAULT }
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. periodt. it's giving โจmany subformsโจ, so let's go through each one, bestie:
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. and here's the tea: 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. iconic behavior honestly ๐ซถ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. (not me dropping columns and getting a lil notice instead of a whole error ๐ญ)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 onlyCHECKandNOT NULLconstraints 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 in CREATE FOREIGN TABLE.) if the constraint is marked NOT VALID (allowed only for the CHECK case), 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. (it's giving legacy, ngl ๐)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. multi-tasking queen behavior ๐
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 be able to SET ROLE to 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. so likeโฆ know your permissions, bestie ๐
๐ฆ 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.15).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. (we love a low-effort keyword ๐)
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);
look at that. slay. ๐
๐ 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. (zero columns?? not a single one?? the audacity, and yet PostgreSQL said yes ๐ซถ)
๐ SEE ALSO ๐
CREATE FOREIGN TABLE(CREATE_FOREIGN_TABLE(7))DROP FOREIGN TABLE(DROP_FOREIGN_TABLE(7))
and that's the whole page, bestie ๐ this was the PostgreSQL 18.6 documentation for ALTER FOREIGN TABLE(7), dated 2026 โ go forth and alter those foreign tables, you absolute main character โจ๐๐ฆ
๐ฆ where this page comes from
- alpine 3.23: postgresql18-doc 18.6-r0 ยท PostgreSQL
- alpine 3.24: postgresql18-doc 18.6-r0 ยท PostgreSQL
- arch rolling: postgresql 18.6-4 ยท PostgreSQL
- fedora 43: postgresql 18.6-1.fc43 ยท PostgreSQL
- fedora 43: postgresql 18.6-1.fc43 ยท PostgreSQL
- fedora 44: postgresql 18.6-1.fc44 ยท PostgreSQL
- fedora 44: postgresql 18.6-1.fc44 ยท PostgreSQL
- nixos 26.05: postgresql 18.6
- ubuntu resolute: postgresql-client-18 18.6-0ubuntu0.26.04.1 ยท PostgreSQL
- ubuntu stonking: postgresql-client-18 18.6-3build2 ยท PostgreSQL
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms