2007-04-02 05:49:42 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* enum.c
|
2007-11-15 22:14:46 +01:00
|
|
|
* I/O functions, operators, aggregates etc for enum types
|
2007-04-02 05:49:42 +02:00
|
|
|
*
|
2019-01-02 18:44:25 +01:00
|
|
|
* Copyright (c) 2006-2019, PostgreSQL Global Development Group
|
2007-04-02 05:49:42 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/backend/utils/adt/enum.c
|
2007-04-02 05:49:42 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
2019-12-27 00:09:00 +01:00
|
|
|
#include "access/genam.h"
|
2012-08-30 22:15:44 +02:00
|
|
|
#include "access/htup_details.h"
|
2019-01-21 19:18:20 +01:00
|
|
|
#include "access/table.h"
|
2010-10-25 05:04:37 +02:00
|
|
|
#include "catalog/indexing.h"
|
2007-04-02 05:49:42 +02:00
|
|
|
#include "catalog/pg_enum.h"
|
2010-10-25 05:04:37 +02:00
|
|
|
#include "libpq/pqformat.h"
|
2018-10-09 01:51:01 +02:00
|
|
|
#include "storage/procarray.h"
|
2011-09-04 07:13:16 +02:00
|
|
|
#include "utils/array.h"
|
2007-04-02 05:49:42 +02:00
|
|
|
#include "utils/builtins.h"
|
2010-10-25 05:04:37 +02:00
|
|
|
#include "utils/fmgroids.h"
|
|
|
|
#include "utils/snapmgr.h"
|
2007-04-02 05:49:42 +02:00
|
|
|
#include "utils/syscache.h"
|
2010-10-25 05:04:37 +02:00
|
|
|
#include "utils/typcache.h"
|
2007-04-02 05:49:42 +02:00
|
|
|
|
|
|
|
|
2010-10-25 05:04:37 +02:00
|
|
|
static Oid enum_endpoint(Oid enumtypoid, ScanDirection direction);
|
2007-04-02 05:49:42 +02:00
|
|
|
static ArrayType *enum_range_internal(Oid enumtypoid, Oid lower, Oid upper);
|
|
|
|
|
|
|
|
|
2018-10-09 01:51:01 +02:00
|
|
|
/*
|
|
|
|
* Disallow use of an uncommitted pg_enum tuple.
|
|
|
|
*
|
|
|
|
* We need to make sure that uncommitted enum values don't get into indexes.
|
|
|
|
* If they did, and if we then rolled back the pg_enum addition, we'd have
|
|
|
|
* broken the index because value comparisons will not work reliably without
|
|
|
|
* an underlying pg_enum entry. (Note that removal of the heap entry
|
|
|
|
* containing an enum value is not sufficient to ensure that it doesn't appear
|
|
|
|
* in upper levels of indexes.) To do this we prevent an uncommitted row from
|
|
|
|
* being used for any SQL-level purpose. This is stronger than necessary,
|
|
|
|
* since the value might not be getting inserted into a table or there might
|
|
|
|
* be no index on its column, but it's easy to enforce centrally.
|
|
|
|
*
|
|
|
|
* However, it's okay to allow use of uncommitted values belonging to enum
|
|
|
|
* types that were themselves created in the same transaction, because then
|
|
|
|
* any such index would also be new and would go away altogether on rollback.
|
|
|
|
* We don't implement that fully right now, but we do allow free use of enum
|
|
|
|
* values created during CREATE TYPE AS ENUM, which are surely of the same
|
|
|
|
* lifespan as the enum type. (This case is required by "pg_restore -1".)
|
|
|
|
* Values added by ALTER TYPE ADD VALUE are currently restricted, but could
|
|
|
|
* be allowed if the enum type could be proven to have been created earlier
|
|
|
|
* in the same transaction. (Note that comparing tuple xmins would not work
|
|
|
|
* for that, because the type tuple might have been updated in the current
|
|
|
|
* transaction. Subtransactions also create hazards to be accounted for.)
|
|
|
|
*
|
|
|
|
* This function needs to be called (directly or indirectly) in any of the
|
|
|
|
* functions below that could return an enum value to SQL operations.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
check_safe_enum_use(HeapTuple enumval_tup)
|
|
|
|
{
|
|
|
|
TransactionId xmin;
|
Remove WITH OIDS support, change oid catalog column visibility.
Previously tables declared WITH OIDS, including a significant fraction
of the catalog tables, stored the oid column not as a normal column,
but as part of the tuple header.
This special column was not shown by default, which was somewhat odd,
as it's often (consider e.g. pg_class.oid) one of the more important
parts of a row. Neither pg_dump nor COPY included the contents of the
oid column by default.
The fact that the oid column was not an ordinary column necessitated a
significant amount of special case code to support oid columns. That
already was painful for the existing, but upcoming work aiming to make
table storage pluggable, would have required expanding and duplicating
that "specialness" significantly.
WITH OIDS has been deprecated since 2005 (commit ff02d0a05280e0).
Remove it.
Removing includes:
- CREATE TABLE and ALTER TABLE syntax for declaring the table to be
WITH OIDS has been removed (WITH (oids[ = true]) will error out)
- pg_dump does not support dumping tables declared WITH OIDS and will
issue a warning when dumping one (and ignore the oid column).
- restoring an pg_dump archive with pg_restore will warn when
restoring a table with oid contents (and ignore the oid column)
- COPY will refuse to load binary dump that includes oids.
- pg_upgrade will error out when encountering tables declared WITH
OIDS, they have to be altered to remove the oid column first.
- Functionality to access the oid of the last inserted row (like
plpgsql's RESULT_OID, spi's SPI_lastoid, ...) has been removed.
The syntax for declaring a table WITHOUT OIDS (or WITH (oids = false)
for CREATE TABLE) is still supported. While that requires a bit of
support code, it seems unnecessary to break applications / dumps that
do not use oids, and are explicit about not using them.
The biggest user of WITH OID columns was postgres' catalog. This
commit changes all 'magic' oid columns to be columns that are normally
declared and stored. To reduce unnecessary query breakage all the
newly added columns are still named 'oid', even if a table's column
naming scheme would indicate 'reloid' or such. This obviously
requires adapting a lot code, mostly replacing oid access via
HeapTupleGetOid() with access to the underlying Form_pg_*->oid column.
The bootstrap process now assigns oids for all oid columns in
genbki.pl that do not have an explicit value (starting at the largest
oid previously used), only oids assigned later by oids will be above
FirstBootstrapObjectId. As the oid column now is a normal column the
special bootstrap syntax for oids has been removed.
Oids are not automatically assigned during insertion anymore, all
backend code explicitly assigns oids with GetNewOidWithIndex(). For
the rare case that insertions into the catalog via SQL are called for
the new pg_nextoid() function can be used (which only works on catalog
tables).
The fact that oid columns on system tables are now normal columns
means that they will be included in the set of columns expanded
by * (i.e. SELECT * FROM pg_class will now include the table's oid,
previously it did not). It'd not technically be hard to hide oid
column by default, but that'd mean confusing behavior would either
have to be carried forward forever, or it'd cause breakage down the
line.
While it's not unlikely that further adjustments are needed, the
scope/invasiveness of the patch makes it worthwhile to get merge this
now. It's painful to maintain externally, too complicated to commit
after the code code freeze, and a dependency of a number of other
patches.
Catversion bump, for obvious reasons.
Author: Andres Freund, with contributions by John Naylor
Discussion: https://postgr.es/m/20180930034810.ywp2c7awz7opzcfr@alap3.anarazel.de
2018-11-21 00:36:57 +01:00
|
|
|
Form_pg_enum en = (Form_pg_enum) GETSTRUCT(enumval_tup);
|
2018-10-09 01:51:01 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If the row is hinted as committed, it's surely safe. This provides a
|
|
|
|
* fast path for all normal use-cases.
|
|
|
|
*/
|
|
|
|
if (HeapTupleHeaderXminCommitted(enumval_tup->t_data))
|
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Usually, a row would get hinted as committed when it's read or loaded
|
|
|
|
* into syscache; but just in case not, let's check the xmin directly.
|
|
|
|
*/
|
|
|
|
xmin = HeapTupleHeaderGetXmin(enumval_tup->t_data);
|
|
|
|
if (!TransactionIdIsInProgress(xmin) &&
|
|
|
|
TransactionIdDidCommit(xmin))
|
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if the enum value is blacklisted. If not, it's safe, because it
|
|
|
|
* was made during CREATE TYPE AS ENUM and can't be shorter-lived than its
|
|
|
|
* owning type. (This'd also be false for values made by other
|
|
|
|
* transactions; but the previous tests should have handled all of those.)
|
|
|
|
*/
|
Remove WITH OIDS support, change oid catalog column visibility.
Previously tables declared WITH OIDS, including a significant fraction
of the catalog tables, stored the oid column not as a normal column,
but as part of the tuple header.
This special column was not shown by default, which was somewhat odd,
as it's often (consider e.g. pg_class.oid) one of the more important
parts of a row. Neither pg_dump nor COPY included the contents of the
oid column by default.
The fact that the oid column was not an ordinary column necessitated a
significant amount of special case code to support oid columns. That
already was painful for the existing, but upcoming work aiming to make
table storage pluggable, would have required expanding and duplicating
that "specialness" significantly.
WITH OIDS has been deprecated since 2005 (commit ff02d0a05280e0).
Remove it.
Removing includes:
- CREATE TABLE and ALTER TABLE syntax for declaring the table to be
WITH OIDS has been removed (WITH (oids[ = true]) will error out)
- pg_dump does not support dumping tables declared WITH OIDS and will
issue a warning when dumping one (and ignore the oid column).
- restoring an pg_dump archive with pg_restore will warn when
restoring a table with oid contents (and ignore the oid column)
- COPY will refuse to load binary dump that includes oids.
- pg_upgrade will error out when encountering tables declared WITH
OIDS, they have to be altered to remove the oid column first.
- Functionality to access the oid of the last inserted row (like
plpgsql's RESULT_OID, spi's SPI_lastoid, ...) has been removed.
The syntax for declaring a table WITHOUT OIDS (or WITH (oids = false)
for CREATE TABLE) is still supported. While that requires a bit of
support code, it seems unnecessary to break applications / dumps that
do not use oids, and are explicit about not using them.
The biggest user of WITH OID columns was postgres' catalog. This
commit changes all 'magic' oid columns to be columns that are normally
declared and stored. To reduce unnecessary query breakage all the
newly added columns are still named 'oid', even if a table's column
naming scheme would indicate 'reloid' or such. This obviously
requires adapting a lot code, mostly replacing oid access via
HeapTupleGetOid() with access to the underlying Form_pg_*->oid column.
The bootstrap process now assigns oids for all oid columns in
genbki.pl that do not have an explicit value (starting at the largest
oid previously used), only oids assigned later by oids will be above
FirstBootstrapObjectId. As the oid column now is a normal column the
special bootstrap syntax for oids has been removed.
Oids are not automatically assigned during insertion anymore, all
backend code explicitly assigns oids with GetNewOidWithIndex(). For
the rare case that insertions into the catalog via SQL are called for
the new pg_nextoid() function can be used (which only works on catalog
tables).
The fact that oid columns on system tables are now normal columns
means that they will be included in the set of columns expanded
by * (i.e. SELECT * FROM pg_class will now include the table's oid,
previously it did not). It'd not technically be hard to hide oid
column by default, but that'd mean confusing behavior would either
have to be carried forward forever, or it'd cause breakage down the
line.
While it's not unlikely that further adjustments are needed, the
scope/invasiveness of the patch makes it worthwhile to get merge this
now. It's painful to maintain externally, too complicated to commit
after the code code freeze, and a dependency of a number of other
patches.
Catversion bump, for obvious reasons.
Author: Andres Freund, with contributions by John Naylor
Discussion: https://postgr.es/m/20180930034810.ywp2c7awz7opzcfr@alap3.anarazel.de
2018-11-21 00:36:57 +01:00
|
|
|
if (!EnumBlacklisted(en->oid))
|
2018-10-09 01:51:01 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* There might well be other tests we could do here to narrow down the
|
|
|
|
* unsafe conditions, but for now just raise an exception.
|
|
|
|
*/
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNSAFE_NEW_ENUM_VALUE_USAGE),
|
|
|
|
errmsg("unsafe use of new value \"%s\" of enum type %s",
|
|
|
|
NameStr(en->enumlabel),
|
|
|
|
format_type_be(en->enumtypid)),
|
|
|
|
errhint("New enum values must be committed before they can be used.")));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-02 05:49:42 +02:00
|
|
|
/* Basic I/O support */
|
|
|
|
|
|
|
|
Datum
|
|
|
|
enum_in(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2007-11-15 22:14:46 +01:00
|
|
|
char *name = PG_GETARG_CSTRING(0);
|
|
|
|
Oid enumtypoid = PG_GETARG_OID(1);
|
|
|
|
Oid enumoid;
|
|
|
|
HeapTuple tup;
|
2007-04-02 05:49:42 +02:00
|
|
|
|
2007-04-03 00:14:17 +02:00
|
|
|
/* must check length to prevent Assert failure within SearchSysCache */
|
|
|
|
if (strlen(name) >= NAMEDATALEN)
|
2007-06-05 23:31:09 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
|
|
|
|
errmsg("invalid input value for enum %s: \"%s\"",
|
2007-04-03 00:14:17 +02:00
|
|
|
format_type_be(enumtypoid),
|
2007-06-05 23:31:09 +02:00
|
|
|
name)));
|
2007-04-03 00:14:17 +02:00
|
|
|
|
2010-02-14 19:42:19 +01:00
|
|
|
tup = SearchSysCache2(ENUMTYPOIDNAME,
|
|
|
|
ObjectIdGetDatum(enumtypoid),
|
|
|
|
CStringGetDatum(name));
|
2007-06-05 23:31:09 +02:00
|
|
|
if (!HeapTupleIsValid(tup))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
|
|
|
|
errmsg("invalid input value for enum %s: \"%s\"",
|
2007-04-02 05:49:42 +02:00
|
|
|
format_type_be(enumtypoid),
|
2007-06-05 23:31:09 +02:00
|
|
|
name)));
|
2007-04-02 05:49:42 +02:00
|
|
|
|
2018-10-09 01:51:01 +02:00
|
|
|
/* check it's safe to use in SQL */
|
|
|
|
check_safe_enum_use(tup);
|
|
|
|
|
2009-12-19 01:47:57 +01:00
|
|
|
/*
|
2010-02-26 03:01:40 +01:00
|
|
|
* This comes from pg_enum.oid and stores system oids in user tables. This
|
|
|
|
* oid must be preserved by binary upgrades.
|
2009-12-19 01:47:57 +01:00
|
|
|
*/
|
Remove WITH OIDS support, change oid catalog column visibility.
Previously tables declared WITH OIDS, including a significant fraction
of the catalog tables, stored the oid column not as a normal column,
but as part of the tuple header.
This special column was not shown by default, which was somewhat odd,
as it's often (consider e.g. pg_class.oid) one of the more important
parts of a row. Neither pg_dump nor COPY included the contents of the
oid column by default.
The fact that the oid column was not an ordinary column necessitated a
significant amount of special case code to support oid columns. That
already was painful for the existing, but upcoming work aiming to make
table storage pluggable, would have required expanding and duplicating
that "specialness" significantly.
WITH OIDS has been deprecated since 2005 (commit ff02d0a05280e0).
Remove it.
Removing includes:
- CREATE TABLE and ALTER TABLE syntax for declaring the table to be
WITH OIDS has been removed (WITH (oids[ = true]) will error out)
- pg_dump does not support dumping tables declared WITH OIDS and will
issue a warning when dumping one (and ignore the oid column).
- restoring an pg_dump archive with pg_restore will warn when
restoring a table with oid contents (and ignore the oid column)
- COPY will refuse to load binary dump that includes oids.
- pg_upgrade will error out when encountering tables declared WITH
OIDS, they have to be altered to remove the oid column first.
- Functionality to access the oid of the last inserted row (like
plpgsql's RESULT_OID, spi's SPI_lastoid, ...) has been removed.
The syntax for declaring a table WITHOUT OIDS (or WITH (oids = false)
for CREATE TABLE) is still supported. While that requires a bit of
support code, it seems unnecessary to break applications / dumps that
do not use oids, and are explicit about not using them.
The biggest user of WITH OID columns was postgres' catalog. This
commit changes all 'magic' oid columns to be columns that are normally
declared and stored. To reduce unnecessary query breakage all the
newly added columns are still named 'oid', even if a table's column
naming scheme would indicate 'reloid' or such. This obviously
requires adapting a lot code, mostly replacing oid access via
HeapTupleGetOid() with access to the underlying Form_pg_*->oid column.
The bootstrap process now assigns oids for all oid columns in
genbki.pl that do not have an explicit value (starting at the largest
oid previously used), only oids assigned later by oids will be above
FirstBootstrapObjectId. As the oid column now is a normal column the
special bootstrap syntax for oids has been removed.
Oids are not automatically assigned during insertion anymore, all
backend code explicitly assigns oids with GetNewOidWithIndex(). For
the rare case that insertions into the catalog via SQL are called for
the new pg_nextoid() function can be used (which only works on catalog
tables).
The fact that oid columns on system tables are now normal columns
means that they will be included in the set of columns expanded
by * (i.e. SELECT * FROM pg_class will now include the table's oid,
previously it did not). It'd not technically be hard to hide oid
column by default, but that'd mean confusing behavior would either
have to be carried forward forever, or it'd cause breakage down the
line.
While it's not unlikely that further adjustments are needed, the
scope/invasiveness of the patch makes it worthwhile to get merge this
now. It's painful to maintain externally, too complicated to commit
after the code code freeze, and a dependency of a number of other
patches.
Catversion bump, for obvious reasons.
Author: Andres Freund, with contributions by John Naylor
Discussion: https://postgr.es/m/20180930034810.ywp2c7awz7opzcfr@alap3.anarazel.de
2018-11-21 00:36:57 +01:00
|
|
|
enumoid = ((Form_pg_enum) GETSTRUCT(tup))->oid;
|
2007-04-02 05:49:42 +02:00
|
|
|
|
|
|
|
ReleaseSysCache(tup);
|
2007-06-05 23:31:09 +02:00
|
|
|
|
|
|
|
PG_RETURN_OID(enumoid);
|
2007-04-02 05:49:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
enum_out(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2007-11-15 22:14:46 +01:00
|
|
|
Oid enumval = PG_GETARG_OID(0);
|
|
|
|
char *result;
|
|
|
|
HeapTuple tup;
|
2007-04-02 05:49:42 +02:00
|
|
|
Form_pg_enum en;
|
|
|
|
|
2010-02-14 19:42:19 +01:00
|
|
|
tup = SearchSysCache1(ENUMOID, ObjectIdGetDatum(enumval));
|
2007-06-05 23:31:09 +02:00
|
|
|
if (!HeapTupleIsValid(tup))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
|
|
|
|
errmsg("invalid internal value for enum: %u",
|
|
|
|
enumval)));
|
2007-04-02 05:49:42 +02:00
|
|
|
en = (Form_pg_enum) GETSTRUCT(tup);
|
|
|
|
|
2007-06-05 23:31:09 +02:00
|
|
|
result = pstrdup(NameStr(en->enumlabel));
|
2007-04-02 05:49:42 +02:00
|
|
|
|
|
|
|
ReleaseSysCache(tup);
|
2007-06-05 23:31:09 +02:00
|
|
|
|
|
|
|
PG_RETURN_CSTRING(result);
|
2007-04-02 05:49:42 +02:00
|
|
|
}
|
|
|
|
|
2007-09-04 18:41:43 +02:00
|
|
|
/* Binary I/O support */
|
|
|
|
Datum
|
|
|
|
enum_recv(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2007-11-15 22:14:46 +01:00
|
|
|
StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
|
|
|
|
Oid enumtypoid = PG_GETARG_OID(1);
|
|
|
|
Oid enumoid;
|
|
|
|
HeapTuple tup;
|
|
|
|
char *name;
|
|
|
|
int nbytes;
|
2007-09-04 18:41:43 +02:00
|
|
|
|
|
|
|
name = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes);
|
|
|
|
|
|
|
|
/* must check length to prevent Assert failure within SearchSysCache */
|
|
|
|
if (strlen(name) >= NAMEDATALEN)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
|
|
|
|
errmsg("invalid input value for enum %s: \"%s\"",
|
|
|
|
format_type_be(enumtypoid),
|
|
|
|
name)));
|
|
|
|
|
2010-02-14 19:42:19 +01:00
|
|
|
tup = SearchSysCache2(ENUMTYPOIDNAME,
|
|
|
|
ObjectIdGetDatum(enumtypoid),
|
|
|
|
CStringGetDatum(name));
|
2007-09-04 18:41:43 +02:00
|
|
|
if (!HeapTupleIsValid(tup))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
|
|
|
|
errmsg("invalid input value for enum %s: \"%s\"",
|
|
|
|
format_type_be(enumtypoid),
|
|
|
|
name)));
|
|
|
|
|
2018-10-09 01:51:01 +02:00
|
|
|
/* check it's safe to use in SQL */
|
|
|
|
check_safe_enum_use(tup);
|
|
|
|
|
Remove WITH OIDS support, change oid catalog column visibility.
Previously tables declared WITH OIDS, including a significant fraction
of the catalog tables, stored the oid column not as a normal column,
but as part of the tuple header.
This special column was not shown by default, which was somewhat odd,
as it's often (consider e.g. pg_class.oid) one of the more important
parts of a row. Neither pg_dump nor COPY included the contents of the
oid column by default.
The fact that the oid column was not an ordinary column necessitated a
significant amount of special case code to support oid columns. That
already was painful for the existing, but upcoming work aiming to make
table storage pluggable, would have required expanding and duplicating
that "specialness" significantly.
WITH OIDS has been deprecated since 2005 (commit ff02d0a05280e0).
Remove it.
Removing includes:
- CREATE TABLE and ALTER TABLE syntax for declaring the table to be
WITH OIDS has been removed (WITH (oids[ = true]) will error out)
- pg_dump does not support dumping tables declared WITH OIDS and will
issue a warning when dumping one (and ignore the oid column).
- restoring an pg_dump archive with pg_restore will warn when
restoring a table with oid contents (and ignore the oid column)
- COPY will refuse to load binary dump that includes oids.
- pg_upgrade will error out when encountering tables declared WITH
OIDS, they have to be altered to remove the oid column first.
- Functionality to access the oid of the last inserted row (like
plpgsql's RESULT_OID, spi's SPI_lastoid, ...) has been removed.
The syntax for declaring a table WITHOUT OIDS (or WITH (oids = false)
for CREATE TABLE) is still supported. While that requires a bit of
support code, it seems unnecessary to break applications / dumps that
do not use oids, and are explicit about not using them.
The biggest user of WITH OID columns was postgres' catalog. This
commit changes all 'magic' oid columns to be columns that are normally
declared and stored. To reduce unnecessary query breakage all the
newly added columns are still named 'oid', even if a table's column
naming scheme would indicate 'reloid' or such. This obviously
requires adapting a lot code, mostly replacing oid access via
HeapTupleGetOid() with access to the underlying Form_pg_*->oid column.
The bootstrap process now assigns oids for all oid columns in
genbki.pl that do not have an explicit value (starting at the largest
oid previously used), only oids assigned later by oids will be above
FirstBootstrapObjectId. As the oid column now is a normal column the
special bootstrap syntax for oids has been removed.
Oids are not automatically assigned during insertion anymore, all
backend code explicitly assigns oids with GetNewOidWithIndex(). For
the rare case that insertions into the catalog via SQL are called for
the new pg_nextoid() function can be used (which only works on catalog
tables).
The fact that oid columns on system tables are now normal columns
means that they will be included in the set of columns expanded
by * (i.e. SELECT * FROM pg_class will now include the table's oid,
previously it did not). It'd not technically be hard to hide oid
column by default, but that'd mean confusing behavior would either
have to be carried forward forever, or it'd cause breakage down the
line.
While it's not unlikely that further adjustments are needed, the
scope/invasiveness of the patch makes it worthwhile to get merge this
now. It's painful to maintain externally, too complicated to commit
after the code code freeze, and a dependency of a number of other
patches.
Catversion bump, for obvious reasons.
Author: Andres Freund, with contributions by John Naylor
Discussion: https://postgr.es/m/20180930034810.ywp2c7awz7opzcfr@alap3.anarazel.de
2018-11-21 00:36:57 +01:00
|
|
|
enumoid = ((Form_pg_enum) GETSTRUCT(tup))->oid;
|
2007-09-04 18:41:43 +02:00
|
|
|
|
|
|
|
ReleaseSysCache(tup);
|
|
|
|
|
|
|
|
pfree(name);
|
|
|
|
|
|
|
|
PG_RETURN_OID(enumoid);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
enum_send(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2007-11-15 22:14:46 +01:00
|
|
|
Oid enumval = PG_GETARG_OID(0);
|
2007-09-04 18:41:43 +02:00
|
|
|
StringInfoData buf;
|
2007-11-15 22:14:46 +01:00
|
|
|
HeapTuple tup;
|
2007-09-04 18:41:43 +02:00
|
|
|
Form_pg_enum en;
|
|
|
|
|
2010-02-14 19:42:19 +01:00
|
|
|
tup = SearchSysCache1(ENUMOID, ObjectIdGetDatum(enumval));
|
2007-09-04 18:41:43 +02:00
|
|
|
if (!HeapTupleIsValid(tup))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
|
|
|
|
errmsg("invalid internal value for enum: %u",
|
|
|
|
enumval)));
|
|
|
|
en = (Form_pg_enum) GETSTRUCT(tup);
|
|
|
|
|
|
|
|
pq_begintypsend(&buf);
|
|
|
|
pq_sendtext(&buf, NameStr(en->enumlabel), strlen(NameStr(en->enumlabel)));
|
|
|
|
|
|
|
|
ReleaseSysCache(tup);
|
|
|
|
|
|
|
|
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
|
|
|
|
}
|
|
|
|
|
2007-04-02 05:49:42 +02:00
|
|
|
/* Comparison functions and related */
|
|
|
|
|
2010-10-25 05:04:37 +02:00
|
|
|
/*
|
|
|
|
* enum_cmp_internal is the common engine for all the visible comparison
|
|
|
|
* functions, except for enum_eq and enum_ne which can just check for OID
|
|
|
|
* equality directly.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
enum_cmp_internal(Oid arg1, Oid arg2, FunctionCallInfo fcinfo)
|
|
|
|
{
|
|
|
|
TypeCacheEntry *tcache;
|
|
|
|
|
2017-02-24 04:08:10 +01:00
|
|
|
/*
|
|
|
|
* We don't need the typcache except in the hopefully-uncommon case that
|
|
|
|
* one or both Oids are odd. This means that cursory testing of code that
|
|
|
|
* fails to pass flinfo to an enum comparison function might not disclose
|
|
|
|
* the oversight. To make such errors more obvious, Assert that we have a
|
|
|
|
* place to cache even when we take a fast-path exit.
|
|
|
|
*/
|
|
|
|
Assert(fcinfo->flinfo != NULL);
|
|
|
|
|
2010-10-25 05:04:37 +02:00
|
|
|
/* Equal OIDs are equal no matter what */
|
|
|
|
if (arg1 == arg2)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Fast path: even-numbered Oids are known to compare correctly */
|
|
|
|
if ((arg1 & 1) == 0 && (arg2 & 1) == 0)
|
|
|
|
{
|
|
|
|
if (arg1 < arg2)
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Locate the typcache entry for the enum type */
|
|
|
|
tcache = (TypeCacheEntry *) fcinfo->flinfo->fn_extra;
|
|
|
|
if (tcache == NULL)
|
|
|
|
{
|
|
|
|
HeapTuple enum_tup;
|
|
|
|
Form_pg_enum en;
|
|
|
|
Oid typeoid;
|
|
|
|
|
|
|
|
/* Get the OID of the enum type containing arg1 */
|
|
|
|
enum_tup = SearchSysCache1(ENUMOID, ObjectIdGetDatum(arg1));
|
|
|
|
if (!HeapTupleIsValid(enum_tup))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
|
|
|
|
errmsg("invalid internal value for enum: %u",
|
|
|
|
arg1)));
|
|
|
|
en = (Form_pg_enum) GETSTRUCT(enum_tup);
|
|
|
|
typeoid = en->enumtypid;
|
|
|
|
ReleaseSysCache(enum_tup);
|
|
|
|
/* Now locate and remember the typcache entry */
|
|
|
|
tcache = lookup_type_cache(typeoid, 0);
|
|
|
|
fcinfo->flinfo->fn_extra = (void *) tcache;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The remaining comparison logic is in typcache.c */
|
|
|
|
return compare_values_of_enum(tcache, arg1, arg2);
|
|
|
|
}
|
|
|
|
|
2007-04-02 05:49:42 +02:00
|
|
|
Datum
|
|
|
|
enum_lt(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2007-11-15 22:14:46 +01:00
|
|
|
Oid a = PG_GETARG_OID(0);
|
|
|
|
Oid b = PG_GETARG_OID(1);
|
2007-04-02 05:49:42 +02:00
|
|
|
|
2010-10-25 05:04:37 +02:00
|
|
|
PG_RETURN_BOOL(enum_cmp_internal(a, b, fcinfo) < 0);
|
2007-04-02 05:49:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
enum_le(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2007-11-15 22:14:46 +01:00
|
|
|
Oid a = PG_GETARG_OID(0);
|
|
|
|
Oid b = PG_GETARG_OID(1);
|
2007-04-02 05:49:42 +02:00
|
|
|
|
2010-10-25 05:04:37 +02:00
|
|
|
PG_RETURN_BOOL(enum_cmp_internal(a, b, fcinfo) <= 0);
|
2007-04-02 05:49:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
enum_eq(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2007-11-15 22:14:46 +01:00
|
|
|
Oid a = PG_GETARG_OID(0);
|
|
|
|
Oid b = PG_GETARG_OID(1);
|
2007-04-02 05:49:42 +02:00
|
|
|
|
|
|
|
PG_RETURN_BOOL(a == b);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
enum_ne(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2007-11-15 22:14:46 +01:00
|
|
|
Oid a = PG_GETARG_OID(0);
|
|
|
|
Oid b = PG_GETARG_OID(1);
|
2007-04-02 05:49:42 +02:00
|
|
|
|
|
|
|
PG_RETURN_BOOL(a != b);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
enum_ge(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2007-11-15 22:14:46 +01:00
|
|
|
Oid a = PG_GETARG_OID(0);
|
|
|
|
Oid b = PG_GETARG_OID(1);
|
2007-04-02 05:49:42 +02:00
|
|
|
|
2010-10-25 05:04:37 +02:00
|
|
|
PG_RETURN_BOOL(enum_cmp_internal(a, b, fcinfo) >= 0);
|
2007-04-02 05:49:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
enum_gt(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2007-11-15 22:14:46 +01:00
|
|
|
Oid a = PG_GETARG_OID(0);
|
|
|
|
Oid b = PG_GETARG_OID(1);
|
2007-04-02 05:49:42 +02:00
|
|
|
|
2010-10-25 05:04:37 +02:00
|
|
|
PG_RETURN_BOOL(enum_cmp_internal(a, b, fcinfo) > 0);
|
2007-04-02 05:49:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
enum_smaller(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2007-11-15 22:14:46 +01:00
|
|
|
Oid a = PG_GETARG_OID(0);
|
|
|
|
Oid b = PG_GETARG_OID(1);
|
2007-04-02 05:49:42 +02:00
|
|
|
|
2010-10-25 05:04:37 +02:00
|
|
|
PG_RETURN_OID(enum_cmp_internal(a, b, fcinfo) < 0 ? a : b);
|
2007-04-02 05:49:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
enum_larger(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2007-11-15 22:14:46 +01:00
|
|
|
Oid a = PG_GETARG_OID(0);
|
|
|
|
Oid b = PG_GETARG_OID(1);
|
2007-04-02 05:49:42 +02:00
|
|
|
|
2010-10-25 05:04:37 +02:00
|
|
|
PG_RETURN_OID(enum_cmp_internal(a, b, fcinfo) > 0 ? a : b);
|
2007-04-02 05:49:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
enum_cmp(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2007-11-15 22:14:46 +01:00
|
|
|
Oid a = PG_GETARG_OID(0);
|
|
|
|
Oid b = PG_GETARG_OID(1);
|
2007-04-02 05:49:42 +02:00
|
|
|
|
2017-02-24 04:08:10 +01:00
|
|
|
PG_RETURN_INT32(enum_cmp_internal(a, b, fcinfo));
|
2007-04-02 05:49:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Enum programming support functions */
|
|
|
|
|
2010-10-25 05:04:37 +02:00
|
|
|
/*
|
|
|
|
* enum_endpoint: common code for enum_first/enum_last
|
|
|
|
*/
|
|
|
|
static Oid
|
|
|
|
enum_endpoint(Oid enumtypoid, ScanDirection direction)
|
|
|
|
{
|
|
|
|
Relation enum_rel;
|
|
|
|
Relation enum_idx;
|
|
|
|
SysScanDesc enum_scan;
|
|
|
|
HeapTuple enum_tuple;
|
|
|
|
ScanKeyData skey;
|
|
|
|
Oid minmax;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the first/last enum member using pg_enum_typid_sortorder_index.
|
2015-04-29 21:48:44 +02:00
|
|
|
* Note we must not use the syscache. See comments for RenumberEnumType
|
|
|
|
* in catalog/pg_enum.c for more info.
|
2010-10-25 05:04:37 +02:00
|
|
|
*/
|
|
|
|
ScanKeyInit(&skey,
|
|
|
|
Anum_pg_enum_enumtypid,
|
|
|
|
BTEqualStrategyNumber, F_OIDEQ,
|
|
|
|
ObjectIdGetDatum(enumtypoid));
|
|
|
|
|
2019-01-21 19:32:19 +01:00
|
|
|
enum_rel = table_open(EnumRelationId, AccessShareLock);
|
2010-10-25 05:04:37 +02:00
|
|
|
enum_idx = index_open(EnumTypIdSortOrderIndexId, AccessShareLock);
|
2015-04-29 21:48:44 +02:00
|
|
|
enum_scan = systable_beginscan_ordered(enum_rel, enum_idx, NULL,
|
2010-10-25 05:04:37 +02:00
|
|
|
1, &skey);
|
|
|
|
|
|
|
|
enum_tuple = systable_getnext_ordered(enum_scan, direction);
|
|
|
|
if (HeapTupleIsValid(enum_tuple))
|
2018-10-09 01:51:01 +02:00
|
|
|
{
|
|
|
|
/* check it's safe to use in SQL */
|
|
|
|
check_safe_enum_use(enum_tuple);
|
Remove WITH OIDS support, change oid catalog column visibility.
Previously tables declared WITH OIDS, including a significant fraction
of the catalog tables, stored the oid column not as a normal column,
but as part of the tuple header.
This special column was not shown by default, which was somewhat odd,
as it's often (consider e.g. pg_class.oid) one of the more important
parts of a row. Neither pg_dump nor COPY included the contents of the
oid column by default.
The fact that the oid column was not an ordinary column necessitated a
significant amount of special case code to support oid columns. That
already was painful for the existing, but upcoming work aiming to make
table storage pluggable, would have required expanding and duplicating
that "specialness" significantly.
WITH OIDS has been deprecated since 2005 (commit ff02d0a05280e0).
Remove it.
Removing includes:
- CREATE TABLE and ALTER TABLE syntax for declaring the table to be
WITH OIDS has been removed (WITH (oids[ = true]) will error out)
- pg_dump does not support dumping tables declared WITH OIDS and will
issue a warning when dumping one (and ignore the oid column).
- restoring an pg_dump archive with pg_restore will warn when
restoring a table with oid contents (and ignore the oid column)
- COPY will refuse to load binary dump that includes oids.
- pg_upgrade will error out when encountering tables declared WITH
OIDS, they have to be altered to remove the oid column first.
- Functionality to access the oid of the last inserted row (like
plpgsql's RESULT_OID, spi's SPI_lastoid, ...) has been removed.
The syntax for declaring a table WITHOUT OIDS (or WITH (oids = false)
for CREATE TABLE) is still supported. While that requires a bit of
support code, it seems unnecessary to break applications / dumps that
do not use oids, and are explicit about not using them.
The biggest user of WITH OID columns was postgres' catalog. This
commit changes all 'magic' oid columns to be columns that are normally
declared and stored. To reduce unnecessary query breakage all the
newly added columns are still named 'oid', even if a table's column
naming scheme would indicate 'reloid' or such. This obviously
requires adapting a lot code, mostly replacing oid access via
HeapTupleGetOid() with access to the underlying Form_pg_*->oid column.
The bootstrap process now assigns oids for all oid columns in
genbki.pl that do not have an explicit value (starting at the largest
oid previously used), only oids assigned later by oids will be above
FirstBootstrapObjectId. As the oid column now is a normal column the
special bootstrap syntax for oids has been removed.
Oids are not automatically assigned during insertion anymore, all
backend code explicitly assigns oids with GetNewOidWithIndex(). For
the rare case that insertions into the catalog via SQL are called for
the new pg_nextoid() function can be used (which only works on catalog
tables).
The fact that oid columns on system tables are now normal columns
means that they will be included in the set of columns expanded
by * (i.e. SELECT * FROM pg_class will now include the table's oid,
previously it did not). It'd not technically be hard to hide oid
column by default, but that'd mean confusing behavior would either
have to be carried forward forever, or it'd cause breakage down the
line.
While it's not unlikely that further adjustments are needed, the
scope/invasiveness of the patch makes it worthwhile to get merge this
now. It's painful to maintain externally, too complicated to commit
after the code code freeze, and a dependency of a number of other
patches.
Catversion bump, for obvious reasons.
Author: Andres Freund, with contributions by John Naylor
Discussion: https://postgr.es/m/20180930034810.ywp2c7awz7opzcfr@alap3.anarazel.de
2018-11-21 00:36:57 +01:00
|
|
|
minmax = ((Form_pg_enum) GETSTRUCT(enum_tuple))->oid;
|
2018-10-09 01:51:01 +02:00
|
|
|
}
|
2010-10-25 05:04:37 +02:00
|
|
|
else
|
2018-10-09 01:51:01 +02:00
|
|
|
{
|
|
|
|
/* should only happen with an empty enum */
|
2010-10-25 05:04:37 +02:00
|
|
|
minmax = InvalidOid;
|
2018-10-09 01:51:01 +02:00
|
|
|
}
|
2010-10-25 05:04:37 +02:00
|
|
|
|
|
|
|
systable_endscan_ordered(enum_scan);
|
|
|
|
index_close(enum_idx, AccessShareLock);
|
2019-01-21 19:32:19 +01:00
|
|
|
table_close(enum_rel, AccessShareLock);
|
2010-10-25 05:04:37 +02:00
|
|
|
|
|
|
|
return minmax;
|
|
|
|
}
|
|
|
|
|
2007-04-02 05:49:42 +02:00
|
|
|
Datum
|
|
|
|
enum_first(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2007-11-15 22:14:46 +01:00
|
|
|
Oid enumtypoid;
|
2010-10-25 05:04:37 +02:00
|
|
|
Oid min;
|
2007-04-02 05:49:42 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We rely on being able to get the specific enum type from the calling
|
|
|
|
* expression tree. Notice that the actual value of the argument isn't
|
|
|
|
* examined at all; in particular it might be NULL.
|
|
|
|
*/
|
|
|
|
enumtypoid = get_fn_expr_argtype(fcinfo->flinfo, 0);
|
|
|
|
if (enumtypoid == InvalidOid)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
|
|
|
errmsg("could not determine actual enum type")));
|
|
|
|
|
2010-10-25 05:04:37 +02:00
|
|
|
/* Get the OID using the index */
|
|
|
|
min = enum_endpoint(enumtypoid, ForwardScanDirection);
|
2007-04-02 05:49:42 +02:00
|
|
|
|
2010-10-25 05:04:37 +02:00
|
|
|
if (!OidIsValid(min))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
|
|
|
|
errmsg("enum %s contains no values",
|
|
|
|
format_type_be(enumtypoid))));
|
2007-04-02 05:49:42 +02:00
|
|
|
|
|
|
|
PG_RETURN_OID(min);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
enum_last(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2007-11-15 22:14:46 +01:00
|
|
|
Oid enumtypoid;
|
2010-10-25 05:04:37 +02:00
|
|
|
Oid max;
|
2007-04-02 05:49:42 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We rely on being able to get the specific enum type from the calling
|
|
|
|
* expression tree. Notice that the actual value of the argument isn't
|
|
|
|
* examined at all; in particular it might be NULL.
|
|
|
|
*/
|
|
|
|
enumtypoid = get_fn_expr_argtype(fcinfo->flinfo, 0);
|
|
|
|
if (enumtypoid == InvalidOid)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
|
|
|
errmsg("could not determine actual enum type")));
|
|
|
|
|
2010-10-25 05:04:37 +02:00
|
|
|
/* Get the OID using the index */
|
|
|
|
max = enum_endpoint(enumtypoid, BackwardScanDirection);
|
2007-04-02 05:49:42 +02:00
|
|
|
|
2010-10-25 05:04:37 +02:00
|
|
|
if (!OidIsValid(max))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
|
|
|
|
errmsg("enum %s contains no values",
|
|
|
|
format_type_be(enumtypoid))));
|
2007-04-02 05:49:42 +02:00
|
|
|
|
2007-06-05 23:31:09 +02:00
|
|
|
PG_RETURN_OID(max);
|
2007-04-02 05:49:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 2-argument variant of enum_range */
|
|
|
|
Datum
|
|
|
|
enum_range_bounds(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2007-11-15 22:14:46 +01:00
|
|
|
Oid lower;
|
|
|
|
Oid upper;
|
|
|
|
Oid enumtypoid;
|
2007-04-02 05:49:42 +02:00
|
|
|
|
|
|
|
if (PG_ARGISNULL(0))
|
|
|
|
lower = InvalidOid;
|
|
|
|
else
|
|
|
|
lower = PG_GETARG_OID(0);
|
|
|
|
if (PG_ARGISNULL(1))
|
|
|
|
upper = InvalidOid;
|
|
|
|
else
|
|
|
|
upper = PG_GETARG_OID(1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We rely on being able to get the specific enum type from the calling
|
|
|
|
* expression tree. The generic type mechanism should have ensured that
|
|
|
|
* both are of the same type.
|
|
|
|
*/
|
|
|
|
enumtypoid = get_fn_expr_argtype(fcinfo->flinfo, 0);
|
|
|
|
if (enumtypoid == InvalidOid)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
|
|
|
errmsg("could not determine actual enum type")));
|
|
|
|
|
|
|
|
PG_RETURN_ARRAYTYPE_P(enum_range_internal(enumtypoid, lower, upper));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 1-argument variant of enum_range */
|
|
|
|
Datum
|
|
|
|
enum_range_all(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2007-11-15 22:14:46 +01:00
|
|
|
Oid enumtypoid;
|
2007-04-02 05:49:42 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We rely on being able to get the specific enum type from the calling
|
|
|
|
* expression tree. Notice that the actual value of the argument isn't
|
|
|
|
* examined at all; in particular it might be NULL.
|
|
|
|
*/
|
|
|
|
enumtypoid = get_fn_expr_argtype(fcinfo->flinfo, 0);
|
|
|
|
if (enumtypoid == InvalidOid)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
|
|
|
errmsg("could not determine actual enum type")));
|
|
|
|
|
|
|
|
PG_RETURN_ARRAYTYPE_P(enum_range_internal(enumtypoid,
|
|
|
|
InvalidOid, InvalidOid));
|
|
|
|
}
|
|
|
|
|
|
|
|
static ArrayType *
|
|
|
|
enum_range_internal(Oid enumtypoid, Oid lower, Oid upper)
|
|
|
|
{
|
2007-11-15 22:14:46 +01:00
|
|
|
ArrayType *result;
|
2010-10-25 05:04:37 +02:00
|
|
|
Relation enum_rel;
|
|
|
|
Relation enum_idx;
|
|
|
|
SysScanDesc enum_scan;
|
|
|
|
HeapTuple enum_tuple;
|
|
|
|
ScanKeyData skey;
|
2007-11-15 22:14:46 +01:00
|
|
|
Datum *elems;
|
2010-10-25 05:04:37 +02:00
|
|
|
int max,
|
|
|
|
cnt;
|
2011-04-10 17:42:00 +02:00
|
|
|
bool left_found;
|
2007-04-02 05:49:42 +02:00
|
|
|
|
2010-10-25 05:04:37 +02:00
|
|
|
/*
|
|
|
|
* Scan the enum members in order using pg_enum_typid_sortorder_index.
|
2015-04-29 21:48:44 +02:00
|
|
|
* Note we must not use the syscache. See comments for RenumberEnumType
|
|
|
|
* in catalog/pg_enum.c for more info.
|
2010-10-25 05:04:37 +02:00
|
|
|
*/
|
|
|
|
ScanKeyInit(&skey,
|
|
|
|
Anum_pg_enum_enumtypid,
|
|
|
|
BTEqualStrategyNumber, F_OIDEQ,
|
|
|
|
ObjectIdGetDatum(enumtypoid));
|
|
|
|
|
2019-01-21 19:32:19 +01:00
|
|
|
enum_rel = table_open(EnumRelationId, AccessShareLock);
|
2010-10-25 05:04:37 +02:00
|
|
|
enum_idx = index_open(EnumTypIdSortOrderIndexId, AccessShareLock);
|
2015-04-29 21:48:44 +02:00
|
|
|
enum_scan = systable_beginscan_ordered(enum_rel, enum_idx, NULL, 1, &skey);
|
2010-10-25 05:04:37 +02:00
|
|
|
|
|
|
|
max = 64;
|
|
|
|
elems = (Datum *) palloc(max * sizeof(Datum));
|
|
|
|
cnt = 0;
|
|
|
|
left_found = !OidIsValid(lower);
|
|
|
|
|
|
|
|
while (HeapTupleIsValid(enum_tuple = systable_getnext_ordered(enum_scan, ForwardScanDirection)))
|
|
|
|
{
|
Remove WITH OIDS support, change oid catalog column visibility.
Previously tables declared WITH OIDS, including a significant fraction
of the catalog tables, stored the oid column not as a normal column,
but as part of the tuple header.
This special column was not shown by default, which was somewhat odd,
as it's often (consider e.g. pg_class.oid) one of the more important
parts of a row. Neither pg_dump nor COPY included the contents of the
oid column by default.
The fact that the oid column was not an ordinary column necessitated a
significant amount of special case code to support oid columns. That
already was painful for the existing, but upcoming work aiming to make
table storage pluggable, would have required expanding and duplicating
that "specialness" significantly.
WITH OIDS has been deprecated since 2005 (commit ff02d0a05280e0).
Remove it.
Removing includes:
- CREATE TABLE and ALTER TABLE syntax for declaring the table to be
WITH OIDS has been removed (WITH (oids[ = true]) will error out)
- pg_dump does not support dumping tables declared WITH OIDS and will
issue a warning when dumping one (and ignore the oid column).
- restoring an pg_dump archive with pg_restore will warn when
restoring a table with oid contents (and ignore the oid column)
- COPY will refuse to load binary dump that includes oids.
- pg_upgrade will error out when encountering tables declared WITH
OIDS, they have to be altered to remove the oid column first.
- Functionality to access the oid of the last inserted row (like
plpgsql's RESULT_OID, spi's SPI_lastoid, ...) has been removed.
The syntax for declaring a table WITHOUT OIDS (or WITH (oids = false)
for CREATE TABLE) is still supported. While that requires a bit of
support code, it seems unnecessary to break applications / dumps that
do not use oids, and are explicit about not using them.
The biggest user of WITH OID columns was postgres' catalog. This
commit changes all 'magic' oid columns to be columns that are normally
declared and stored. To reduce unnecessary query breakage all the
newly added columns are still named 'oid', even if a table's column
naming scheme would indicate 'reloid' or such. This obviously
requires adapting a lot code, mostly replacing oid access via
HeapTupleGetOid() with access to the underlying Form_pg_*->oid column.
The bootstrap process now assigns oids for all oid columns in
genbki.pl that do not have an explicit value (starting at the largest
oid previously used), only oids assigned later by oids will be above
FirstBootstrapObjectId. As the oid column now is a normal column the
special bootstrap syntax for oids has been removed.
Oids are not automatically assigned during insertion anymore, all
backend code explicitly assigns oids with GetNewOidWithIndex(). For
the rare case that insertions into the catalog via SQL are called for
the new pg_nextoid() function can be used (which only works on catalog
tables).
The fact that oid columns on system tables are now normal columns
means that they will be included in the set of columns expanded
by * (i.e. SELECT * FROM pg_class will now include the table's oid,
previously it did not). It'd not technically be hard to hide oid
column by default, but that'd mean confusing behavior would either
have to be carried forward forever, or it'd cause breakage down the
line.
While it's not unlikely that further adjustments are needed, the
scope/invasiveness of the patch makes it worthwhile to get merge this
now. It's painful to maintain externally, too complicated to commit
after the code code freeze, and a dependency of a number of other
patches.
Catversion bump, for obvious reasons.
Author: Andres Freund, with contributions by John Naylor
Discussion: https://postgr.es/m/20180930034810.ywp2c7awz7opzcfr@alap3.anarazel.de
2018-11-21 00:36:57 +01:00
|
|
|
Oid enum_oid = ((Form_pg_enum) GETSTRUCT(enum_tuple))->oid;
|
2007-04-02 05:49:42 +02:00
|
|
|
|
2010-10-25 05:04:37 +02:00
|
|
|
if (!left_found && lower == enum_oid)
|
|
|
|
left_found = true;
|
2007-04-02 05:49:42 +02:00
|
|
|
|
2010-10-25 05:04:37 +02:00
|
|
|
if (left_found)
|
|
|
|
{
|
2018-10-09 01:51:01 +02:00
|
|
|
/* check it's safe to use in SQL */
|
|
|
|
check_safe_enum_use(enum_tuple);
|
|
|
|
|
2010-10-25 05:04:37 +02:00
|
|
|
if (cnt >= max)
|
|
|
|
{
|
|
|
|
max *= 2;
|
|
|
|
elems = (Datum *) repalloc(elems, max * sizeof(Datum));
|
|
|
|
}
|
2007-04-02 05:49:42 +02:00
|
|
|
|
2010-10-25 05:04:37 +02:00
|
|
|
elems[cnt++] = ObjectIdGetDatum(enum_oid);
|
|
|
|
}
|
2007-04-02 05:49:42 +02:00
|
|
|
|
2010-10-25 05:04:37 +02:00
|
|
|
if (OidIsValid(upper) && upper == enum_oid)
|
|
|
|
break;
|
|
|
|
}
|
2007-04-02 05:49:42 +02:00
|
|
|
|
2010-10-25 05:04:37 +02:00
|
|
|
systable_endscan_ordered(enum_scan);
|
|
|
|
index_close(enum_idx, AccessShareLock);
|
2019-01-21 19:32:19 +01:00
|
|
|
table_close(enum_rel, AccessShareLock);
|
2007-04-02 05:49:42 +02:00
|
|
|
|
2010-10-25 05:04:37 +02:00
|
|
|
/* and build the result array */
|
2007-04-02 05:49:42 +02:00
|
|
|
/* note this hardwires some details about the representation of Oid */
|
2010-10-25 05:04:37 +02:00
|
|
|
result = construct_array(elems, cnt, enumtypoid, sizeof(Oid), true, 'i');
|
2007-04-02 05:49:42 +02:00
|
|
|
|
|
|
|
pfree(elems);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|