@@ -41,7 +41,7 @@ PG_MODULE_MAGIC;
41
41
extern Datum PGUT_EXPORT repack_version (PG_FUNCTION_ARGS );
42
42
extern Datum PGUT_EXPORT repack_trigger (PG_FUNCTION_ARGS );
43
43
extern Datum PGUT_EXPORT repack_apply (PG_FUNCTION_ARGS );
44
- extern Datum PGUT_EXPORT repack_get_index_keys (PG_FUNCTION_ARGS );
44
+ extern Datum PGUT_EXPORT repack_get_order_by (PG_FUNCTION_ARGS );
45
45
extern Datum PGUT_EXPORT repack_indexdef (PG_FUNCTION_ARGS );
46
46
extern Datum PGUT_EXPORT repack_swap (PG_FUNCTION_ARGS );
47
47
extern Datum PGUT_EXPORT repack_drop (PG_FUNCTION_ARGS );
@@ -50,7 +50,7 @@ extern Datum PGUT_EXPORT repack_disable_autovacuum(PG_FUNCTION_ARGS);
50
50
PG_FUNCTION_INFO_V1 (repack_version );
51
51
PG_FUNCTION_INFO_V1 (repack_trigger );
52
52
PG_FUNCTION_INFO_V1 (repack_apply );
53
- PG_FUNCTION_INFO_V1 (repack_get_index_keys );
53
+ PG_FUNCTION_INFO_V1 (repack_get_order_by );
54
54
PG_FUNCTION_INFO_V1 (repack_indexdef );
55
55
PG_FUNCTION_INFO_V1 (repack_swap );
56
56
PG_FUNCTION_INFO_V1 (repack_drop );
@@ -472,21 +472,51 @@ parse_indexdef(IndexDef *stmt, Oid index, Oid table)
472
472
stmt -> options = sql ;
473
473
}
474
474
475
+ /*
476
+ * Parse the trailing ... [ COLLATE X ] [ DESC ] [ NULLS { FIRST | LAST } ] from an index
477
+ * definition column.
478
+ * Returned values point to token. \0's are inserted to separate parsed parts.
479
+ */
480
+ static void
481
+ parse_indexdef_col (char * token , char * * desc , char * * nulls , char * * collate )
482
+ {
483
+ char * pos ;
484
+
485
+ /* easier to walk backwards than to parse quotes and escapes... */
486
+ if (NULL != (pos = strstr (token , " NULLS FIRST" )))
487
+ {
488
+ * nulls = pos + 1 ;
489
+ * pos = '\0' ;
490
+ }
491
+ else if (NULL != (pos = strstr (token , " NULLS LAST" )))
492
+ {
493
+ * nulls = pos + 1 ;
494
+ * pos = '\0' ;
495
+ }
496
+ if (NULL != (pos = strstr (token , " DESC" )))
497
+ {
498
+ * desc = pos + 1 ;
499
+ * pos = '\0' ;
500
+ }
501
+ if (NULL != (pos = strstr (token , " COLLATE " )))
502
+ {
503
+ * collate = pos + 1 ;
504
+ * pos = '\0' ;
505
+ }
506
+ }
507
+
475
508
/**
476
- * @fn Datum repack_get_index_keys (PG_FUNCTION_ARGS)
509
+ * @fn Datum repack_get_order_by (PG_FUNCTION_ARGS)
477
510
* @brief Get key definition of the index.
478
511
*
479
- * repack_get_index_keys (index, table)
512
+ * repack_get_order_by (index, table)
480
513
*
481
514
* @param index Oid of target index.
482
515
* @param table Oid of table of the index.
483
516
* @retval Create index DDL for temp table.
484
- *
485
- * FIXME: this function is named get_index_keys, but actually returns
486
- * an expression for ORDER BY clause. get_order_by() might be a better name.
487
517
*/
488
518
Datum
489
- repack_get_index_keys (PG_FUNCTION_ARGS )
519
+ repack_get_order_by (PG_FUNCTION_ARGS )
490
520
{
491
521
Oid index = PG_GETARG_OID (0 );
492
522
Oid table = PG_GETARG_OID (1 );
@@ -514,12 +544,21 @@ repack_get_index_keys(PG_FUNCTION_ARGS)
514
544
for (nattr = 0 , next = stmt .columns ; next ; nattr ++ )
515
545
{
516
546
char * opcname ;
547
+ char * coldesc = NULL ;
548
+ char * colnulls = NULL ;
549
+ char * colcollate = NULL ;
517
550
518
551
token = next ;
519
552
while (isspace ((unsigned char ) * token ))
520
553
token ++ ;
521
554
next = skip_until (index , next , ',' );
555
+ parse_indexdef_col (token , & coldesc , & colnulls , & colcollate );
522
556
opcname = skip_until (index , token , ' ' );
557
+ appendStringInfoString (& str , token );
558
+ if (colcollate )
559
+ appendStringInfo (& str , " %s" , colcollate );
560
+ if (coldesc )
561
+ appendStringInfo (& str , " %s" , coldesc );
523
562
if (opcname )
524
563
{
525
564
/* lookup default operator name from operator class */
@@ -556,12 +595,11 @@ repack_get_index_keys(PG_FUNCTION_ARGS)
556
595
elog (ERROR , "missing operator %d(%u,%u) in opfamily %u" ,
557
596
strategy , opcintype , opcintype , opfamily );
558
597
559
-
560
598
opcname [-1 ] = '\0' ;
561
- appendStringInfo (& str , "%s USING %s" , token , get_opname (oprid ));
599
+ appendStringInfo (& str , " USING %s" , get_opname (oprid ));
562
600
}
563
- else
564
- appendStringInfoString (& str , token );
601
+ if ( colnulls )
602
+ appendStringInfo (& str , " %s" , colnulls );
565
603
if (next )
566
604
appendStringInfoString (& str , ", " );
567
605
}
0 commit comments