1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
2004-10-07 20:38:51 +02:00
|
|
|
* execJunk.c
|
1997-09-07 07:04:48 +02:00
|
|
|
* Junk attribute support stuff....
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2020-01-01 18:21:45 +01:00
|
|
|
* Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/backend/executor/execJunk.c
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
1996-10-31 11:12:26 +01:00
|
|
|
#include "postgres.h"
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
#include "executor/executor.h"
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* XXX this stuff should be rewritten to take advantage
|
|
|
|
* of ExecProject() and the ProjectionInfo node.
|
|
|
|
* -cim 6/3/91
|
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* An attribute of a tuple living inside the executor, can be
|
|
|
|
* either a normal attribute or a "junk" attribute. "junk" attributes
|
|
|
|
* never make it out of the executor, i.e. they are never printed,
|
2004-10-11 04:02:41 +02:00
|
|
|
* returned or stored on disk. Their only purpose in life is to
|
1996-07-09 08:22:35 +02:00
|
|
|
* store some information useful only to the executor, mainly the values
|
2006-12-04 03:06:55 +01:00
|
|
|
* of system attributes like "ctid", or sort key columns that are not to
|
|
|
|
* be output.
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* The general idea is the following: A target list consists of a list of
|
2005-04-06 18:34:07 +02:00
|
|
|
* TargetEntry nodes containing expressions. Each TargetEntry has a field
|
|
|
|
* called 'resjunk'. If the value of this field is true then the
|
1996-07-09 08:22:35 +02:00
|
|
|
* corresponding attribute is a "junk" attribute.
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
2009-10-10 03:43:50 +02:00
|
|
|
* When we initialize a plan we call ExecInitJunkFilter to create a filter.
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
2006-12-04 03:06:55 +01:00
|
|
|
* We then execute the plan, treating the resjunk attributes like any others.
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* Finally, when at the top level we get back a tuple, we can call
|
2006-12-04 03:06:55 +01:00
|
|
|
* ExecFindJunkAttribute/ExecGetJunkAttribute to retrieve the values of the
|
2011-02-22 03:41:08 +01:00
|
|
|
* junk attributes we are interested in, and ExecFilterJunk to remove all the
|
|
|
|
* junk attributes from a tuple. This new "clean" tuple is then printed,
|
|
|
|
* inserted, or updated.
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2004-10-07 20:38:51 +02:00
|
|
|
/*
|
1996-07-09 08:22:35 +02:00
|
|
|
* ExecInitJunkFilter
|
|
|
|
*
|
|
|
|
* Initialize the Junk filter.
|
1999-10-31 01:13:30 +02:00
|
|
|
*
|
2014-05-06 18:12:18 +02:00
|
|
|
* The source targetlist is passed in. The output tuple descriptor is
|
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
|
|
|
* built from the non-junk tlist entries.
|
2001-05-27 22:48:51 +02:00
|
|
|
* An optional resultSlot can be passed as well.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1997-09-08 04:41:22 +02:00
|
|
|
JunkFilter *
|
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
|
|
|
ExecInitJunkFilter(List *targetList, TupleTableSlot *slot)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
JunkFilter *junkfilter;
|
1999-10-31 01:13:30 +02:00
|
|
|
TupleDesc cleanTupType;
|
2004-10-07 20:38:51 +02:00
|
|
|
int cleanLength;
|
|
|
|
AttrNumber *cleanMap;
|
2004-05-26 06:41:50 +02:00
|
|
|
ListCell *t;
|
1997-09-08 04:41:22 +02:00
|
|
|
AttrNumber cleanResno;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2004-10-07 20:38:51 +02:00
|
|
|
* Compute the tuple descriptor for the cleaned tuple.
|
1997-09-07 07:04:48 +02: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
|
|
|
cleanTupType = ExecCleanTypeFromTL(targetList);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2005-03-16 22:38:10 +01:00
|
|
|
/*
|
|
|
|
* Use the given slot, or make a new slot if we weren't given one.
|
|
|
|
*/
|
|
|
|
if (slot)
|
2006-06-16 20:42:24 +02:00
|
|
|
ExecSetSlotDescriptor(slot, cleanTupType);
|
2005-03-16 22:38:10 +01:00
|
|
|
else
|
Introduce notion of different types of slots (without implementing them).
Upcoming work intends to allow pluggable ways to introduce new ways of
storing table data. Accessing those table access methods from the
executor requires TupleTableSlots to be carry tuples in the native
format of such storage methods; otherwise there'll be a significant
conversion overhead.
Different access methods will require different data to store tuples
efficiently (just like virtual, minimal, heap already require fields
in TupleTableSlot). To allow that without requiring additional pointer
indirections, we want to have different structs (embedding
TupleTableSlot) for different types of slots. Thus different types of
slots are needed, which requires adapting creators of slots.
The slot that most efficiently can represent a type of tuple in an
executor node will often depend on the type of slot a child node
uses. Therefore we need to track the type of slot is returned by
nodes, so parent slots can create slots based on that.
Relatedly, JIT compilation of tuple deforming needs to know which type
of slot a certain expression refers to, so it can create an
appropriate deforming function for the type of tuple in the slot.
But not all nodes will only return one type of slot, e.g. an append
node will potentially return different types of slots for each of its
subplans.
Therefore add function that allows to query the type of a node's
result slot, and whether it'll always be the same type (whether it's
fixed). This can be queried using ExecGetResultSlotOps().
The scan, result, inner, outer type of slots are automatically
inferred from ExecInitScanTupleSlot(), ExecInitResultSlot(),
left/right subtrees respectively. If that's not correct for a node,
that can be overwritten using new fields in PlanState.
This commit does not introduce the actually abstracted implementation
of different kind of TupleTableSlots, that will be left for a followup
commit. The different types of slots introduced will, for now, still
use the same backing implementation.
While this already partially invalidates the big comment in
tuptable.h, it seems to make more sense to update it later, when the
different TupleTableSlot implementations actually exist.
Author: Ashutosh Bapat and Andres Freund, with changes by Amit Khandekar
Discussion: https://postgr.es/m/20181105210039.hh4vvi4vwoq5ba2q@alap3.anarazel.de
2018-11-16 07:00:30 +01:00
|
|
|
slot = MakeSingleTupleTableSlot(cleanTupType, &TTSOpsVirtual);
|
2005-03-16 22:38:10 +01:00
|
|
|
|
2004-10-07 20:38:51 +02:00
|
|
|
/*
|
|
|
|
* Now calculate the mapping between the original tuple's attributes and
|
|
|
|
* the "clean" tuple's attributes.
|
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* The "map" is an array of "cleanLength" attribute numbers, i.e. one
|
|
|
|
* entry for every attribute of the "clean" tuple. The value of this entry
|
|
|
|
* is the attribute number of the corresponding attribute of the
|
|
|
|
* "original" tuple. (Zero indicates a NULL output attribute, but we do
|
|
|
|
* not use that feature in this routine.)
|
2004-10-07 20:38:51 +02:00
|
|
|
*/
|
|
|
|
cleanLength = cleanTupType->natts;
|
|
|
|
if (cleanLength > 0)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2004-10-07 20:38:51 +02:00
|
|
|
cleanMap = (AttrNumber *) palloc(cleanLength * sizeof(AttrNumber));
|
|
|
|
cleanResno = 1;
|
|
|
|
foreach(t, targetList)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2004-10-07 20:38:51 +02:00
|
|
|
TargetEntry *tle = lfirst(t);
|
|
|
|
|
2005-04-06 18:34:07 +02:00
|
|
|
if (!tle->resjunk)
|
2004-10-07 20:38:51 +02:00
|
|
|
{
|
2005-04-06 18:34:07 +02:00
|
|
|
cleanMap[cleanResno - 1] = tle->resno;
|
2004-10-07 20:38:51 +02:00
|
|
|
cleanResno++;
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
2004-10-07 20:38:51 +02:00
|
|
|
else
|
|
|
|
cleanMap = NULL;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2004-10-07 20:38:51 +02:00
|
|
|
* Finally create and initialize the JunkFilter struct.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2004-10-07 20:38:51 +02:00
|
|
|
junkfilter = makeNode(JunkFilter);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2004-10-07 20:38:51 +02:00
|
|
|
junkfilter->jf_targetList = targetList;
|
|
|
|
junkfilter->jf_cleanTupType = cleanTupType;
|
|
|
|
junkfilter->jf_cleanMap = cleanMap;
|
|
|
|
junkfilter->jf_resultSlot = slot;
|
|
|
|
|
|
|
|
return junkfilter;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ExecInitJunkFilterConversion
|
|
|
|
*
|
|
|
|
* Initialize a JunkFilter for rowtype conversions.
|
|
|
|
*
|
|
|
|
* Here, we are given the target "clean" tuple descriptor rather than
|
|
|
|
* inferring it from the targetlist. The target descriptor can contain
|
|
|
|
* deleted columns. It is assumed that the caller has checked that the
|
|
|
|
* non-deleted columns match up with the non-junk columns of the targetlist.
|
|
|
|
*/
|
|
|
|
JunkFilter *
|
|
|
|
ExecInitJunkFilterConversion(List *targetList,
|
|
|
|
TupleDesc cleanTupType,
|
|
|
|
TupleTableSlot *slot)
|
|
|
|
{
|
|
|
|
JunkFilter *junkfilter;
|
|
|
|
int cleanLength;
|
|
|
|
AttrNumber *cleanMap;
|
|
|
|
ListCell *t;
|
|
|
|
int i;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2005-03-16 22:38:10 +01:00
|
|
|
/*
|
|
|
|
* Use the given slot, or make a new slot if we weren't given one.
|
|
|
|
*/
|
|
|
|
if (slot)
|
2006-06-16 20:42:24 +02:00
|
|
|
ExecSetSlotDescriptor(slot, cleanTupType);
|
2005-03-16 22:38:10 +01:00
|
|
|
else
|
Introduce notion of different types of slots (without implementing them).
Upcoming work intends to allow pluggable ways to introduce new ways of
storing table data. Accessing those table access methods from the
executor requires TupleTableSlots to be carry tuples in the native
format of such storage methods; otherwise there'll be a significant
conversion overhead.
Different access methods will require different data to store tuples
efficiently (just like virtual, minimal, heap already require fields
in TupleTableSlot). To allow that without requiring additional pointer
indirections, we want to have different structs (embedding
TupleTableSlot) for different types of slots. Thus different types of
slots are needed, which requires adapting creators of slots.
The slot that most efficiently can represent a type of tuple in an
executor node will often depend on the type of slot a child node
uses. Therefore we need to track the type of slot is returned by
nodes, so parent slots can create slots based on that.
Relatedly, JIT compilation of tuple deforming needs to know which type
of slot a certain expression refers to, so it can create an
appropriate deforming function for the type of tuple in the slot.
But not all nodes will only return one type of slot, e.g. an append
node will potentially return different types of slots for each of its
subplans.
Therefore add function that allows to query the type of a node's
result slot, and whether it'll always be the same type (whether it's
fixed). This can be queried using ExecGetResultSlotOps().
The scan, result, inner, outer type of slots are automatically
inferred from ExecInitScanTupleSlot(), ExecInitResultSlot(),
left/right subtrees respectively. If that's not correct for a node,
that can be overwritten using new fields in PlanState.
This commit does not introduce the actually abstracted implementation
of different kind of TupleTableSlots, that will be left for a followup
commit. The different types of slots introduced will, for now, still
use the same backing implementation.
While this already partially invalidates the big comment in
tuptable.h, it seems to make more sense to update it later, when the
different TupleTableSlot implementations actually exist.
Author: Ashutosh Bapat and Andres Freund, with changes by Amit Khandekar
Discussion: https://postgr.es/m/20181105210039.hh4vvi4vwoq5ba2q@alap3.anarazel.de
2018-11-16 07:00:30 +01:00
|
|
|
slot = MakeSingleTupleTableSlot(cleanTupType, &TTSOpsVirtual);
|
2005-03-16 22:38:10 +01:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Calculate the mapping between the original tuple's attributes and the
|
|
|
|
* "clean" tuple's attributes.
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* The "map" is an array of "cleanLength" attribute numbers, i.e. one
|
|
|
|
* entry for every attribute of the "clean" tuple. The value of this entry
|
|
|
|
* is the attribute number of the corresponding attribute of the
|
|
|
|
* "original" tuple. We store zero for any deleted attributes, marking
|
|
|
|
* that a NULL is needed in the output tuple.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2004-10-07 20:38:51 +02:00
|
|
|
cleanLength = cleanTupType->natts;
|
1997-09-07 07:04:48 +02:00
|
|
|
if (cleanLength > 0)
|
|
|
|
{
|
2004-10-07 20:38:51 +02:00
|
|
|
cleanMap = (AttrNumber *) palloc0(cleanLength * sizeof(AttrNumber));
|
|
|
|
t = list_head(targetList);
|
|
|
|
for (i = 0; i < cleanLength; i++)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2017-08-20 20:19:07 +02:00
|
|
|
if (TupleDescAttr(cleanTupType, i)->attisdropped)
|
2004-10-07 20:38:51 +02:00
|
|
|
continue; /* map entry is already zero */
|
|
|
|
for (;;)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2004-10-07 20:38:51 +02:00
|
|
|
TargetEntry *tle = lfirst(t);
|
|
|
|
|
Represent Lists as expansible arrays, not chains of cons-cells.
Originally, Postgres Lists were a more or less exact reimplementation of
Lisp lists, which consist of chains of separately-allocated cons cells,
each having a value and a next-cell link. We'd hacked that once before
(commit d0b4399d8) to add a separate List header, but the data was still
in cons cells. That makes some operations -- notably list_nth() -- O(N),
and it's bulky because of the next-cell pointers and per-cell palloc
overhead, and it's very cache-unfriendly if the cons cells end up
scattered around rather than being adjacent.
In this rewrite, we still have List headers, but the data is in a
resizable array of values, with no next-cell links. Now we need at
most two palloc's per List, and often only one, since we can allocate
some values in the same palloc call as the List header. (Of course,
extending an existing List may require repalloc's to enlarge the array.
But this involves just O(log N) allocations not O(N).)
Of course this is not without downsides. The key difficulty is that
addition or deletion of a list entry may now cause other entries to
move, which it did not before.
For example, that breaks foreach() and sister macros, which historically
used a pointer to the current cons-cell as loop state. We can repair
those macros transparently by making their actual loop state be an
integer list index; the exposed "ListCell *" pointer is no longer state
carried across loop iterations, but is just a derived value. (In
practice, modern compilers can optimize things back to having just one
loop state value, at least for simple cases with inline loop bodies.)
In principle, this is a semantics change for cases where the loop body
inserts or deletes list entries ahead of the current loop index; but
I found no such cases in the Postgres code.
The change is not at all transparent for code that doesn't use foreach()
but chases lists "by hand" using lnext(). The largest share of such
code in the backend is in loops that were maintaining "prev" and "next"
variables in addition to the current-cell pointer, in order to delete
list cells efficiently using list_delete_cell(). However, we no longer
need a previous-cell pointer to delete a list cell efficiently. Keeping
a next-cell pointer doesn't work, as explained above, but we can improve
matters by changing such code to use a regular foreach() loop and then
using the new macro foreach_delete_current() to delete the current cell.
(This macro knows how to update the associated foreach loop's state so
that no cells will be missed in the traversal.)
There remains a nontrivial risk of code assuming that a ListCell *
pointer will remain good over an operation that could now move the list
contents. To help catch such errors, list.c can be compiled with a new
define symbol DEBUG_LIST_MEMORY_USAGE that forcibly moves list contents
whenever that could possibly happen. This makes list operations
significantly more expensive so it's not normally turned on (though it
is on by default if USE_VALGRIND is on).
There are two notable API differences from the previous code:
* lnext() now requires the List's header pointer in addition to the
current cell's address.
* list_delete_cell() no longer requires a previous-cell argument.
These changes are somewhat unfortunate, but on the other hand code using
either function needs inspection to see if it is assuming anything
it shouldn't, so it's not all bad.
Programmers should be aware of these significant performance changes:
* list_nth() and related functions are now O(1); so there's no
major access-speed difference between a list and an array.
* Inserting or deleting a list element now takes time proportional to
the distance to the end of the list, due to moving the array elements.
(However, it typically *doesn't* require palloc or pfree, so except in
long lists it's probably still faster than before.) Notably, lcons()
used to be about the same cost as lappend(), but that's no longer true
if the list is long. Code that uses lcons() and list_delete_first()
to maintain a stack might usefully be rewritten to push and pop at the
end of the list rather than the beginning.
* There are now list_insert_nth...() and list_delete_nth...() functions
that add or remove a list cell identified by index. These have the
data-movement penalty explained above, but there's no search penalty.
* list_concat() and variants now copy the second list's data into
storage belonging to the first list, so there is no longer any
sharing of cells between the input lists. The second argument is
now declared "const List *" to reflect that it isn't changed.
This patch just does the minimum needed to get the new implementation
in place and fix bugs exposed by the regression tests. As suggested
by the foregoing, there's a fair amount of followup work remaining to
do.
Also, the ENABLE_LIST_COMPAT macros are finally removed in this
commit. Code using those should have been gone a dozen years ago.
Patch by me; thanks to David Rowley, Jesper Pedersen, and others
for review.
Discussion: https://postgr.es/m/11587.1550975080@sss.pgh.pa.us
2019-07-15 19:41:58 +02:00
|
|
|
t = lnext(targetList, t);
|
2005-04-06 18:34:07 +02:00
|
|
|
if (!tle->resjunk)
|
2004-10-07 20:38:51 +02:00
|
|
|
{
|
2005-04-06 18:34:07 +02:00
|
|
|
cleanMap[i] = tle->resno;
|
2004-10-07 20:38:51 +02:00
|
|
|
break;
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
cleanMap = NULL;
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2001-01-29 01:39:20 +01:00
|
|
|
* Finally create and initialize the JunkFilter struct.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
|
|
|
junkfilter = makeNode(JunkFilter);
|
|
|
|
|
|
|
|
junkfilter->jf_targetList = targetList;
|
|
|
|
junkfilter->jf_cleanTupType = cleanTupType;
|
|
|
|
junkfilter->jf_cleanMap = cleanMap;
|
2001-05-27 22:48:51 +02:00
|
|
|
junkfilter->jf_resultSlot = slot;
|
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return junkfilter;
|
2001-01-29 01:39:20 +01:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2004-10-07 20:38:51 +02:00
|
|
|
/*
|
2006-12-04 03:06:55 +01:00
|
|
|
* ExecFindJunkAttribute
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2006-12-04 03:06:55 +01:00
|
|
|
* Locate the specified junk attribute in the junk filter's targetlist,
|
|
|
|
* and return its resno. Returns InvalidAttrNumber if not found.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2006-12-04 03:06:55 +01:00
|
|
|
AttrNumber
|
|
|
|
ExecFindJunkAttribute(JunkFilter *junkfilter, const char *attrName)
|
2011-01-13 02:47:02 +01:00
|
|
|
{
|
|
|
|
return ExecFindJunkAttributeInTlist(junkfilter->jf_targetList, attrName);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ExecFindJunkAttributeInTlist
|
|
|
|
*
|
|
|
|
* Find a junk attribute given a subplan's targetlist (not necessarily
|
|
|
|
* part of a JunkFilter).
|
|
|
|
*/
|
|
|
|
AttrNumber
|
|
|
|
ExecFindJunkAttributeInTlist(List *targetlist, const char *attrName)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2004-05-26 06:41:50 +02:00
|
|
|
ListCell *t;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2011-01-13 02:47:02 +01:00
|
|
|
foreach(t, targetlist)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
TargetEntry *tle = lfirst(t);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2005-04-06 18:34:07 +02:00
|
|
|
if (tle->resjunk && tle->resname &&
|
|
|
|
(strcmp(tle->resname, attrName) == 0))
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
|
|
|
/* We found it ! */
|
2006-12-04 03:06:55 +01:00
|
|
|
return tle->resno;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-04 03:06:55 +01:00
|
|
|
return InvalidAttrNumber;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ExecGetJunkAttribute
|
|
|
|
*
|
|
|
|
* Given a junk filter's input tuple (slot) and a junk attribute's number
|
|
|
|
* previously found by ExecFindJunkAttribute, extract & return the value and
|
|
|
|
* isNull flag of the attribute.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
ExecGetJunkAttribute(TupleTableSlot *slot, AttrNumber attno,
|
|
|
|
bool *isNull)
|
|
|
|
{
|
|
|
|
Assert(attno > 0);
|
|
|
|
|
|
|
|
return slot_getattr(slot, attno, isNull);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2004-10-07 20:38:51 +02:00
|
|
|
/*
|
2005-03-16 22:38:10 +01:00
|
|
|
* ExecFilterJunk
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2005-03-16 22:38:10 +01:00
|
|
|
* Construct and return a slot with all the junk attributes removed.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2005-03-16 22:38:10 +01:00
|
|
|
TupleTableSlot *
|
|
|
|
ExecFilterJunk(JunkFilter *junkfilter, TupleTableSlot *slot)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2005-03-16 22:38:10 +01:00
|
|
|
TupleTableSlot *resultSlot;
|
1997-09-08 04:41:22 +02:00
|
|
|
AttrNumber *cleanMap;
|
|
|
|
TupleDesc cleanTupType;
|
|
|
|
int cleanLength;
|
|
|
|
int i;
|
|
|
|
Datum *values;
|
2005-03-16 22:38:10 +01:00
|
|
|
bool *isnull;
|
2004-06-04 22:35:21 +02:00
|
|
|
Datum *old_values;
|
2005-03-16 22:38:10 +01:00
|
|
|
bool *old_isnull;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2005-03-16 22:38:10 +01:00
|
|
|
* Extract all the values of the old tuple.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2005-03-16 22:38:10 +01:00
|
|
|
slot_getallattrs(slot);
|
|
|
|
old_values = slot->tts_values;
|
|
|
|
old_isnull = slot->tts_isnull;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2005-03-16 22:38:10 +01:00
|
|
|
/*
|
|
|
|
* get info from the junk filter
|
|
|
|
*/
|
2001-01-29 01:39:20 +01:00
|
|
|
cleanTupType = junkfilter->jf_cleanTupType;
|
2004-10-07 20:38:51 +02:00
|
|
|
cleanLength = cleanTupType->natts;
|
1997-09-07 07:04:48 +02:00
|
|
|
cleanMap = junkfilter->jf_cleanMap;
|
2005-03-16 22:38:10 +01:00
|
|
|
resultSlot = junkfilter->jf_resultSlot;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2005-03-16 22:38:10 +01:00
|
|
|
* Prepare to build a virtual result tuple.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2005-03-16 22:38:10 +01:00
|
|
|
ExecClearTuple(resultSlot);
|
|
|
|
values = resultSlot->tts_values;
|
|
|
|
isnull = resultSlot->tts_isnull;
|
2004-06-04 22:35:21 +02:00
|
|
|
|
|
|
|
/*
|
2005-03-16 22:38:10 +01:00
|
|
|
* Transpose data into proper fields of the new tuple.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
|
|
|
for (i = 0; i < cleanLength; i++)
|
|
|
|
{
|
2004-10-07 20:38:51 +02:00
|
|
|
int j = cleanMap[i];
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2005-03-16 22:38:10 +01:00
|
|
|
if (j == 0)
|
|
|
|
{
|
|
|
|
values[i] = (Datum) 0;
|
|
|
|
isnull[i] = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
values[i] = old_values[j - 1];
|
|
|
|
isnull[i] = old_isnull[j - 1];
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2005-03-16 22:38:10 +01:00
|
|
|
* And return the virtual tuple.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2005-03-16 22:38:10 +01:00
|
|
|
return ExecStoreVirtualTuple(resultSlot);
|
|
|
|
}
|