Get the offset of a tuple inside a table

Started by Pei Heover 15 years ago9 messages
#1Pei He
hepeimail@gmail.com

Hi,
When I using an index scan, can I get the offset of the tuple in the table?

Thanks
--
Pei

#2Szymon Guz
mabewlun@gmail.com
In reply to: Pei He (#1)
Re: Get the offset of a tuple inside a table

On 21 September 2010 23:02, Pei He <hepeimail@gmail.com> wrote:

Hi,
When I using an index scan, can I get the offset of the tuple in the table?

Thanks
--
Pei

What do you mean by "the offset in the table"?

#3Kevin Grittner
Kevin.Grittner@wicourts.gov
In reply to: Pei He (#1)
Re: Get the offset of a tuple inside a table

Pei He <hepeimail@gmail.com> wrote:

When I using an index scan, can I get the offset of the tuple in
the table?

Well, regardless of the type of scan, you can select the ctid
column, which gives the block number and the tuple number within the
block.

-Kevin

#4Pei He
hepeimail@gmail.com
In reply to: Szymon Guz (#2)
Re: Get the offset of a tuple inside a table

Hi Szymon, Kevin,
The offset is the order of a tuple in a Sequential Scan.

What I want to do is, for a given key return the tuples that Index scan can
find, and return the rest tuples by a seq scan. So, I need to know which
tuples have been returned by maintain a bitmap, and to avoid return the same
tuple twice.

If I can know the offset of a tuple in the order of file scan, then I can
force the seq scan to skip it.

By ctid, can I know how much tuples are in a block, is it an constant?

I think a more general solution would be build a hash table on ctid.
What do you think?

Thanks
--
Pei

On Tue, Sep 21, 2010 at 5:44 PM, Szymon Guz <mabewlun@gmail.com> wrote:

Show quoted text

On 21 September 2010 23:02, Pei He <hepeimail@gmail.com> wrote:

Hi,
When I using an index scan, can I get the offset of the tuple in the
table?

Thanks
--
Pei

What do you mean by "the offset in the table"?

#5Kevin Grittner
Kevin.Grittner@wicourts.gov
In reply to: Pei He (#4)
Re: Get the offset of a tuple inside a table

Pei He wrote:

The offset is the order of a tuple in a Sequential Scan.

That's not a safe assumption. Try starting a sequential scan against
a large table on one connection; then before it finishes, start the
same query on another connection. The second query joins the one
already in progress and then starts over, "wrapping around".

-Kevin

#6Pei He
hepeimail@gmail.com
In reply to: Kevin Grittner (#5)
Re: Get the offset of a tuple inside a table

I doesn't realized postgresql can be such clever.

Another problem I just found is after one tuple is deleted, there will be
some slots unoccupied. The offset cannot be computed even know how much
tuples a page can contain.

I need one hash table.

Thanks
--
Pei

On Tue, Sep 21, 2010 at 8:47 PM, Kevin Grittner <Kevin.Grittner@wicourts.gov

Show quoted text

wrote:

Pei He wrote:

The offset is the order of a tuple in a Sequential Scan.

That's not a safe assumption. Try starting a sequential scan against
a large table on one connection; then before it finishes, start the
same query on another connection. The second query joins the one
already in progress and then starts over, "wrapping around".

-Kevin

#7Tom Lane
tgl@sss.pgh.pa.us
In reply to: Pei He (#4)
Re: Get the offset of a tuple inside a table

Pei He <hepeimail@gmail.com> writes:

What I want to do is, for a given key return the tuples that Index scan can
find, and return the rest tuples by a seq scan. So, I need to know which
tuples have been returned by maintain a bitmap, and to avoid return the same
tuple twice.

If I can know the offset of a tuple in the order of file scan, then I can
force the seq scan to skip it.

As pointed out, "offset" is an unworkable concept here. That's why the
tidbitmap code doesn't work with offsets; it works with tids. You don't
really need to reinvent this wheel. Go read the bitmapscan code.

(One wonders though what you think you are going to save if you have to
do a seqscan anyway. Where's the advantage over just doing a seqscan?)

regards, tom lane

#8Pei He
hepeimail@gmail.com
In reply to: Tom Lane (#7)
Re: Get the offset of a tuple inside a table

Hi Tom,
The bitmapset works for me.

I want to implement the operator for the following query:

Select * from a left join b on a.id = b.id order by b.id;

In a left outer join, I want the tuples that have matches in the inner table
appear first. So, the order by clause is need.

If there is a index on a.id, I can use the tuples in b to probe the index.

After return all the tuples retrieved through index, it needs to return the
rest tuples in a, because it is a left outer join in the query.

What I need to do is remember what have been returned by the index, and
avoid to return it twice.

The bitmapscan needs to remember what have to been retrieved later, so it
used the tidbitmap. But, for me, I need the bitmapset.

Thanks, your reply helps me to find the bitmapset.

Regards
--
Pei

On Tue, Sep 21, 2010 at 9:32 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:

Show quoted text

Pei He <hepeimail@gmail.com> writes:

What I want to do is, for a given key return the tuples that Index scan

can

find, and return the rest tuples by a seq scan. So, I need to know which
tuples have been returned by maintain a bitmap, and to avoid return the

same

tuple twice.

If I can know the offset of a tuple in the order of file scan, then I can
force the seq scan to skip it.

As pointed out, "offset" is an unworkable concept here. That's why the
tidbitmap code doesn't work with offsets; it works with tids. You don't
really need to reinvent this wheel. Go read the bitmapscan code.

(One wonders though what you think you are going to save if you have to
do a seqscan anyway. Where's the advantage over just doing a seqscan?)

regards, tom lane

#9Robert Haas
robertmhaas@gmail.com
In reply to: Pei He (#8)
Re: Get the offset of a tuple inside a table

On Tue, Sep 21, 2010 at 10:58 PM, Pei He <hepeimail@gmail.com> wrote:

Hi Tom,
The bitmapset works for me.

I want to implement the operator for the following query:

Select * from a left join b on a.id = b.id order by b.id;

In a left outer join, I want the tuples that have matches in the inner table
appear first. So, the order by clause is need.

Why can't you just write SELECT * FROM a LEFT JOIN b ON a.id = b.id
ORDER BY b.id NULLS FIRST? "I want my query results in a different
order" is almost never something that requires modifying the source
code.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise Postgres Company