Обсуждение: A question on systable_beginscan()

Поиск
Список
Период
Сортировка

A question on systable_beginscan()

От
Onder Kalaci
Дата:
Hi hackers,

As it's documented in the source code, systable_beginscan() could be used to on non-system tables as well. My question is that, is it possible to write a C code with systable_beginscan(), systable_getnext() and ScanKeys which is equivalent to the following query: (Assume that the qual_column is a text column)

SELECT id FROM table WHERE qual_column::int = 15;

In other words, can we cast a column type to another type via these low-level APIs? Are there any examples in the Postgres source?

Thanks,
Onder

Re: A question on systable_beginscan()

От
Amit Langote
Дата:
Hi,

On 2016/03/25 23:49, Onder Kalaci wrote:
> Hi hackers,
> 
> As it's documented in the source code, systable_beginscan() could be used
> to on non-system tables as well. My question is that, is it possible to
> write a C code with systable_beginscan(), systable_getnext() and ScanKeys
> which is equivalent to the following query: (Assume that the qual_column is
> a text column)
> 
> SELECT id FROM table WHERE qual_column::int = 15;
> 
> In other words, can we cast a column type to another type via these
> low-level APIs? Are there any examples in the Postgres source?

Don't think the API advertises any support for that or at least I couldn't
find any. However, I am not sure if it's outright impossible to achieve
that with some prep - <uglyhack> initialize the scan key (sk_func) with
FmgrInfo of a specialized function, that performs the cast before
comparing.  Such a function would be written to work with fixed pair of
source and target types to be able to invoke, say, textout() -> int4in(),
before comparing the value with the argument (sk_argument). The argument
would have to be of the target type, needless to say </uglyhack>. Most
likely, this would be an unrecommended course of action in the long run,
so you would be better off considering other ways (like SPI).

Thanks,
Amit